python - 如何从 Telegram Bot 发送文件?

标签 python bots telegram telegram-bot python-telegram-bot

我正在编写一个 Telegram 机器人,我需要向用户发送文件。我想获取要发送的文件的 file_id。我正在关注 this answer .

要获取 file_id,我应该将文件发送到我的 Telegram 机器人。问题是如何通过我的机器人将文件发送给自己? (也许使用 API?)

最佳答案

You can send files没有file_id:

There are three ways to send files (photos, stickers, audio, media, etc.):

  • If the file is already stored somewhere on the Telegram servers, you don't need to reupload it: each file object has a file_id field, simply pass this file_id as a parameter instead of uploading. There are no limits for files sent this way.
  • Provide Telegram with an HTTP URL for the file to be sent. Telegram will download and send the file. 5 MB max size for photos and 20 MB max for other types of content.
  • Post the file using multipart/form-data in the usual way that files are uploaded via the browser. 10 MB max size for photos, 50 MB for other files

仅当您之前已经将其发送给某人(使用 URL 或 multipart/form-data)时,您才可以使用 file_id


因此,如果您要发送的文件列表是预定义的“静态”,您可以一次上传所有文件,获取它们的 file_id(使用 getUpdates方法),存储文件与其各自的 file_id 之间的关联,最终您的机器人可以仅使用 file_id 与用户通信。

另一方面,如果可能的文件列表不同,您只能通过 URL 或 multipart/form-data 发送文件。

关于python - 如何从 Telegram Bot 发送文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54080533/

相关文章:

python - 在 Telegram Bot 中发送一些照片时出现内存错误

ios - iOS 机器人上的 Telegram 错误发送多次相同的消息

Telegram-bot 用户控制

python - 如何使用python在 Telegram 上发送照片

python - 迭代外加法 Numpy

python - 如何在 Python 中打开外部程序

python - 如何在Tensorflow中使用变量的旧值和新值?

automation - 是否可以 100% 以编程方式创建、更新和删除(管理)Dialogflow 代理?

python - 使用日期逻辑创建假数据

Python 迭代器中的反向列表