Files
talia_bot/bot/debug.md

5.7 KiB

Debugging Report: Telegram Bot Conversational Flows

Problem Description

The primary issue is that the Telegram bot is not engaging in conversational flows and is failing to respond to button presses, effectively ignoring "triggers" sent via inline keyboard buttons.

Initially, the bot exhibited Python runtime errors:

  1. IndentationError: unexpected indent in main.py, specifically around the ConversationHandler definition.
  2. SyntaxError: unterminated string literal in main.py due to an incomplete pattern in a CallbackQueryHandler.
  3. AttributeError: 'Application' object has no attribute 'add_h_handler' due to a typo in main.py.

After addressing these syntax and indentation issues, the bot launched successfully without crashing. However, the core problem of unresponsive buttons and non-functional conversational flows persisted, with no relevant application logs appearing when buttons were pressed.

Initial Diagnosis & Fixes

  1. Indentation and Syntax Errors:

    • The main.py file was found to have severely malformed code within the main() function, including duplicated sections and an incorrectly constructed ConversationHandler.
    • The entire main() function and the if __name__ == "__main__": block were rewritten to correct these structural and syntactical errors, ensuring proper Python code execution. This included fixing the IndentationError and the SyntaxError in the CallbackQueryHandler pattern.
    • A typo (add_h_handler instead of add_handler) causing an AttributeError was corrected.
  2. Lack of Application Logs:

    • To diagnose the unresponsive buttons, diagnostic print statements were added to the button_dispatcher in main.py and propose_activity_start in modules/equipo.py.
    • A generic TypeHandler with a catch_all_handler was added to main.py to log all incoming updates from Telegram.
    • Despite these additions, no diagnostic output appeared when buttons were pressed, indicating that the handlers were not being triggered.

Deep Dive into Button Handling

  • Flows and Triggers: Examination of data/flows/admin_create_nfc_tag.json confirmed that flows are triggered by specific callback_data (e.g., start_create_tag).
  • Button Definitions: Review of modules/onboarding.py confirmed that buttons were correctly configured with callback_data values like view_pending, start_create_tag, and propose_activity.
  • Handler Registration: The order and definition of handlers in main.py were reviewed:
    • A ConversationHandler (for propose_activity) with a specific CallbackQueryHandler pattern (^propose_activity$).
    • A generic CallbackQueryHandler(button_dispatcher) to catch other button presses.
    • The order was deemed logically correct for dispatching.

Isolation Attempts

To rule out interference from the main application's complexity, two simplified bot scripts were created and tested:

  1. debug_main.py: A minimal bot that loaded the TELEGRAM_BOT_TOKEN and registered a simple /start command and a CallbackQueryHandler. This script failed to respond to button presses.
  2. simplest_bot.py: An even more stripped-down, self-contained bot with the bot token hardcoded, designed only to respond to /start and a single "Test Me" button press. This script also failed to trigger its CallbackQueryHandler.

Root Cause Identification

The consistent failure across all test cases (original bot, debug_main.py, simplest_bot.py), despite correct code logic, led to an investigation of the python-telegram-bot library version.

  • pip show python-telegram-bot revealed that version 22.5 was installed.
  • Research indicated that python-telegram-bot versions 22.x are pre-release and contain significant breaking changes, including the removal of functionality deprecated in v20.x. This incompatibility was the likely cause of the handlers not being triggered.

Solution

The python-telegram-bot library was downgraded to a stable version:

  • Command executed: pip install --force-reinstall "python-telegram-bot<22"
  • Verified installed version: 21.11.1

Current Status and Next Steps

Even after successfully downgrading the library, the bot still does not respond to button presses, and the diagnostic print statements are not being hit. This is highly unusual given the simplicity of the simplest_bot.py script.

This suggests that the updates from Telegram are still not reaching the application's handlers. The deleteWebhook command was executed and confirmed no active webhook exists.

Remaining Suspicions:

  1. Conflicting Bot Instance: There might be another instance of this bot (using the same token) running somewhere else (e.g., on a different server, or another terminal on the same machine) that is consuming the updates before the current local process can receive them.
  2. Bot Token Issue: In rare cases, a bot token itself can become "stuck" or problematic on Telegram's side, preventing updates from being reliably delivered.

Next Steps:

  • User Action Required: The user must ensure with absolute certainty that no other instances of the bot (using the token 8065880723:AAHOYnTe0PlP6pkjBirK8REtDDlZOrhc-qw) are currently running on any other machine or process.
  • If no other instances are found: As a last resort, the user should revoke the current bot token via BotFather in Telegram and generate a completely new token. Then, update config.py (and simplest_bot.py if testing that again) with the new token.
  • Clean up diagnostic code: Once the core issue is resolved, all temporary diagnostic print statements and files (debug_main.py, simplest_bot.py) will be removed.