如何根据Telegram上的文件ID发送文件

问题描述 投票:0回答:1

如何根据文件ID将Telegram上存在的文件(非转发)发送给用户?

我知道Telegram上传的所有文件都有唯一的ID。如何提取该ID以及通过提取的ID发送该文件的功能或方法是什么? (我使用Python Telepot库)

import telepot
from telepot.loop import MessageLoop
def on_chat_message(msg):
content_type, chat_type, chat_id = telepot.glance(msg,'chat')
if content_type == 'text':
    user_msg_text = msg['text']
    if user_msg_text == '/start':
        bot.sendDocument(chat_id=chat_id, )
TOKEN = "479761462:AAE8yqX2RGCbynHJgShIdJzCZWYF9SSBUkU"
bot = telepot.Bot(TOKEN)
MessageLoop(bot, {'chat': on_chat_message,
                  'callback_query': on_callback_query}).run_as_thread()
print('Listening ...')

我应该在sendDocument()方法的第二个参数中输入什么?

python telegram telegram-bot python-telegram-bot telepot
1个回答
0
投票
import telepot
from telepot.loop import MessageLoop
def on_chat_message(msg):
   content_type, chat_type, chat_id = telepot.glance(msg,'chat')
     if content_type == 'document':
        file_id = msg['document']['file_id']
        print(file_id)
TOKEN = "479761462:AAE8yqX2RGCbynHJgShIdJzCZWYF9SSBUkU"
bot = telepot.Bot(TOKEN)
MessageLoop(bot, {'chat': on_chat_message,
              'callback_query': on_callback_query}).run_as_thread()
print('Listening ...')

您应该首先从文档文件中提取file_id。当您运行机器人,将文档发送到机器人,您可以打印并复制它,并将其传递给此命令:bot.sendDocument(chat_id = chat_id,file_id)您应该放置你复制的短语而不是file_id。

© www.soinside.com 2019 - 2024. All rights reserved.