简体   繁体   English

将图像发送到机器人时,电报机器人中没有“getFile”属性

[英]no 'getFile' attribute in the telegram bot when sending image to bot

I Found this from a 2 years ago post about downloading files and I don't know how to fix this我从 2 年前的一篇关于下载文件的帖子中找到了这个,但我不知道如何解决这个问题

def image_handler(bot, update):
    file = bot.getFile(update.message.photo[-1].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()

and the error is "caused error "'Update' object has no attribute 'getFile'" How i can fix this?!并且错误是“导致错误”'更新'object 没有属性'getFile'”我该如何解决这个问题?!

According to the docs for MessageHandler , its second parameter should be a function with this signature:根据MessageHandler 的文档,它的第二个参数应该是具有此签名的 function:

def callback(update: Update, context: CallbackContext)

You're passing your image_handler function as this parameter, so it is expected to have this same signature.您将image_handler function 作为此参数传递,因此预计它具有相同的签名。 Your handler function's signature doesn't seem to match up with this signature.您的处理程序函数的签名似乎与此签名不匹配。 What your function is labeling bot is an Update object.您的 function 标记botUpdate object。 Your function tries to call getFile on that object, but an Update object does not have a method with that name.您的 function 尝试在该 object 上调用getFile ,但更新 object 没有具有该名称的方法。 So you get the error you're getting.所以你得到你得到的错误。

I can't say why the code you found isn't matching up with the situation in which you're trying to use it, nor can I tell you how to make the code work given just the information you've supplied in your question.我不能说为什么您找到的代码与您尝试使用它的情况不匹配,也不能告诉您如何仅根据您在问题中提供的信息来使代码工作. You might try reversing the order of the function parameters so that the update parameter accepts incoming Update object, but I don't think that's going to solve your problem, because a CallbackContext object does not have a getFile method either.您可以尝试颠倒 function 参数的顺序,以便update参数接受传入的Update object,但我认为这不会解决您的问题,因为CallbackContext getFile方法也没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM