Sometimes my Python Telegram bot does not react when users send photos or PDFs. The message handler is skipped. Here is a sample code for reference:
import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ConversationHandler
STEP = 1
def start_session(update, context):
update.message.reply_text('Use /send to continue.')
return ConversationHandler.END
def send_command(update, context):
update.message.reply_text('Please send your file.')
return STEP
def handle_file(update, context):
print('Message processed...')
update.message.reply_text('File received!')
return ConversationHandler.END
def abort_process(update, context):
print('Operation aborted')
return start_session(update, context)
def run_bot():
updater = Updater('YOUR_API_TOKEN')
dispatcher = updater.dispatcher
conv_handler = ConversationHandler(
entry_points=[CommandHandler('send', send_command), MessageHandler(Filters.text, start_session)],
states={
STEP: [CommandHandler('cancel', abort_process), MessageHandler(Filters.all, handle_file)]
},
fallbacks=[CommandHandler('cancel', abort_process)],
conversation_timeout=60
)
dispatcher.add_handler(conv_handler)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
run_bot()