Search code examples
pythontelegram-botpython-telegram-bot

How to make telegram bot reply with a photo?


I have this telegram bot, and i want it to reply to a specific message with a specific photo, to be something like this

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()

but I got this error

Photo_invalid_dimensions

How can I make it work?


Solution

  • According to the sendPhoto docs here:

    • The photo must be at most 10 MB in size.
    • The photo's width and height must not exceed 10000 in total.
    • Width and height ratio must be at most 20.

    Based on the error you receive, your photo's width/height doesn't comply to the second rule. You can use sendDocument method which is reply_document instead of reply_photo.