Host your Telegram bot on Cloudflare workers
- Install wrangler and login with your Cloudflare account.
$ npm i @cloudflare/wrangler -g - Install ngrok for local port forwarding.
- Generate your project and install the dependencies.
$ wrangler generate my-project https://github.com/zayabighead/cfworker-telegrambot-template --type=webpack $ npm i - Rename
bot.config.example.jstobot.config.jsand setup your bot configuration.// bot.config.js module.exports = { token: '', //get your bot token from @botfather webhookPath: '/wefwefwefe', // a random string } - Rename
wrangler.example.tomltowrangler.tomland setup your wrangler configuration.
The main idea is to set up a local dev port and use ngrok to forward the port after setting the ngrok temporary url as the bot's webhook.
- Run
npm run ngrokto start ngrok local port forwrading. - Run
npm run webhookto set the webhook for the bot. - Run
npm run devto startwrangler dev.
- Run
npm run webhookto set the webhook for the bot. - Run
npm run publishto publish to a Cloudflare worker.
- Some Node.js built-in modules won't be available, you can set webpack
nodeoption to use some of them.- available modules: https://github.com/webpack/node-libs-browser/blob/master/README.md
- webpack configuration: https://v4.webpack.js.org/configuration/node/
- Other runtime limitations for Cloudflare workers: https://developers.cloudflare.com/workers/runtime-apis/web-standards
- cfworker-middware-telegraf: https://github.com/Tsuk1ko/cfworker-middware-telegraf
- Cloudflare workers doc: https://developers.cloudflare.com/workers/
- wrangler: https://developers.cloudflare.com/workers/cli-wrangler
- ngrok: https://dashboard.ngrok.com/get-started/tutorials
- telegraf: https://telegraf.js.org/v3#/