如何使用 Telegram 机器人发送文档?
How to send a document with Telegram bot?
我在 youtube 上学习了一个关于如何创建 Telegram Bot 的教程,现在它只能发送消息,但我什至想发送文档或音频、视频、照片等文件......现在我只是在尝试发送文件,但我很困惑,我不知道该怎么做。
bot 的源代码分为 2 个主要文件。一 responser.py:
def responses(input_text):
user_message = str(input_text).lower()
if user_message in ("test", "testing"):
return "123 working..."
return "The command doesn't exists. Type /help to see the command options."
和main.py:
import constants as key
from telegram.ext import *
import responser as r
print("Hello. Cleint has just started.")
def start_command(update):
update.message.reply_text("The Bot Has Started send you command sir.")
def help_command(update):
update.message.reply_text("""
Welcome to the Cleint Bot. For this purchase the following commands are available:
send - send command is to send the log file from the other side of computer""")
def send_document(update, context):
doc_file = open("image1.png", "rb")
chat_id = update.effctive_chat.id
return context.bot.send_document(chat_id, doc_file)
def handle_message(update, context):
text = str(update.message.text).lower()
response = r.responses(text)
update.message.reply_text(response)
def error(update, context):
print(f"Update {update} cause error: {context.error}")
def main():
updater = Updater(key.API_KEY, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start_command))
dp.add_handler(CommandHandler("help", help_command))
dp.add_handler(MessageHandler(Filters.text, handle_message))
dp.add_error_handler(error)
updater.start_polling()
updater.idle()
main()
有人可以帮我吗?
尝试以下作为您的 send_document
功能:
def send_document(update, context):
chat_id = update.message.chat_id
document = open('image1.png', 'rb')
context.bot.send_document(chat_id, document)
然后将命令 'send' 添加到 main 函数中的 bot,如下所示:
dp.add_handler(CommandHandler("send", send_document))
如果您在 Telegram 中输入 /send,机器人就会将文档发送给您。
我在 youtube 上学习了一个关于如何创建 Telegram Bot 的教程,现在它只能发送消息,但我什至想发送文档或音频、视频、照片等文件......现在我只是在尝试发送文件,但我很困惑,我不知道该怎么做。
bot 的源代码分为 2 个主要文件。一 responser.py:
def responses(input_text):
user_message = str(input_text).lower()
if user_message in ("test", "testing"):
return "123 working..."
return "The command doesn't exists. Type /help to see the command options."
和main.py:
import constants as key
from telegram.ext import *
import responser as r
print("Hello. Cleint has just started.")
def start_command(update):
update.message.reply_text("The Bot Has Started send you command sir.")
def help_command(update):
update.message.reply_text("""
Welcome to the Cleint Bot. For this purchase the following commands are available:
send - send command is to send the log file from the other side of computer""")
def send_document(update, context):
doc_file = open("image1.png", "rb")
chat_id = update.effctive_chat.id
return context.bot.send_document(chat_id, doc_file)
def handle_message(update, context):
text = str(update.message.text).lower()
response = r.responses(text)
update.message.reply_text(response)
def error(update, context):
print(f"Update {update} cause error: {context.error}")
def main():
updater = Updater(key.API_KEY, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start_command))
dp.add_handler(CommandHandler("help", help_command))
dp.add_handler(MessageHandler(Filters.text, handle_message))
dp.add_error_handler(error)
updater.start_polling()
updater.idle()
main()
有人可以帮我吗?
尝试以下作为您的 send_document
功能:
def send_document(update, context):
chat_id = update.message.chat_id
document = open('image1.png', 'rb')
context.bot.send_document(chat_id, document)
然后将命令 'send' 添加到 main 函数中的 bot,如下所示:
dp.add_handler(CommandHandler("send", send_document))
如果您在 Telegram 中输入 /send,机器人就会将文档发送给您。