Skip to content

Instantly share code, notes, and snippets.

@terenty-rezman
Created January 11, 2022 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terenty-rezman/3d2891896c207507a7c00fedd0509a94 to your computer and use it in GitHub Desktop.
Save terenty-rezman/3d2891896c207507a7c00fedd0509a94 to your computer and use it in GitHub Desktop.
python-telegram-bot dialog interrupt workaround / converstaionhandler
"""
workaround for dialog interruption for python-telegram-bot
see the problem of parallel dialogs:
https://stackoverflow.com/questions/64146229/python-telegram-bot-disallow-nested-conversations
https://github.com/python-telegram-bot/python-telegram-bot/issues/1640
"""
from telegram import ReplyKeyboardMarkup
from telegram.ext import (
Updater,
CommandHandler,
MessageHandler,
Filters,
ConversationHandler,
)
(
A1, A2,
B1, B2
) = range(4)
def cancel(update, context):
update.message.reply_text('cancel dialog')
return ConversationHandler.END
def start(update, context):
reply_keyboard = [['A', 'B']]
markup_key = ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
update.message.reply_text('select dialog', reply_markup=markup_key,)
return None
def enter_a(update, context):
update.message.reply_text('a1')
return A1
def a1(update, context):
update.message.reply_text('a2')
return A2
def a2(update, context):
update.message.reply_text('exit a')
context.user_data['in_conversation'] = False
return ConversationHandler.END
def enter_b(update, context):
update.message.reply_text('b1')
return B1
def b1(update, context):
update.message.reply_text('b2')
return B2
def b2(update, context):
update.message.reply_text('exit b')
return ConversationHandler.END
if __name__ == '__main__':
# specify your bot token here
updater = Updater("1586839780:AAEte-6_VVVGbqh7c7wmrcxkUZX0R2TRiJc")
# dispatcher to attach handlers
dispatcher = updater.dispatcher
# dialog A: press 'a' multiple time to go through the dialog
dispatcher.add_handler(ConversationHandler(
entry_points=[MessageHandler(Filters.regex('^[aA]$'), enter_a)],
states= {
A1: [MessageHandler(Filters.regex('^[aA]$'), a1)],
A2: [MessageHandler(Filters.regex('^[aA]$'), a2)]
},
fallbacks=[MessageHandler(Filters.regex('^[bB]$'), cancel)] # NOTE: pressing 'b' cancels current dialog and starts dialog 'B'
), 1) # NOTE: add handler to group 1
# dialog B: press 'b' multiple time to go through the dialog
dispatcher.add_handler(ConversationHandler(
entry_points=[MessageHandler(Filters.regex('^[bB]$'), enter_b)],
states= {
B1: [MessageHandler(Filters.regex('^[bB]$'), b1)],
B2: [MessageHandler(Filters.regex('^[bB]$'), b2)]
},
fallbacks=[MessageHandler(Filters.regex('^[aA]$'), cancel)] # NOTE: pressing 'a' cancels current dialog and starts dialog 'A'
), 2) # NOTE: add handler to group 2
# shows main menu
dispatcher.add_handler(CommandHandler('start', start), 3)
# start bot
updater.start_polling()
updater.idle()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment