Skip to content

Commit

Permalink
Add tutorial for Cloudflare Workers with Deno
Browse files Browse the repository at this point in the history
Co-authored-by: Ciki Momogi <cikimomogi@protonmail.com>
Co-authored-by: KnorpelSenf <shtrog@gmail.com>
  • Loading branch information
3 people authored and jebreuer committed Mar 30, 2023
1 parent fdd333c commit 120e7f0
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 2 deletions.
6 changes: 5 additions & 1 deletion site/docs/.vuepress/configs/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,13 @@ export const localeEn: LocaleConfig<DefaultThemeLocaleData> = {
link: "/hosting/supabase.html",
},
{
text: "Cloudflare Workers",
text: "Cloudflare Workers (Node)",
link: "/hosting/cloudflare-workers.html",
},
{
text: "Cloudflare Workers (Deno)",
link: "/hosting/cloudflare-workers-deno.html",
},
{
text: "Heroku",
link: "/hosting/heroku.html",
Expand Down
141 changes: 141 additions & 0 deletions site/docs/hosting/cloudflare-workers-deno.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# Hosting: Cloudflare Workers (Deno)

[Cloudflare Workers](https://workers.cloudflare.com/) is a public serverless computing platform that offers a convenient and simple solution for running small workloads at the [edge](https://en.wikipedia.org/wiki/Edge_computing).

This guide will take you through the process of hosting your Telegram bot on Cloudflare Workers.

:::tip Looking for the Node.js Version?
This tutorial explains how to deploy a Telegram bot to Cloudflare Workers using Deno.
If you're looking for the Node.js version, please check out [this tutorial](./cloudflare-workers.md) instead.
:::

## Prerequisites

To follow along, please make sure that you have a [Cloudflare account](https://dash.cloudflare.com/login) with your workers subdomain [configured](https://dash.cloudflare.com/?account=workers).

## Setting Things Up

Make sure you have [Deno](https://deno.land/) and [Denoflare](https://denoflare.dev/) installed.

Create a new directory, and create a new file `.denoflare` in that directory.
Put the following contents in the file:

> Note: The "$schema" key in the following JSON code specifies a fixed version in its URL ("v0.5.11").
> At the time of writing, this was the latest version available.
> You should update them to the [newest version](https://github.com/skymethod/denoflare/releases).
```json
{
"$schema": "https://raw.githubusercontent.com/skymethod/denoflare/v0.5.11/common/config.schema.json",
"scripts": {
"my-bot": {
"path": "bot.ts",
"localPort": 3030,
"bindings": {
"BOT_TOKEN": {
"value": "YOUR_BOT_TOKEN"
}
},
"workersDev": true
}
},
"profiles": {
"account1": {
"accountId": "YOUR_ACCOUNT_ID",
"apiToken": "YOUR_API_TOKEN"
}
}
}
```

Make sure to replace `YOUR_ACCOUNT_ID`, `YOUR_API_TOKEN`, and `YOUR_BOT_TOKEN` appropriately.
When creating your API token, you can choose the `Edit Cloudflare Workers` preset from the pre-configured permissions.

## Creating Your Bot

Create a new file named `bot.ts` and put the following contents in it:

```ts
import {
Bot,
webhookCallback,
} from "https://deno.land/x/grammy/mod.ts";
import { UserFromGetMe } from "https://deno.land/x/grammy/types.ts";

interface Environment {
BOT_TOKEN: string;
}

let botInfo: UserFromGetMe | undefined = undefined;

export default {
async fetch(request: Request, env: Environment) {
try {
const bot = new Bot(env.BOT_TOKEN, { botInfo });

if (botInfo === undefined) {
await bot.init();
botInfo = bot.botInfo;
}

bot.command("start", (ctx) => ctx.reply("Welcome! Up and running."));
bot.on("message", (ctx) => ctx.reply("Got another message!"));

const cb = webhookCallback(bot, "cloudflare-mod");

return await cb(request);
} catch (e) {
return new Response(e.message);
}
},
};
```

## Deploying Your Bot

It's as easy as running:

```sh
denoflare push my-bot
```

The output of the above command will provide you with the host the worker is running on.
Watch out for a line containing something like `<MY_BOT>.<MY_SUBDOMAIN>.workers.dev`.
That's the host where your bot is waiting to be called.

## Setting Your Webhook

We need to tell Telegram where to send updates to.
Open your browser and visit this URL:

```text
https://api.telegram.org/bot<BOT_TOKEN>/setWebhook?url=https://<MY_BOT>.<MY_SUBDOMAIN>.workers.dev/
```

Replace `<BOT_TOKEN>`, `<MY_BOT>`, and `<MY_SUBDOMAIN>` with your values.
If the setup is successful, you'll see a JSON response like this:

```json
{
"ok": true,
"result": true,
"description": "Webhook was set"
}
```

## 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:

```sh
denoflare serve my-bot
```

Once the development server has started, you can test your bot by sending sample updates to it using tools like `curl`, [Insomnia](https://insomnia.rest), or [Postman](https://postman.com).
Refer to [here](https://core.telegram.org/bots/webhooks#testing-your-bot-with-updates) for update examples and [here](https://core.telegram.org/bots/api#update) for more information on the update structure.
7 changes: 6 additions & 1 deletion site/docs/hosting/cloudflare-workers.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# Hosting: Cloudflare Workers
# Hosting: Cloudflare Workers (Node)

[Cloudflare Workers](https://workers.cloudflare.com/) is a public serverless computing platform that offers a convenient and simple solution for running JavaScript at the [edge](https://en.wikipedia.org/wiki/Edge_computing).
Having the ability to handle HTTP traffic and being based on the [Service Worker API](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API), building Telegram bots becomes a breeze.
In addition, you can even develop [Web Apps](https://core.telegram.org/bots/webapps) 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.

:::tip 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](./cloudflare-workers-deno.md) instead.
:::

## Prerequisites

To follow along, please make sure that you have a [Cloudflare account](https://dash.cloudflare.com/login) with your workers subdomain [configured](https://dash.cloudflare.com/?account=workers).
Expand Down

0 comments on commit 120e7f0

Please sign in to comment.