Seeking a cleaner design pattern for a Telegram bot

My telegram bot’s ConversationHandler is too cluttered. See the sample code below. I need a simpler and more manageable design pattern for clean code.

# New bot states
ALPHA, BETA, GAMMA = range(3)
chat_flow = ConversationHandler(
    entry_points=[CommandHandler('begin', launch_bot)],
    states={
        ALPHA: [CallbackQueryHandler(handle_alpha, pattern='^choice1$')],
        BETA: [MessageHandler(Filters.text, handle_beta)]
    },
    fallbacks=[CommandHandler('stop', terminate_bot)]
)

i tried modularized classes for each state; ripping out logic from conversationhandler helped alot. submodules made it easier to follow code flow and debug. thinking maybe a state machine could work well too, depending on how complex things get.

Refactoring the ConversationHandler improved overall code clarity in my projects. I separated the core functionalities into dedicated modules that encapsulated the state transitions and corresponding business logic. This approach allowed me to manage and test individual components more efficiently, reducing the complexity of the main bot script. A rough state machine pattern, implemented as a series of lightweight classes, provided the necessary abstraction. The key was to focus on decoupling event handling from the conversation logic, which ultimately led to better maintainability and scalability of the bot application.

In my experience simplifying a Telegram bot’s conversation flow, I moved device logic into smaller, dedicated modules and used a dispatcher approach instead of embedding all state transitions in the ConversationHandler. I created individual functions that encapsulated the behavior of each state and coordinated them using a simple state machine, which made debugging and testing isolated components easier. Moreover, separating the messaging logic from the state handling allowed me to maintain a cleaner overall design, minimizing dependencies and making it straightforward to expand functionality as the project evolved.