Telegram AI Bot
A complete, working Telegram bot that answers every message through your own gateway (same provider chain, same automatic failover as the website chatbot). Two ready options: a serverless webhook on Cloudflare Pages (recommended) and a local polling script that runs on any computer. No provider keys ever touch Telegram or your local machine.
Telegram POSTs every incoming message to /api/telegram/<SECRET> — a Cloudflare Pages Function
that asks your own /api/v1/chat core (with failover across all configured providers) and sends the answer back
via sendMessage. Long answers are split into Telegram-sized chunks; a per-chat rate limit blocks loops.
1. Create the bot (2 minutes)
- Open Telegram and message @BotFather.
- Send
/newbot, choose a display name and a username (must end in bot). - BotFather replies with a token like
7216334:AAH8k…— that is yourTELEGRAM_BOT_TOKEN.
2. Add the env vars to Cloudflare Pages
Dashboard → Workers & Pages → aigateway → Settings → Variables and Secrets (add to Production and Preview):
TELEGRAM_BOT_TOKEN=7216334:AAH8k-your-real-token-from-botfather
TELEGRAM_WEBHOOK_SECRET=pick-any-long-random-string-here
# optional:
TELEGRAM_BOT_USERNAME=mygatewaybot
TELEGRAM_MAX_TOKENS=1200
The complete importable file lives on the Env Setup page — Telegram vars are already included there.
3. Register the webhook (one command)
Replace the two placeholders and run once — the bot goes live immediately:
curl "https://api.telegram.org/bot<TELEGRAM_BOT_TOKEN>/setWebhook" \
--data-urlencode "url=https://aigatewaybd.pages.dev/api/telegram/<TELEGRAM_WEBHOOK_SECRET>" \
--data-urlencode "allowed_updates=["message"]"
Check it any time: open /api/telegram (status JSON) or run getWebhookInfo the same way.
4. Test it
Open your bot in Telegram, press Start, then send anything — Bangla or English:
bot → আকাশে তারা জ্বলে রাতে,
নদীর জলে ছায়া ভাসে হাতে…
you → Explain CORS in one paragraph
bot → CORS lets a browser trust another origin…
5. Alternative: local polling bot (no Cloudflare)
The repo ships a zero-dependency Node script at examples/telegram-bot-poller.mjs — it long-polls Telegram and answers via your gateway's public /ask endpoint. Run it on any machine with Node 18+:
export TELEGRAM_BOT_TOKEN="7216334:AAH8k-your-token"
export GATEWAY_BASE="https://aigatewaybd.pages.dev"
node examples/telegram-bot-poller.mjs
6. All Telegram env vars
| Variable | Required | Purpose |
|---|---|---|
TELEGRAM_BOT_TOKEN | Yes | Bot token from @BotFather. |
TELEGRAM_WEBHOOK_SECRET | Recommended | Secret path segment — updates to other paths get 403. |
TELEGRAM_BOT_USERNAME | — | Shown in the /start welcome text. |
TELEGRAM_MAX_TOKENS | — | Reply length cap (default 1200). |
TELEGRAM_SYSTEM_PROMPT | — | Override the bot's whole personality/system prompt. |
- No reply at all → run
getWebhookInfo; if last_error_message mentions 403, the secret path in the webhook URL doesn't match the env var. - "TELEGRAM_BOT_TOKEN is not configured" → add the variable in Cloudflare and redeploy.
- Error replies from the bot → the providers themselves are failing; check API Status. The bot message shows the exact error code.