This repository provides an easy-to-use, fully documented template for building Telegram bots using Cloudflare Workers, written in TypeScript. It is designed to balance staying as close as possible to the Telegram Bot API while providing ease of use and effortless deployment of serverless Telegram bots on the Cloudflare platform.
- Install
wrangler
by runningnpm install wrangler
Note
If you need to install npm, download and install node.js from nodejs.org
- Go to a directory that you want your worker files to be in and run
npm create cloudflare@latest MY_WORKER_NAME
ReplaceMY_WORKER_NAME
with a name you like for your worker.
Note
If you are running it for the first time, it will open the browser to connect to your Cloudflare account, follow its instructions.
- For "What would you like to start with?" Choose
Template from a Github repo
- For "What's the url of git repo containing the template you'd like to use?" Enter
https://github.com/m-sarabi/cloudflare-telegram-bot/tree/main/ts
and press enter.
Tip
Alternatively, you can enter https://github.com/m-sarabi/cloudflare-telegram-bot
and then choose Typescript
- For "Do you want to use git for version control?" Choose if you want to use git for your worker or not.
- For "Do you want to deploy your application?" Choose if you want to deploy your worker right now or not.
Note
I suggest not to deploy it right now. You can deploy it later using wrangler deploy
You should see the message:
- go to @BotFather, and use the
/newbot
command to create a new bot. Follow the instructions until you get your API token. - Go to the
wrangler.toml
file in your project directory and add these lines:[vars] SECRET = "<SECRET>" TOKEN = "<API_TOKEN>"
- Replace
<SECRET>
with a secret token of random characters of your choice. It is useful to ensure that the request comes from a webhook set by you. 1-256 characters, only charactersA-Z
,a-z
,0-9
,_
and-
are allowed. - Replace
<API_TOKEN>
with the API token you got from @BotFather.
- Replace
- Run
npm run cf-typegen
in the root directory of your project to regenerate theworker-configuration.d.ts
file with the variables you just set.
Let’s write a simple bot that replies to an inline keyboard button.
Scenario: When a user sends the /start command, the bot displays a message with a button. Upon pressing the button, the bot removes it and sends a follow-up message.
Note
All update handler functions are in the src/Telegram/handlers
directory
- Handle
/start
Command: Modifysrc/Telegram/handlers/handleMessage.ts
as follows:import { tg } from '../lib/methods'; export async function handleMessage(message: tgTypes.Message) { const messageText = message.text; const chatId = message.chat.id; if (messageText === '/start') { await tg.sendMessage({ text: 'Welcome to my bot! Press the button to accept my rules!', chat_id: chatId, reply_markup: { inline_keyboard: [[{ text: 'I Accept', callback_data: 'accept_rules' }]] } }); } }
Explanation: This code sends a message with an inline keyboard button using the tg.sendMessage method.
- Handle Inline Keyboard Press: Modify
src/Telegram/handlers/handleCallbackQuery.ts
as follows:import { tg } from '../lib/methods'; export async function handleCallbackQuery(callbackQuery: tgTypes.CallbackQuery) { const data = callbackQuery.data; const messageId = callbackQuery.message?.message_id; const chatId = callbackQuery.message?.chat.id; if (messageId && chatId) { if (data === 'accept_rules') { await tg.editMessageReplyMarkup({ chat_id: chatId, message_id: messageId, reply_markup: undefined }); await tg.sendMessage({ chat_id: chatId, text: 'Thanks for accepting my rules.' }); } } }
Explanation: This code removes the inline button and sends a follow-up message using the tg.editMessageReplyMarkup method.
Run wrangler deploy
to deploy your worker.
To register your bot's webhook:
- Go to your Cloudflare dashboard and go to
workers-and-pages
- Next to our project name click
Visit
- In the URL append
/registerWebhook
, so it will be likehttps://my-project.my-username.workers.dev/registerWebhook
, then press enter.- If you see the message "Webhook registered." it means you have set it correctly.
You can then go to the bot you created on Telegram, click Start (or send /start command), and you should see the message with the button.