如何通过 Telegram Bot API 发送文档

时间:2021-05-13 10:58:09

标签: bots telegram telegram-bot

我想通过 Telegram bot API 发送文档,并且我的文档存储在本地。我试过了:

https://api.telegram.org/botToken/sendDocument?chat_id=-5278798&document=C:\Users\Administrator\Desktop\file.txt

但是我遇到了错误...

<块引用>

Bad Request: failed to get HTTP URL content

此方法仅适用于远程 URL,如何通过 bot API 发送文档?

2 个答案:

答案 0 :(得分:1)

你不能使用你电脑上的本地文件,因为我假设你是通过 URL 中的 document=C:\Users\Administrator\Desktop\file.txt 完成的 在文档中,它指出“通过 file_id 发送存在于 Telegram 服务器上的文件(推荐),为 Telegram 传递 HTTP URL 以从 Internet 获取文件。” Telegram media Documents 这意味着您要么必须使用电报服务器上的文件,要么将其上传到网站并通过您上传的 URL 发送。

答案 1 :(得分:1)

使用 python-telegram-bot 您可以使用 send_document 方法发送本地文件

 c_id = '000011'   
 filename = '/tmp/googledoc.docx'

 context.bot.send_document(chat_id='c_id, document=open('googledoc.docx', 'rb'), filename="googledoc.docx")