python - 下载图片 Python Telegram API

标签 python telegram python-telegram-bot

我按照他们关于下载图像的简短教程进行操作,但我遇到了异常:

telegram.photosize.PhotoSize object at ... is not JSON serializable

捕获图像的函数如下所示:

def photo(bot, update):
    file_id = update.message.photo[-1]
    newFile = bot.getFile(file_id)
    newFile.download('test.jpg')
    bot.sendMessage(chat_id=update.message.chat_id, text="download succesfull")

photo_handler = MessageHandler(Filters.photo, photo)
dispatcher.add_handler(photo_handler)

此时我不知道自己做错了什么,也无法在网上找到任何解决方案。

最佳答案

原来我误解了数据的形状。我最初认为 update.message.photo 集合只包含文件 ID。这导致我在尝试通过 ID 获取文件时传递了错误类型的对象。为了提取文件 ID,我需要从最后一张照片中获取 file_id:

file_id = update.message.photo[-1].file_id

关于python - 下载图片 Python Telegram API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43912852/

相关文章:

python - 安装 django-registration-redux 时没有名为 'django.urls' 的模块

python - 如何获取循环小数的长度?

c# - 如何在 C# 中通过 Telegram Bot 发送消息?

python - 使用 python-telegram-bot 构建菜单的正确方法

python - 采样音频不保留波(矢量)!

Python - 将颜色图应用于灰度 numpy 数组并将其转换为图像

python - Python 中的有效数据验证

java - 尝试在 Spring Boot 应用程序中连接到 Telegram Bot 时出错

Python-telegram-bot - 命令行处理程序未被调用

python - 如何使用远程机器人从服务器发送文件?