我想写个能保存照片的电报机器人。这是我的代码,但不起作用。我不知道我有什么问题?
def image_handler(bot, update):
file = bot.getFile(update.message.photo.file_id)
print ("file_id: " + str(update.message.photo.file_id))
file.download('image.jpg')
updater.dispatcher.add_handler(MessageHandler(Filters.photo, image_handler))
updater.start_polling()
updater.idle()请帮助我解决我的问题。
发布于 2018-05-18 22:11:27
update.message.photo是一个照片大小数组(PhotoSize对象)。
使用file = bot.getFile(update.message.photo[-1].file_id)。这将得到的图像最大的大小可用。
发布于 2021-07-18 04:14:58
这是我的密码
from telegram.ext import *
import telegram
def start_command(update, context):
name = update.message.chat.first_name
update.message.reply_text("Hello " + name)
update.message.reply_text("Please share your image")
def image_handler(update, context):
file = update.message.photo[0].file_id
obj = context.bot.get_file(file)
obj.download()
update.message.reply_text("Image received")
def main():
print("Started")
TOKEN = "your-token"
updater = Updater(TOKEN, use_context = True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start_command))
dp.add_handler(MessageHandler(Filters.photo, image_handler))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()发布于 2021-06-16 20:26:27
与公认的答案不同,实际上不需要bot对象来获取文件:
file = update.message.photo[-1].get_file()然后下载该文件:
path = file.download("output.jpg")若要将其用于进一步处理,或只需将其放在设备上:)
https://stackoverflow.com/questions/50388435
复制相似问题