Telegram Bot
Deploy Telegram bot on Cloudflare Workers. Configure BotFather token, AI Gateway, webhook. Session persistence via Durable Objects.
Telegram Bot
Back to: Cloudflare Deploy
TL;DR (2 mins)
@BotFather → token → bun scripts/config.ts telegram → bun run deploy:telegram → webhook → "hi"!
Quick Steps
- @BotFather →
/newbot→ Copy token bun scripts/config.ts telegram(paste token)bun run deploy:telegram- Set webhook:
/setwebhook→https://YOUR_WORKER.workers.dev/webhook - Test: Message "hi" → Bot responds!
Quick Checklist
- Token from BotFather
-
bun scripts/config.ts telegram -
bun run deploy:telegram - Webhook set
- Test: "hi" responds
Full Guide
Telegram Bot Deployment
Back to: Deployment Overview
Deploy the Telegram bot as a serverless webhook on Cloudflare Workers with Durable Objects for session persistence.
Overview
The Telegram bot provides:
- Chat interface via Telegram
- AI agent powered by OpenRouter via Cloudflare AI Gateway
- Session persistence via Durable Objects (no external database needed)
- Built with Hono + Cloudflare Agents SDK
Prerequisites
- Cloudflare account with Workers enabled
- Telegram account for creating bot
- Cloudflare AI Gateway configured with OpenRouter
Step 1: Create Bot with BotFather
- Open Telegram and search for @BotFather
- Send
/newbotcommand - Enter a display name (e.g., "Duyetbot Agent")
- Enter a username ending in
bot(e.g.,duyetbot_agent_bot) - Save the HTTP API token - this is your
TELEGRAM_BOT_TOKEN
Configure Bot Commands (Optional)
Send to @BotFather:
Then paste:
Step 2: Create Cloudflare AI Gateway
- Go to Cloudflare Dashboard -> AI -> AI Gateway
- Click "Create Gateway"
- Name it (e.g.,
duyetbot) - Save this name asAI_GATEWAY_NAME - Configure the gateway:
- Add OpenRouter as a provider
- Set your OpenRouter API key in the gateway settings
- Enable caching (optional, reduces costs)
- Enable logging (recommended for debugging)
Step 3: Get Your Telegram User ID (Optional)
To restrict bot access to specific users:
- Send a message to @userinfobot
- It will reply with your user ID (e.g.,
123456789) - Use this for
TELEGRAM_ALLOWED_USERS(comma-separated for multiple users)
Step 4: Configure Environment
All environment variables are configured in a single .env.local file at the project root.
Edit .env.local with your values:
Environment Variable Groups
The .env.local file is organized by prefix:
AI_GATEWAY_*- Cloudflare AI Gateway settingsGITHUB_*- GitHub integrationTELEGRAM_*- Telegram bot settings
Step 5: Deploy to Cloudflare Workers
This command will:
- Build the Telegram bot package
- Deploy to Cloudflare Workers
- Set all secrets from
.env.localviawrangler secret put - Configure Telegram webhook automatically
Note the deployed URL:
Step 6: Test the Bot
- Open Telegram and find your bot by username
- Send
/start - Send a message like "Hello, what can you do?"
- The bot should respond with AI-generated content
Environment Variables
| Variable | Required | Description |
|---|---|---|
TELEGRAM_BOT_TOKEN | Yes | Bot token from @BotFather |
AI_GATEWAY_NAME | Yes | Cloudflare AI Gateway name |
AI_GATEWAY_API_KEY | No | API key for authenticated gateway access |
TELEGRAM_WEBHOOK_SECRET | No | Secret for webhook verification |
TELEGRAM_ALLOWED_USERS | No | Comma-separated Telegram user IDs (empty = all allowed) |
MODEL | No | Model name (default: x-ai/grok-4.1-fast) |
GITHUB_TOKEN | No | GitHub personal access token for github-mcp |
Monitoring & Logs
Troubleshooting
Webhook not receiving updates
- Check webhook status:
- Look at
last_error_messagein response - Verify URL is correct and accessible
- Re-run
bun run deploy:telegramto reconfigure webhook
401 Unauthorized errors
TELEGRAM_WEBHOOK_SECRETmust match the secret configured in webhook- Re-run
bun run deploy:telegramto update secrets and webhook
Bot not responding
- Check logs:
npx wrangler tail --name duyetbot-telegram - Verify
AI_GATEWAY_NAMEmatches your gateway name in Cloudflare - Verify
TELEGRAM_BOT_TOKENis correct - Check if user is in
TELEGRAM_ALLOWED_USERS(if configured)
AI Gateway errors
- Check your AI Gateway is created in Cloudflare Dashboard
- Verify
AI_GATEWAY_NAMEmatches exactly (case-sensitive) - Check OpenRouter API key is configured in AI Gateway settings
- Check the AI Gateway logs in Cloudflare Dashboard for details
401 No auth credentials found
This error from AI Gateway means your OpenRouter API key is not configured:
- Go to Cloudflare Dashboard -> AI -> AI Gateway
- Select your gateway and configure authentication
- Add your OpenRouter API key in the provider settings
Local Development
For local development with wrangler dev:
Values in .dev.vars:
Then run:
Note: Local development requires a tunnel (like ngrok) to receive webhooks.
Architecture
- Hono: Lightweight web framework for routing
- Cloudflare Agents SDK: Stateful agents with Durable Objects
- AI Gateway: Cloudflare's proxy for LLM providers with caching/logging
Updating the Bot
To update after code changes:
This will rebuild, redeploy, and reconfigure the webhook. Secrets are preserved.
Next Steps
- GitHub Bot Deployment - Deploy the GitHub bot
- Memory MCP Deployment - Deploy the memory server
- Deployment Overview - All components