Serverless Telegram bot on Cloudflare
- 1. Difficulties on Telegram side.
- 2. Difficulties on Cloudflare side.
- 3. Simplest template to start Telegram bot development.
- 4. Use Wrangler.
- 5. Telegram Mini Apps with @tma.js and Typescript.
- 6. Development with Telegraf
- 7. Related page
1. Difficulties on Telegram side.
Telegram bots has a lot of various features and API (look to right panel, this is common list of method and fields description)
Problem is local debugging. For that case there is local Telegram stub - https://core.telegram.org/bots/api#using-a-local-bot-api-server, this is most interesting case to create reach application, this is installation procedure https://tdlib.github.io/telegram-bot-api/build.html?os=Windows. Also we can use Docker with deployed API Docker image for Telegram Bot API. We can use Docker Desktop in this case, for example, if we use Windows.
This allow developing reach bot locally and setup local Webhook, this is main difficulties for create any bot or any payment gateways.
Payment gateway also always need to setup Webhook, see for example this my payment gateways: Liqpay Payment gateway, Шлюзы к платежным системам интернет-денег., Payment Gateway до системи Paymaster на VB.NET, My payment gateway for Inplat.ru
This is difficulties from Telegram side - a lot of API methods and parameters, a lot of very special opportunities like inline bot, channel, or games and need to setup external IP for Webhook.
Or maybe there is possible to use VPN instead external IP, but I don't know how to do this. There are a couple of huge Telegram community, try to ask question on this, this or this.
2. Difficulties on Cloudflare side.
On Cloudflare side we have another difficulties. We need to pack HTML-file to JS, but this is not a main problem. Usually if we develop simple Cloudflare Webworker application with Wrangler we can use Browser debugger
or even VS code debugger.
For VS Code Debugger I use this lunch file
However, Webhook imposes the limitation to development, we can not develop bot locally, only on Cloudflare and we can use only console.log with Wrangler tail. (Wrangler Tail looks to wrangler.toml config file to detect what exactly service working log you want to see.)
3. Simplest template to start Telegram bot development.
So, simplest way to start your first Telegram bot with Cloudflare will start from that template https://github.com/cvzi/telegram-bot-cloudflare and you need to deploy this bot directly on Cloudflare web editor without Wrangler. Than manually add Environment variables to Bot and call RegisterWebhook with you Worker URL
Of course, this is very basic template, but It working. It you need something debugging information, you can send it to Telegram Chat
4. Use Wrangler.
Or, on another case, install wrangler, Authorize it, prepare wrangler.toml config and than use Wrangler tail (see above). Wrangler use 3 files with setup info, two of them is creating during login (in my Windows case in folder Wrangler config C:\Users\Razen\AppData\Roaming\xdg.config\.wrangler and E:\Angular\CloudflareWorkerTest\node_modules\.cache\wrangler\).
And third file we need to create in local project folder, this is usual scheme of Wrangler.toml file and this is full documentation about it's parameters - https://developers.cloudflare.com/workers/wrangler/configuration/
name = "justrelax10-telegram-bot" main = "./bot.js" account_id = "879d7uisfdkljfhueoii762387623862" compatibility_date = "2023-03-04" [vars] PORT=3000 BOT_TOKEN = "6797298786:AoeueihfuerhilV3j-cnnPGyQ_-Xijushdfu BOT_SECRET = "wl2iuerhjiikfhiusd28989fsndtIlMt"
Most interesting feature of Wrangler that it looks as Wrangler has embedded Webpack functionality. For me, no need to use Webpack with new Wrangler. Look to this test, I have add flag to CF location object - lets compare source code and result of Wrangler processing
Also new Wrangler has many other interesting opportunity.
5. Telegram Mini Apps with @tma.js and Typescript.
There are cool type of internal Telegram apps Telegram Mini Apps, in this case we need to start on BotFather with /newapps (instead /newbot). I recommend as start point this template https://github.com/Telegram-Mini-Apps
And this template I have modified nothing, only adding Google font, a couple string of description and return two lines on bot with reference to Html5 Game and Mini apps.
And this is result https://t.me/justrelax10bot/testminiapps:
And this full @tma.js documentation - https://docs.telegram-mini-apps.com/packages/tma-js-sdk/components
6. Development with Telegraf.
More interesting and sophisticated way is starting from this template Telegraf Cloudflare Telegram Bot Boilerplate, but I'm sorry, I have no time to describe this way.
7. Related page:
- (2024) Google Cloud and Cloudflare
- (2023) CloudflareWorker and Supabase
- (2022) JS, Css
- (2022) Typescript, Webpack
- (2022) Angular, RxJs, Firebase, MongoDb
- (2022) Node, NestJs, Electron, Pwa, Telegram
- (2022) React, Redux, GraphQL, NextJs
- (2022) Angular/Typescript, JS books
<SITEMAP> <MVC> <ASP> <NET> <DATA> <KIOSK> <FLEX> <SQL> <NOTES> <LINUX> <MONO> <FREEWARE> <DOCS> <ENG> <CHAT ME> <ABOUT ME> < THANKS ME> |