Functional code sample — step by step

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.

Webhook: serverless, free Polling: zero dependency Replies in Bangla & English
How the webhook works

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)

  1. Open Telegram and message @BotFather.
  2. Send /newbot, choose a display name and a username (must end in bot).
  3. BotFather replies with a token like 7216334:AAH8k… — that is your TELEGRAM_BOT_TOKEN.

2. Add the env vars to Cloudflare Pages

Dashboard → Workers & Pages → aigateway → Settings → Variables and Secrets (add to Production and Preview):

env
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:

bash
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:

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+:

bash
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

VariableRequiredPurpose
TELEGRAM_BOT_TOKENYesBot token from @BotFather.
TELEGRAM_WEBHOOK_SECRETRecommendedSecret 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.
Troubleshooting
  • 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.