Hosting: Deno Deploy
This guide tells you about the ways you can host your grammY bots on Deno Deploy.
Please note that this guide is only for Deno users, and you need to have a Git
Deno Deploy is ideal for most simple bots, and you should note that not all Deno features are available for apps running on Deno Deploy. For example, there is no file system on Deno Deploy. It’s just like the other many serverless platforms, but dedicated for Deno apps.
The result of this tutorial can be seen in our example bots repository.
Preparing Your Code
Remember that you need to run your bot on webhooks, so you should use
webhook
and not callCallback bot
in your code..start()
- Make sure that you have a file which exports your
Bot
object, so that you can import it later to run it. - Create a file named
mod
or.ts mod
, or actually any name you like (but you should be remembering and using this as the main file to deploy), with the following content:.js
import { serve } from "https://deno.land/std@0.160.0/http/server.ts";
import { webhookCallback } from "https://deno.land/x/grammy@v1.11.2/mod.ts";
// You might modify this to the correct way to import your `Bot` object.
import bot from "./bot.ts";
const handleUpdate = webhookCallback(bot, "std/http");
serve(async (req) => {
if (req.method === "POST") {
const url = new URL(req.url);
if (url.pathname.slice(1) === bot.token) {
try {
return await handleUpdate(req);
} catch (err) {
console.error(err);
}
}
}
return new Response();
});
We advise you to have your handler on some secret path rather than the root (/
). Here, we are using the bot token (/<bot token>
).
Deploying
Method 1: With GitHub
This is the recommended method, and the easiest one to go with. The main advantage of following this method is that Deno Deploy will watch for changes in your repository which includes your bot code, and it will deploy new versions automatically.
- Create a repository on GitHub, it can be either private or public.
- Push your code.
It is recommended that you have a single stable branch and you do your testing stuff in other branches, so that you won’t get some unexpected things happen.
- Visit your Deno Deploy dashboard.
- Click on “New Project”, and go to the “Deploy from GitHub repository” section.
- Install the GitHub app on your account or organization, and choose your repository.
- Select the branch you want to deploy, and then choose your
mod
file to be deployed..ts
deployctl
Method 2: With This is a method for more advanced users. It allows you to deploy the project via the command line or Github Actions.
- Visit your Deno Deploy dashboard.
- Click “New Project”, and then “Empty Project”.
- Install
deployctl
. - Create an access token.
- Run the following command:
deployctl deploy --project <project> ./mod.ts --prod --token <token>
- To set up Github Actions, refer to this.
Method 3: With URL
All you need for following this method to deploy your grammY bot, is a public URL to your
mod
file..ts
- Create a new project on Deno Deploy.
- Click “Deploy URL”.
- Input the public URL to your
mod
file, and click “Deploy”..ts
Note
After getting your app running, you should configure your bot’s webhook settings to point to your app. To do that, send a request to
https://api.telegram.org/bot<token>/setWebhook?url=<url>
replacing <token>
with your bot’s token, and <url>
with the full URL of your app along with the path to the webhook handler.