Hosting: Vercel Serverless Functions
This tutorial will guide you on how to deploy your bot to Vercel by using Vercel Serverless Functions, assuming that you already have a Vercel account.
Project Structure
The only prerequisite to get started with Vercel Serverless Functions is to move your code to the api
directory as shown below. You can also see Vercel’s documentation for more on this.
.
├── node_modules/
├── build/
├── api/
│ └── bot.ts
├── package.json
├── package-lock.json
└── tsconfig.json
If you are using TypeScript, you might as well want to install @vercel
as a dev dependency, but it is not mandatory for following this guide.
Configuring Vercel
The next step is to create a vercel
file at the top level of your project. For our example structure, its content would be:
{
"functions": {
"api/bot.ts": {
"memory": 1024,
"maxDuration": 10
}
}
}
2
3
4
5
6
7
8
If you want to use Vercel’s free subscription, your
memory
andmax
configurations might look like above to not bypass its limits.Duration
If you want to learn more about the vercel
configuration file, see its documentation.
Configuring TypeScript
In our tsconfig
, we have to specify our output directory as build
, and our root directory as api
. This is important since we will specify them in Vercel’s deploy options.
{
"compilerOptions": {
"target": "ES2019",
"module": "commonjs",
"rootDir": "./api",
"moduleResolution": "node",
"resolveJsonModule": true,
"outDir": "./build",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
The Main File
Regardless of using TypeScript or JavaScript, we should have a source file through which our bot runs. It should look roughly like this:
import { Bot, webhookCallback } from "grammy";
const token = process.env.BOT_TOKEN;
if (!token) throw new Error("BOT_TOKEN is unset");
const bot = new Bot(token);
export default webhookCallback(bot, "https");
2
3
4
5
6
7
8
Vercel Edge Functions provides limited support for grammY
You can still use the core grammY package and a number of plugins, but others may be incompatible due to Node.js-only dependencies that might not be supported by Vercel’s Edge Runtime.
Currently, we don’t have a comprehensive list of compatible plugins, so you need to test it by yourself.
Add this line to snippet above if you want to switch to Edge Functions:
export const config = {
runtime: "edge",
};
2
3
In Vercel’s Dashboard
Assuming that you already have a Vercel account your GitHub is connected to, add a new project and select your bot’s repository. In Build & Development Settings:
- Output directory:
build
- Install command:
npm install
Don’t forget to add the secrets such as your bot token as environment variables in the settings. Once you have done that, you can deploy it!
Setting the Webhook
The last step is to connect your Vercel app with Telegram. Modify the below URL to your credentials and visit it from your browser:
https://api.telegram.org/bot<BOT_TOKEN>/setWebhook?url=<HOST_URL>
The HOST
is a little tricky, because you need to use your Vercel app domain following with the route to the bot code, for example https://
. Where bot
is referring to your bot
or bot
file.
You should then see a response like this:
{
"ok": true,
"result": true,
"description": "Webhook was set"
}
2
3
4
5
Congratulations! Your bot should now be up and running.