logo
bg
return Blog

March 8, 2024

Create and Deploy a Telegram Bot with JavaScript

Yuhang XiaYuhang Xia

This tutorial will guide you through developing a simple Telegram bot from scratch, utilizing the GrammY framework with JavaScript, and deploying it on Zeabur.

What is the GrammY Framework?

> GrammY - The Telegram Bot Framework

Developing Telegram bots with JavaScript offers several options, such as node-telegram-bot-api or GrammY. The first one is Telegram's Node.js SDK, while GrammY is an emerging, mature framework that comes with many pre-packaged functionalities, simplifying our development process.

What is Zeabur?

Zeabur is a deployment service platform that makes it convenient for developers to deploy their services with one click, without worrying about server infrastructure.

To keep a Telegram bot running continuously, we have two options:

  • Running it locally, which requires the computer to stay connected and on
  • Deploying it to the cloud, completing development and deploying with one click to Zeabur for continuous and stable bot service

Creating a Bot and Obtaining a Token

The process to create a bot on Telegram is straightforward. Open Telegram, search for BotFather. In the chat, follow the guide and send the `/newbot` command to create a bot, then copy its token.

Project Initialization

Prerequisites

  • Node.js environment installed on your computer
  • A package manager like npm/pnpm installed

Creating a New Project

First, create a new folder to store your bot code. Then, open a terminal in that directory and enter the command `npm init` to generate a default package.json. Next, install the grammy dependency with `npm install grammy`.

Create a bot.js file:

const { Bot } = require("grammy");

const bot = new Bot("Your token here");

// Listening for messages
bot.on("message:text", (ctx) => ctx.reply("Received: " + ctx.message.text));

// Starting the bot
bot.start();

At this point, you can run your bot with the following command:

```bash
node bot.js
```

node bot.js

After running the bot, you can send messages to the bot you just created on Telegram, and it will reply with "Received: + your message content."

Deploying to Zeabur

After development is complete, you can deploy your bot to the cloud. Zeabur is recommended for easy one-click deployment.

First, visit Zeabur's official website and register a new account using GitHub. After entering the dashboard, click to create a new project and select your preferred region; here, we choose a data center in Hong Kong.

After creating the project, click to create a new service on the project page, choose to deploy from a GitHub repository, select the bot repository you just created, and click import. Zeabur will automatically start deploying your bot. Wait about a minute for the deployment to finish, and then you can go back and enjoy chatting with your bot.

Deploying Telegram Bot Templates

If you just want to have a bot that allows you to use ChatGPT or Gemini on Telegram, you can directly deploy a few bots from Zeabur's template market, such as Gemini-Telegram-Bot or ChatGPT-Telegram-Bot. After clicking deploy, Zeabur will automatically create a corresponding repository for you. You only need to enter your bot token and API key during deployment to start using it.