有没有办法使用 python Telegram-Bot 获取个人资料图片更新?

Is there a way to get profile picture updates using python Telegram-Bot?

所以我使用 python-telegram-bot 将电报集成到另一个应用程序中。我的目标是在我的应用程序中使用电报显示用户的个人资料图片。 (用户和群聊)

获取用户或群组的头像很容易,在我的应用程序中下载和使用它也很容易。但是,如果用户更改了他们的头像怎么办?我在文档中找不到任何允许机器人检索图片更改的更新消息或处理程序,甚至对于群组也是如此。

我的第一个想法是首先检索所有图片并将 file_id 存储在数据库中,然后定期检查该用户s/group 的图片并返回查看他们的图片,直到 file_id 匹配数据库中最后保存的 file_id

这与 JobQueue 相结合是我能想到的最好的办法,所以我会使用它来自我回答,但我认为它仍然不是一个完美的解决方案,所以如果有人有更好的主意非常感谢您的回答。

我正在专门为群组寻找更好的解决方案,因为我认为除了群组的最新图片之外没有办法检索任何图片,我的应用程序应该检索 all 其中。我的自我回答的另一个缺陷是,如果用户在这六个小时内两次更改个人资料图片,我只会得到最近的一张。对于在 bot 调用中具有 offset 属性的用户,这可以修复,但是 method 获取组的个人资料图片似乎没有。

tl;博士:

当用户更改他们的 或组 个人资料图片时,我如何检索 更新 使用 [=39 最有效和可靠的方式=]-telegram-bot 和 python 3.5?

这正在使用 telegram.ext.JobQueue 每 6 小时检查一次个人资料图片更新。

# define job queue
j = updater.job_queue

def dl_pfps(bot, job):
    # this assumes that we have a textfile with the following
    # layout: "user_id:last_pfp_file_id" - One per line
    # later we'll write a list back into it with the newest IDs
    user_pfp_list = []
    with open("user_pfps.txt") as f:
        for line in f:
            user_id = line.split(':')[0]
            last_file_id = line.split(':')[1]
            most_recent_pfp = bot.get_user_profile_photos(user_id, limit=1).photos[0]
            if last_file_id == most_recent_pfp[-1].file_id:
                print("No change")
                user_pfp_list.append(user_id + ":" + last_file_id)
            else:
                print("User updated profile picture. Geting full size picture...")
                # download and process the picture
                file_id = most_recent_pfp[-1].file_id
                newFile = bot.getFile(file_id)
                newFile.download('my/filename.jpg')
                user_pfp_list.append(user_id + ":" + file_id)

    # write new list back to file (overwrite current list)
    with open("user_pfps.txt", "w") as f:
        f.write("\n".join(user_pfp_list))

# check for new profile pictures every 6 hours
job_dlpfps = j.run_repeating(dl_pfps, interval=21600, first=0)

这是我能想到的最好的。如果您想在代码中使用它,您必须将 'my/filename.jpg' 调整为正确的文件名,并且您需要在 user_pfps.txt 中生成一个初始列表,每个用户一行,如下所示:user_id:0