Building a simple telegram bot with nodejs and grammy

Jul 18, 2023 - 15:05
Jul 18, 2023 - 15:07
 0  37
Building a simple telegram bot with nodejs and grammy

Popular messaging software Telegram has added audio and video chat as well as file sharing options for its users. In particular, the Telegram Bot API stands out because it enables developers to create their own chatbots that can perform a variety of activities and provide automated services. 

In this tutorial, we'll use Node.js and the Grammy library to create a basic Telegram bot. To execute JavaScript code outside of a web browser, Node.js provides a robust JavaScript engine. The Grammy project, on the other hand, is a Node.js package that simplifies interaction with the Telegram Bot API.

This tutorial will teach you all you need to know to create your own Telegram bot, including how to utilize the BotFather to create a new bot and how to use Node.js and Grammy to add fundamental bot functionality. 

Whether you're interested in creating a bot for personal use or for a group, this tutorial will teach you how to do it. Let's go straight into creating your own Telegram bot using Node.js and Grammy!

  1. Make Your Own Telegram Bot

You may create your own Telegram app by following the instructions given below. 

  • The "BotFather" bot may be found by searching for it on Telegram.

  • To create a new bot, start a conversation with BotFather and type "/newbot" into the command prompt.

  • Launching BotFather for the first time will ask you to create a username and password for your bot. The next step is for BotFather to provide a token that may be used with your bot. Be cautious to safeguard this token, since it is required for authentication of bots.

  1. Create a brand-new Node from scratch

Create a new Node.js project to house the bot's code. Navigate to the file folder in which you'd want to save your work. To start a new project in Node.js using the default configuration, just run the npm init -y script. This creates a package.json file that may be used to store the project's configuration and prerequisites.

After that, we install the Grammy library with the command npm run install grammy. Grammy is a Node.js utility that facilitates communication with the Telegram Bot API. As a result, node js development company have an easier time managing bot instructions, messages, and API interactions.

  1. Write the code for the robot

Create a new file with the name bot.js in your preferred code editor. After obtaining a bot token from BotFather, you must create a new bot by importing the Bot class from the Grammy library.

To process each bot command, use the bot.command() method. The "/start" switch is now implemented in the example code. The robot will respond to this order by greeting the individual warmly.

To process incoming text messages, use the bot.on('message:text') method. In the piece of code, we modify the user's message by prepending "You said:" before sending it back to them.

Connecting to the Telegram Bot API takes place in the bot.start() function. Initiating this lengthy voting process is how the bot obtains fresh data and messages from its consumers.

The required syntax is const Bot = 'grammy';

Const bot = new Bot("YOUR_BOT_TOKEN"); // Make a new bot instance.

Bot.command('start,', (ctx => ")); // Process the /start command Welcome to your Telegram bot! '); ctx.reply('Hello!'); );

Bot.on('message:text', (ctx => "")); // Manage incoming text messages You said: $message>, therefore I'll respond with: ctx.reply('You said: $message>'); );

 

To activate the robot, use bot.start();

Bot-ify It 

  1. Lauch the bot 

Launch the Telegram bot by opening the console and navigating to the project's save location. Then, go ahead and replace it with node bot.js. The Node.js script for the bot is executed at this point. A successful installation of the bot should be indicated by a message.

  1. Do Bot Test 

Use the Telegram mobile app and your computer's login details to test your bot. Try out a few different conversational tacks with the bot. Use the predetermined rules for the bot's responses.

The fundamentals of creating a Telegram bot are covered briefly in this article. Your bot might benefit from a wide variety of additional capabilities and lines of thought, including:

  • Users may have their queries answered by the bot as they write them into the chat window itself using inline inquiries. To process inline requests, Grammy provides the bot.on('inline_query') function. It is possible to get data from external APIs or databases and send it back as responses to internal queries.

  • In addition to text messages, users may also share and receive media files, documents, and more. In response to user communications, media may be sent using ctx.replyWithPhoto() or ctx.replyWithDocument().

  • Telegram users may make their selections utilizing in-conversation live tools and buttons. Grammy provides mechanisms for handling such capabilities, such as the ctx.replyWithKeyboard() and ctx.replyWithInlineKeyboard() methods. A specialized keyboard or closely spaced buttons can process the user's input.

Conclusion 

You have learned the basics of using Node.js and the Grammy library to create a basic Telegram bot. We began a fresh bot, created a Node.js project, installed the Grammy library, and programmed in some fundamental bot features. It was also discussed that the bot be modified to allow for inline inquiries, the transmission of media, and navigation via the keyboard and inline buttons, among other things.

Making a Telegram bot may provide users with several new avenues for interaction. Creating complex bots with a wide range of features is now possible with the help of the Telegram Bot API and tools like Grammy.

When you're ready to develop a bot with more advanced capabilities and concepts, you may consult both the official Telegram Bot API documentation and Grammy's guide. Making effective tools that are tailored to certain use cases and providing value to clients requires expertise and testing. 

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow