Cloudflare Worker that acts as a Telegram operator via Hono. The current implementation validates Telegram webhook requests, sends OpenAI-backed replies to a single allowed chat, stores schedules in Cloudflare D1, and runs reminders plus web monitors on a cron trigger.
Run commands from the repo root unless noted otherwise.
- Create a Telegram bot via @BotFather
- Create
.dev.vars(gitignored):TELEGRAM_BOT_TOKEN=<token from BotFather> TELEGRAM_WEBHOOK_SECRET=<random string with at least 32 characters> ALLOWED_CHAT_ID=<your Telegram user ID> OPENAI_API_KEY=<your OpenAI API key> - Install dependencies from repo root:
pnpm install - Apply local D1 migrations before testing schedule features:
pnpm --filter @repo/operator db:migrate:local
GET /healthreturns{ "status": "ok" }POST /webhook/telegramonly accepts requests from Telegram IP ranges- The webhook requires the
X-Telegram-Bot-Api-Secret-Tokenheader to match - The webhook body is limited to 64 KiB and validated against the Telegram update schema
- Messages from the allowed Telegram chat are forwarded to OpenAI
- The assistant can create, list, and delete scheduled reminders through tool calls
- Schedule creation and deletion require confirmation within two minutes via inline ✅/❌ buttons (typing
YESstill works as a fallback) - A cron trigger runs every minute and executes due reminders and web monitors from D1
- Monitor schedules scrape a URL, analyze the content with OpenAI, and notify only when the condition matches
Requires two terminals and cloudflared.
Terminal 1 - start the worker:
pnpm devTerminal 2 - start a tunnel:
cloudflared tunnel --url http://localhost:8787Then register the webhook with the tunnel URL:
pnpm --filter @repo/operator set-webhook <tunnel-url>
# e.g. pnpm --filter @repo/operator set-webhook https://xxx.trycloudflare.comSend a message to your bot on Telegram. The worker will send the message text to OpenAI and reply in Telegram with the model output. Schedule creation, deletion, and monitoring features require the local D1 migrations above.
Set secrets via Wrangler CLI:
pnpm --filter @repo/operator exec wrangler secret put TELEGRAM_BOT_TOKEN
pnpm --filter @repo/operator exec wrangler secret put TELEGRAM_WEBHOOK_SECRET
pnpm --filter @repo/operator exec wrangler secret put ALLOWED_CHAT_ID
pnpm --filter @repo/operator exec wrangler secret put OPENAI_API_KEYApply remote D1 migrations before or after deploy:
pnpm --filter @repo/operator db:migrate:remoteCreate .prod.vars (gitignored) with your production bot credentials, then deploy and register the webhook:
pnpm --filter @repo/operator deploy
pnpm --filter @repo/operator set-webhook \
https://switch-operator.<account>.workers.dev -- --prod| Command | Description |
|---|---|
pnpm dev |
Start local dev server |
pnpm --filter @repo/operator deploy |
Deploy to Cloudflare Workers |
pnpm --filter @repo/operator db:migrate:local |
Apply local D1 migrations |
pnpm --filter @repo/operator db:migrate:remote |
Apply remote D1 migrations |
pnpm typecheck |
Run TypeScript type checking |
pnpm lint |
Run ESLint |
pnpm test |
Run tests |
pnpm --filter @repo/operator set-webhook <url> [-- --prod] |
Register Telegram webhook URL |
| Method | Path | Description |
|---|---|---|
| GET | /health |
Health check |
| POST | /webhook/telegram |
Telegram webhook receiver |