A small Telegram command bot written in JavaScript. It replies to /start, /services, /resources, and /contact with fixed messages. The links in index.js are examples; replace them with your own before using the bot.
Telegram update
|-- HTTPS webhook --> index.js (Worker fetch handler) --|
`-- getUpdates --> polling.js --> pollTelegram() -------|
handleUpdate()
|
Telegram sendMessage API
index.jsholds the command replies andhandleUpdate(), which reads a text message, identifies its first command, and sends the matching reply. Unknown commands and non-text updates are ignored. A command addressed to a bot in a group, such as/start@MyBot, is accepted.- The default export in
index.jshandles HTTP requests in a Cloudflare Worker.GET /andGET /healthreturn a health response.POST /telegram/webhookandPOST /webhookaccept updates after checking theX-Telegram-Bot-Api-Secret-Tokenheader againstWEBHOOK_SECRET. pollTelegram()inindex.jsprovides an alternative local runtime. It checks that no webhook is active, then repeatedly calls TelegramgetUpdateswith a 30-second timeout. Its offset advances through the received update IDs so subsequent requests skip those updates.polling.jsloads.env, lets process environment variables override its values, and startspollTelegram(). Polling errors are logged and set a nonzero process exit code.
Use either webhook delivery or long polling for a bot token. The polling runner refuses to start while Telegram reports an active webhook.
- Node.js 20.12 or newer for local polling.
- A Telegram bot token from BotFather.
- For webhook deployment, a public HTTPS Worker endpoint and a webhook secret.
- Install dependencies with
npm install. - Create a
.envfile in the project root containingTELEGRAM_BOT_TOKEN=your_bot_token. The file is ignored by Git. You can also setTELEGRAM_BOT_TOKENin the process environment; it takes precedence over.env. The current runner still requires the.envfile to exist. - Ensure no webhook is configured for that bot token.
- Run
npm run polland send/startto the bot. Press Ctrl+C to stop.
Deploy index.js as a Cloudflare Worker entry point and provide TELEGRAM_BOT_TOKEN and WEBHOOK_SECRET as Worker bindings. Configure Telegram's webhook URL to point to /telegram/webhook (or /webhook) on the deployed Worker, with the same secret token. The repository does not include a Worker deployment configuration or a webhook setup script, so those steps must be supplied for your deployment.
Edit the messages object at the top of index.js. Each key is a lowercase Telegram command and each value is the text sent back to the chat. The bot currently has no storage, conversation state, or user data recording.