Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

Telegram Bot Record

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.

Architecture

Telegram update
   |-- HTTPS webhook --> index.js (Worker fetch handler) --|
   `-- getUpdates --> polling.js --> pollTelegram() -------|
                                                    handleUpdate()
                                                          |
                                               Telegram sendMessage API
  • index.js holds the command replies and handleUpdate(), 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.js handles HTTP requests in a Cloudflare Worker. GET / and GET /health return a health response. POST /telegram/webhook and POST /webhook accept updates after checking the X-Telegram-Bot-Api-Secret-Token header against WEBHOOK_SECRET.
  • pollTelegram() in index.js provides an alternative local runtime. It checks that no webhook is active, then repeatedly calls Telegram getUpdates with a 30-second timeout. Its offset advances through the received update IDs so subsequent requests skip those updates.
  • polling.js loads .env, lets process environment variables override its values, and starts pollTelegram(). 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.

Requirements

  • 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.

Run with long polling

  1. Install dependencies with npm install.
  2. Create a .env file in the project root containing TELEGRAM_BOT_TOKEN=your_bot_token. The file is ignored by Git. You can also set TELEGRAM_BOT_TOKEN in the process environment; it takes precedence over .env. The current runner still requires the .env file to exist.
  3. Ensure no webhook is configured for that bot token.
  4. Run npm run poll and send /start to the bot. Press Ctrl+C to stop.

Run as a webhook

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.

Customize replies

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.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages