Skip to content

Conversation

@TrezorHannes
Copy link
Owner

PR: Fix Telegram Callback Buttons Not Working

Summary of Changes

  • Fixed issue where clicking Approve/Reject buttons in Telegram did nothing
  • Auto-approval after 5 minutes worked, but manual button clicks were ignored

Root Cause

The bot.polling(none_stop=True) thread was silently dying on network timeouts or exceptions. When this happened:

  • The main scheduler continued running (auto-approval worked ✅)
  • But Telegram updates were never received (buttons broken ❌)
  • No errors logged because thread died silently

Detailed Changes

magma_sale_process.py

Polling fix:

  • Replace bot.polling(none_stop=True) with bot.infinity_polling()
  • Add run_telegram_polling() wrapper with automatic restart on failure
  • Log errors and send Telegram notification when poller restarts
  • Set thread as daemon for clean shutdown
  • Configure timeouts: 60s connection, 30s long_polling

Minor cleanup:

  • Translate Portuguese comments to English in calculate_transaction_size()

Testing Strategy

  • Unit Tests: 12/12 passed ✅
  • Syntax Check: python3 -m py_compile
  • Manual Verification: Restart service and test button clicks
  • Monitoring: Watch for "Telegram polling restart" logs/notifications

Commits

  1. fix(magma): replace bot.polling() with robust infinity_polling wrapper
  2. chore(magma): translate Portuguese comments to English

Checklist

  • Code follows project conventions
  • All tests passing locally (12/12)
  • Syntax verified
  • Comments in English

The Telegram polling thread would silently crash due to network timeouts
or other exceptions, causing callback buttons (Approve/Reject) to stop
working while the scheduled auto-approval continued functioning normally.

Changes:
- Replace bot.polling(none_stop=True) with bot.infinity_polling()
- Add run_telegram_polling() wrapper with automatic restart on failure
- Log and notify when polling restarts to aid debugging
- Set thread as daemon for clean shutdown
- Configure appropriate timeout values (60s connection, 30s long_polling)

This ensures the polling thread recovers from transient failures instead
of dying silently.
Translate inline comments in calculate_transaction_size() for consistency.
@TrezorHannes TrezorHannes self-assigned this Jan 14, 2026
@TrezorHannes TrezorHannes added the bug Something isn't working label Jan 14, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @TrezorHannes, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the stability and reliability of the Telegram bot's interaction capabilities. By addressing a critical issue where the polling thread could silently die, leading to unresponsive callback buttons, the changes ensure that the bot consistently processes user input. The new robust polling mechanism, complete with automatic restarts and error notifications, guarantees continuous operation and a much improved user experience without requiring manual intervention.

Highlights

  • Telegram Polling Stability: Replaced the less robust bot.polling() with bot.infinity_polling() within a custom wrapper function to prevent silent thread termination and ensure continuous processing of Telegram updates, thereby fixing unresponsive callback buttons.
  • Automatic Poller Restart and Error Handling: Implemented an automatic restart mechanism for the Telegram poller, which logs errors, sends Telegram notifications upon restart, and introduces timeouts (60s connection, 30s long-polling) to enhance resilience against network issues or exceptions.
  • Code Readability Improvement: Translated Portuguese comments in the calculate_transaction_size() function to English, improving code clarity and maintainability for a broader audience.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively addresses the silent failure of the Telegram polling thread by replacing bot.polling with the more robust bot.infinity_polling within a restart wrapper. This is a great improvement for the stability of the bot. The translation of comments also improves code clarity. I have a few suggestions to further enhance the robustness and maintainability of the new implementation.

1. Exponential backoff for polling restarts:
   - Add TELEGRAM_POLL_* constants for configurable backoff
   - Delay increases 10s -> 20s -> 40s -> ... up to 5 min cap
   - Resets on successful polling

2. Extract magic numbers to named constants:
   - P2WPKH_INPUT_VBYTES = 57.5
   - P2WPKH_OUTPUT_VBYTES = 43
   - TRANSACTION_OVERHEAD_VBYTES = 10.5

3. Move run_telegram_polling() to module level:
   - Better modularity and testability
   - No longer nested inside if __name__ == '__main__'

Tests: 12/12 passing
@TrezorHannes TrezorHannes merged commit ca9754a into main Jan 14, 2026
3 checks passed
@TrezorHannes TrezorHannes deleted the fix/telegram-polling-stability branch January 14, 2026 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants