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

时间:2021-05-24 09:12:01

标签: python telegram-bot python-telegram-bot

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

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

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

1 个答案:

答案 0 :(得分:1)

根据 sendPhoto 文档 here

  • 照片大小不得超过 10 MB。
  • 照片的宽度和高度的总和不得超过 10000。
  • 宽高比不得超过 20。

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