如何让电报机器人用照片回复?

How to make telegram bot reply with a photo?

我有这个电报机器人,我希望它用特定的照片回复特定的消息,就像这样

from telegram.ext import Updater, CommandHandler, MessageHandler, Filters

def text(update, context):
    text_received = update.message.text
    if text_received == "12345":
        update.message.reply_photo(open('df.png', 'r'))
    else:
        update.message.reply_text(f'Did you say "{text_received}"?')
def main():
    TOKEN = "TOKEN"

    updater = Updater(TOKEN, use_context=True)
    dispatcher = updater.dispatcher

    dispatcher.add_handler(CommandHandler("start", start))
    dispatcher.add_handler(CommandHandler("help", help))

    dispatcher.add_handler(MessageHandler(Filters.text, text))

    dispatcher.add_error_handler(error)

    updater.start_polling()

    updater.idle()

if __name__ == '__main__':
    main()

但是我遇到了这个错误

Photo_invalid_dimensions

我怎样才能让它发挥作用?

根据 sendPhoto 文档 here:

  • 照片的大小不得超过 10 MB。
  • 照片的宽高合计不得超过10000。
  • 宽高比不得超过 20。

根据您收到的错误消息,您的照片 width/height 不符合第二条规则。您可以使用 sendDocument 方法,即 reply_document 而不是 reply_photo.