Skip to content

Receive updates via Bot API when Telethon MTProto is blocked - #5

Open
kinton wants to merge 1 commit into
mainfrom
cursor/botapi-polling-fallback-d56e
Open

Receive updates via Bot API when Telethon MTProto is blocked#5
kinton wants to merge 1 commit into
mainfrom
cursor/botapi-polling-fallback-d56e

Conversation

@kinton

@kinton kinton commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Why

This Cloud Agent environment can reach api.telegram.org over HTTPS, but raw MTProto to Telegram DCs is reset (IncompleteReadError / HTTP 404 on port 443). The bot used to connect Telethon at import time, so python main.py crashed before it could serve anything.

What

  • Connect Telethon lazily (try_start_telethon()), skip it when TELEGRAM_FORCE_BOTAPI=1 or when the handshake fails
  • Keep rec/update worker threads alive without MTProto so they do not look “dead” and spam the creator
  • Long-poll Bot API getUpdates as a receive fallback and dispatch into the existing route workers (commands, callbacks, inline, Stars pre-checkout)
  • Sending still uses pyTelegramBotAPI, so /start, menus, and search work without Telethon. Large file upload via Telethon still needs MTProto

How to run here

export TELEGRAM_FORCE_BOTAPI=1
export TELEGRAM_BOT_NAME=YourInstaFeedDevelopingBot
./venv/bin/python -u main.py

Then message @YourInstaFeedDevelopingBot.

Tests

  • python app/routes/test_botapi_receiver.py — command/callback routing helpers (ok)
  • Live main.py with TELEGRAM_FORCE_BOTAPI=1: process stays up, logs Receiving via Bot API long polling
  • Bot API getMeYourInstaFeedDevelopingBot, webhook empty
  • Sent a ping and the welcome screen to the creator chat (message ids 30621 / 30622)
Open in Web Open in Cursor 

Cloud Agent and similar networks allow HTTPS Bot API but reset raw
MTProto, so importing bot_telethon used to crash before the bot could
start. Connect Telethon lazily, keep send/update workers alive without
it, and long-poll getUpdates as a receive fallback.

Co-authored-by: Kovalenko K. <kintonkk@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants