使用 python 获取电报频道发布的照片

getting telegram channel posted photos with python

我想要在特定频道时 post 一张照片我的机器人下载该照片 bot 是频道的成员

这是我的代码:

if update.channel_post.photo:
    bot.get_file(file_id=update.channel_post.photo.file_id).download()

实际行为: 即使 channel post a photo

if 条件也永远不会变为真

Python、python-telegram-bot 和依赖项的版本: 所有

的最新版本

我认为您的代码不起作用的原因是它有错误。 Message.photo实际上是一个不同照片尺寸的列表,所以 file_id=update.channel_post.photo.file_id 应该是 file_id=update.channel_post.photo[-1].file_id(以获得最大尺寸)

我还建议您启用日志记录,这样您会更容易看到此类错误。

import logging
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')