Hosting: Zeabur (Node.js)
Zeabur is a platform that allows you to deploy your full-stack applications with ease. It supports various programming languages and frameworks, including Node.js and grammY.
This tutorial will guide you how to deploy your grammY bots with Node.js to Zeabur.
Looking for the Deno Version?
This tutorial explains how to deploy a Telegram bot to Zeabur using Node.js. If you’re looking for the Deno version, please check out this tutorial instead.
Prerequisites
To follow along, you need to have Git
Method 1: Create a New Project from Scratch
Initialize your project and install some necessary dependencies:
# Initialize the project.
mkdir grammy-bot
cd grammy-bot
npm init -y
# Install main dependencies.
npm install grammy
# Install development dependencies.
npm install -D typescript ts-node @types/node
# Initialize TypeScript.
npx tsc --init
2
3
4
5
6
7
8
9
10
11
12
13
Then, cd
into src
, and create a file named bot
. It is where you will write your bot’s code.
Now, you can start writing your bot’s code in src
.
import { Bot } from "grammy";
const token = process.env.TELEGRAM_BOT_TOKEN;
if (!token) throw new Error("TELEGRAM_BOT_TOKEN is unset");
const bot = new Bot(token);
bot.on("message:text", async (ctx) => {
console.log("Message: ", ctx.message.text);
const response = "Hello, I'm a bot!";
await ctx.reply(response);
});
bot.start();
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Note: Get your bot token with @Bot
Father on Telegram, and set is as an environment variableTELEGRAM
in Zeabur._BOT _TOKEN You can check out this tutorial for setting environment variables in Zeabur.
Now your project’s root directory should now look like this:
.
├── node_modules/
├── src/
│ └── bot.ts
├── package.json
├── package-lock.json
└── tsconfig.json
And then we have to add start
scripts to our package
. Our package
should now be similar to this:
{
"name": "telegram-bot-starter",
"version": "1.0.0",
"description": "Telegram Bot Starter with TypeScript and grammY",
"scripts": {
"start": "ts-node src/bot.ts"
},
"author": "MichaelYuhe",
"license": "MIT",
"dependencies": {
"grammy": "^1.21.1"
},
"devDependencies": {
"@types/node": "^20.14.5",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Now, you can run your bot locally by running:
npm run start
Method 2: Use Zeabur’s Template
Zeabur has already provided a template for you to use. You can find it here.
You can just use the template and start writing your bot’s code.
Deploying
Method 1: Deploy From GitHub in Zeabur’s Dashboard
- Create a repository on GitHub, it can be public or private and push your code to it.
- Go to Zeabur dashboard.
- Click on the
New Project
button, and click on theDeploy New Service
button, chooseGit
as the source and select your repository.Hub - Go to
Variables
tab to add your environment variables likeTELEGRAM
._BOT _TOKEN - Your service will be deployed automatically.
Method 2: Deploy With Zeabur CLI
cd
into your project directory and run the following command:
npx @zeabur/cli deploy
Follow the instructions to select a region to deploy, and your bot will be deployed automatically.