Hosting: Cloudflare Workers (Node.js)
Cloudflare Workers is a public serverless computing platform that offers a convenient and simple solution for running JavaScript at the edge. Having the ability to handle HTTP traffic and being based on the Service Worker API, building Telegram bots becomes a breeze. In addition, you can even develop Web Apps at the edge, all for free within certain quotas.
This guide will take you through the process of hosting your Telegram bots on Cloudflare Workers.
Looking for the Deno Version?
This tutorial explains how to deploy a Telegram bot to Cloudflare Workers using Node.js. If you’re looking for the Deno version, please check out this tutorial instead.
Prerequisites
To follow along, please make sure that you have a Cloudflare account with your workers subdomain configured.
Setting Things Up
First, create a new project:
npx wrangler generate my-bot
You can change my
to whatever you want. This will be the name of your bot and the project directory.
After running the above command, follow the instructions you see to initialize the project. There, you can choose between JavaScript or TypeScript.
When the project is initialized, cd
into my
or whatever directory you initialized your project in. Depending on how you initialized the project, you should see a file structure similar to the following:
.
├── node_modules
├── package.json
├── package-lock.json
├── src
│ ├── index.js
│ └── index.test.js
└── wrangler.toml
Next, install grammy
, and other packages you might need:
npm install grammy
Creating Your Bot
Edit src
or src
, and write this code inside:
// Note that we're importing from 'grammy/web', not 'grammy'.
import { Bot, webhookCallback } from "grammy/web";
// The following line of code assumes that you have configured the secrets BOT_TOKEN and BOT_INFO.
// See https://developers.cloudflare.com/workers/platform/environment-variables/#secrets-on-deployed-workers.
// The BOT_INFO is obtained from `bot.api.getMe()`.
const bot = new Bot(BOT_TOKEN, { botInfo: BOT_INFO });
bot.command("start", async (ctx) => {
await ctx.reply("Hello, world!");
});
addEventListener("fetch", webhookCallback(bot, "cloudflare"));
2
3
4
5
6
7
8
9
10
11
12
13
The above example bot replies “Hello, world!” when it receives /start
.
Deploying Your Bot
Before deploying, we need to edit wrangler
:
account_id = 'your account_id' # Get this from Cloudflare's dashboard.
name = 'my-bot' # Your bot's name, which will appear in the webhook URL, for example: https://my-bot.my-subdomain.workers.dev
main = "src/index.js" # The entry file of the worker.
compatibility_date = "2023-01-16"
2
3
4
You can then deploy using the following command:
npm run deploy
Setting Your Webhook
We need to tell Telegram where to send updates to. Open your browser and visit this URL:
https://api.telegram.org/bot<BOT_TOKEN>/setWebhook?url=https://<MY_BOT>.<MY_SUBDOMAIN>.workers.dev/
Replace <BOT
, <MY
, and <MY
with your values. If the setup is successful, you’ll see a JSON response like this:
{
"ok": true,
"result": true,
"description": "Webhook was set"
}
2
3
4
5
Testing Your Bot
Open your Telegram app, and start your bot. If it responds, it means you’re good to go!
Debugging Your Bot
For testing and debugging purposes, you can run a local or remote development server before deploying your bot to production. Simply run the following command:
npm run start
Once the development server has started, you can test your bot by sending sample updates to it using tools like curl
, Insomnia, or Postman. Refer to here for update examples and here for more information on the update structure.