paint-brush
Building a Simple Telegram Bot With Node.js and GrammY by@mikemkhlv
3,961 reads
3,961 reads

Building a Simple Telegram Bot With Node.js and GrammY

by Mikhail MikhaylovFebruary 3rd, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

This tutorial will guide you through the process of creating a simple Telegram bot that greets users. You will be using the JavaScript language and Node.js to write the code and the GrammY framework.
featured image - Building a Simple Telegram Bot With Node.js and GrammY
Mikhail Mikhaylov HackerNoon profile picture

This tutorial will guide you through the process of creating a simple Telegram bot that greets users. You will be using the JavaScript language and Node.js to write the code and the GrammY framework.

Why GrammY?

There are several alternatives for the development of Telegram bots in JavaScript like node-telegram-bot-api or . The first one is more just a library than a full-fledged framework. The second one is old and mature. But GrammY is relatively new, modern, and rapidly gaining popularity.


Furthermore, it has excellent documentation and a supportive community that can assist. And last but not least there is not a single article about GrammY on the HackerNoon, so let’s fix it!

Meet the Hello Bot

We will develop a simple bot that can greet people in different languages. It’s a great example to learn how to process text messages and commands.

Creating a bot and getting token

First, you need to create a bot account. To do this you need to use the . As the description says


BotFather is the one bot to rule them all. Use it to create new bot accounts and manage your existing bots.


Open the bot, send /newbot command follow the instructions, create a bot and get a token.

Project set up

As a prerequisite, you have to have Node.js installed. The easiest way is to download it from the  and install it.


The set up is usual for Node.js projects. Сreate an empty grammy-hello-bot folder open it in Terminal and run npm init -y command. Then install the GrammY package with npm install grammy. And create an empty index.js file. Eventually, you will get the following project structure.


Eventual project structure

The source code

Put the following code into the index.js file.

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

const bot = new Bot(''); // <-- put your bot token here (//t.me/BotFather)

bot.command('help', (ctx) => {
    ctx.reply(`
    The bot could greet people in different languages.
    The list of supported greetings:
    - hello - English
    - salut - French
    - hola - Spanish
    `)
});

bot.hears('salut', (ctx) => ctx.reply('salut'));
bot.hears('hello', (ctx) => ctx.reply('hello'));
bot.hears('hola', (ctx) => ctx.reply('hola'));

bot.on('message:text', (ctx) => ctx.reply(`Greeting "${ctx.update.message.text}" is not supported.`))

bot.start();

Let’s go line by line. First, we are requiring the GrammY module and creating an instance of our bot. It’s important to specify the bot token you got from the BotFather here.

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

const bot = new Bot(''); // <-- put your bot token here (//t.me/BotFather)

Now register a middleware to process /help command and to reply with instructions. Commands are just text messages that start with /, like /start, /help etc. You can come up with your own commands when you are developing a bot. And middlewares are just functions that process messages.

bot.command('help', (ctx) => {
    ctx.reply(`
    The bot could greet people in different languages.
    The list of supported greetings:
    - hello - English
    - salut - French
    - hola - Spanish
    `)
});

The code above basically says: “If you got a message with /help command, just call this function and stop processing the message”. Meaning if one middleware processed a message, the other will not get a chance to do it. Middlewares are called one by one in the order they were registered.


Then, using hears method we are registering middlewares that process different greetings and reply with it to the user.

bot.hears('salut', (ctx) => ctx.reply('salut'));
bot.hears('hello', (ctx) => ctx.reply('hello'));
bot.hears('hola', (ctx) => ctx.reply('hola'));
And then a middleware that processes every text message that wasn’t processed by middlewares above. This middleware is necessary for the case when a user sends a greeting that is not supported by the bot.
bot.on('message:text', (ctx) => ctx.reply(`Greeting "${ctx.update.message.text}" is not supported.`))
And then we are starting our bot. After that, it will begin to listen for messages from Telegram using a long-polling mechanism.
bot.start();

Time to test it. Run the bot with node index.js command. Leave it running. Open Telegram and send something to the bot.


Sending messages to the bot

Links


바카라사이트 바카라사이트 온라인바카라