{"title":"Cloudflare Workers Telegram Bot","url":"https://seanbehan.ca/posts/cf-workers-telegram-bot","description":"A tour of the rewritten cf-workers-telegram-bot API for running a Telegram bot on Cloudflare Workers.","author":"Sean Behan","published":"2024-05-15T07:11:06.679Z","updated":null,"draft":false,"tags":["cloudflare","typescript"],"readingMinutes":2,"image":null,"sections":[{"id":"project-setup","text":"Project Setup","level":2},{"id":"adding-your-bot-token","text":"Adding Your Bot Token","level":2},{"id":"writing-the-bot","text":"Writing the Bot","level":2},{"id":"deployment-and-webhook","text":"Deployment and Webhook","level":2},{"id":"starter-template","text":"Starter Template","level":2}],"content_format":"text/markdown","content_url":"https://seanbehan.ca/posts/cf-workers-telegram-bot.md","content":"This past week I spent rewriting\n\n[`cf-workers-telegram-bot`](https://github.com/codebam/cf-workers-telegram-bot).\n\nHere I'm going to show you the newly updated API that anyone can use to run a\n\nTelegram Bot on Cloudflare Workers or any other serverless platform.\n\n### Project Setup\n\nFirst you need to set up a new Cloudflare Workers project using wrangler.\n\n```shell\nnpm create cloudflare@latest\n```\n\nThis will prompt you for a project name first. Choose a name for your new\n\nproject or use the default.\n\nNext you'll be prompted for the type of application you want to deploy. You\n\nshould choose a \"Hello World\" Worker.\n\nAfter that you'll be asked a few more questions, like if you want to use\n\nTypeScript, and Git (optional, but recommended). You don't need to deploy just\n\nyet.\n\n### Adding Your Bot Token\n\nNext you can add the token you get from `@BotFather` on Telegram as a secret so\n\nyou don't have to keep it in the code.\n\n```shell\nnpx wrangler secret put TOKEN\n```\n\nOnce you've created your project and created your secret you'll want to install\n\nthe `cf-workers-telegram-bot` library to start coding.\n\n```shell\ncd your-new-project\nnpm i @codebam/cf-workers-telegram-bot\n```\n\nThis will give you access to my `TelegramBot` class for creating a Telegram Bot.\n\n### Writing the Bot\n\nTo get started you can open `src/index.ts` in your favorite editor such as\n\nvscode or vim. Then you can import like this.\n\n```javascript\nimport TelegramBot, { TelegramExecutionContext } from '@codebam/cf-workers-telegram-bot';\n```\n\nThen you can simply write your bot like this.\n\n```javascript\nexport default {\n  async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {\n    const bot = new TelegramBot(env.TOKEN);\n    await bot.on('start', async function (context: TelegramExecutionContext) {\n      switch (context.update_type) {\n\tcase 'message':\n\t  await context.reply('Hello from Cloudflare workers');\n\t  break;\n\n\tdefault:\n\t  break;\n      }\n    }).handle(request.clone());\n    return new Response('ok');\n  },\n};\n```\n\n### Deployment and Webhook\n\nNow you're ready to deploy.\n\n```shell\nnpx wrangler deploy\n```\n\nYou can set your webhook by going to the URL you've been given plus your token and `?command=set`.\n\nLike this. `https://telegram-bot.username.workers.dev/YOURTOKENHERE?command=set`\n\nNow when you send your bot a message on Telegram it should respond with `Hello from Cloudflare workers`.\n\n### Starter Template\n\nIf you've got this far and you don't want to code it all by yourself, you can\n\nfork my repository [here](https://github.com/codebam/cwtb-consumer) to get\n\nstarted with a working copy.\n"}