Skip to content

Feature/telegram polling#192

Open
Coder666 wants to merge 2 commits intostephengpope:mainfrom
Coder666:feature/telegram-polling
Open

Feature/telegram polling#192
Coder666 wants to merge 2 commits intostephengpope:mainfrom
Coder666:feature/telegram-polling

Conversation

@Coder666
Copy link
Copy Markdown

@Coder666 Coder666 commented Mar 9, 2026

feat(telegram): add long-polling mode as alternative to webhooks

The current Telegram integration requires a public HTTPS endpoint (via ngrok, VPS, etc.) for webhook delivery. This PR adds long polling as an alternative — the bot polls Telegram's getUpdates API, so no public URL is needed.

Config

TELEGRAM_MODE=polling    # or "webhook" (default — no behaviour change)

One env var. No new dependencies. No separate service. Runs in the existing Node.js process.

How it works

Mode How Requires public URL?
Polling Bot calls getUpdates with 25s long-poll timeout No — works behind firewalls, NAT, localhost
Webhook (existing) Telegram POSTs to your HTTPS endpoint Yes

Both modes route through the same pipeline: TelegramAdapter.receive() → processChannelMessage() → AI → sendResponse(). The AI layer is mode-agnostic.

Changes

New file:

  • lib/channels/telegram-polling.js — getUpdates loop with 25s long-poll, auto-clears webhook on start, constructs fake Request objects for the existing adapter

Modified files:

  • api/index.js — maybeStartPolling() lazy-starts on first request when TELEGRAM_MODE=polling
  • lib/channels/telegram.js — skip webhook secret validation in polling mode (updates come from our own getUpdates call, not an external source)
  • setup/setup-telegram.mjs — new Step 2 offers "Polling" vs "Webhook" mode; skips APP_URL and webhook registration for polling; clears any existing webhook when switching to polling
  • docs/CONFIGURATION.md — add TELEGRAM_MODE env var
  • docs/CHAT_INTEGRATIONS.md — document both delivery modes with comparison table
  • package.json — add test/telegram-polling.test.js to test script

Tests

test/telegram-polling.test.js — 9 new tests:

  • Webhook secret rejection in webhook mode (missing / mismatched)
  • Webhook secret bypass in polling mode
  • Case-insensitive TELEGRAM_MODE check
  • Chat ID authorization still enforced in polling mode
  • Fake request construction and header access
  • stopPolling() lifecycle safety

All 213 tests pass (204 existing + 9 new).

Coder666 added 2 commits March 9, 2026 21:06
When TELEGRAM_MODE=polling, the bot polls Telegram's getUpdates API
instead of requiring a public HTTPS endpoint. Messages are routed
through the same TelegramAdapter → processChannelMessage pipeline.

- Add lib/channels/telegram-polling.js: getUpdates loop with 25s
  long-poll timeout, auto-deletes webhook on start
- Modify api/index.js: lazy-start polling on first request
- Modify lib/channels/telegram.js: skip webhook secret validation
  in polling mode (updates come from our own getUpdates call)
- Modify setup/setup-telegram.mjs: offer webhook vs polling mode,
  skip APP_URL and webhook steps when polling is selected

No new dependencies. Polling runs in the existing Node.js process.
Config: TELEGRAM_MODE=polling (default: webhook, no behaviour change)
- Add test/telegram-polling.test.js (9 tests):
  - Webhook secret rejection in webhook mode
  - Webhook secret bypass in polling mode
  - Case-insensitive TELEGRAM_MODE check
  - Chat ID authorization in polling mode
  - Fake request construction
  - stopPolling lifecycle safety
- Update CONFIGURATION.md: add TELEGRAM_MODE env var
- Update CHAT_INTEGRATIONS.md: document polling vs webhook modes
- Add test file to npm test script in package.json

All 213 tests pass.
@Coder666 Coder666 requested a review from stephengpope as a code owner March 9, 2026 21:09
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.

1 participant