How to build a Telegram bot with Node.Js

How to build a Telegram bot with Node.Js

In this article, I'm going to explain how to set up a Telegram bot. To illustrate the procedure, we'll take the example of a bot tasked with welcoming

Telegram bots are programmable entities that can automate various tasks within groups or discussions. In this article, we'll show you how to create a Telegram bot using Node.js to welcome new members joining a group. We'll explain each step, from creating the bot to setting up the welcome response.

Step 1: Create a Bot on Telegram

Pour créer un bot sur Telegram, vous devez suivre ces étapes :

  1. Open the Telegram application and search for the bot "BotFather".

  2. Start a conversation with BotFather.

  3. Use the `/newbot` command to create a new bot.

  4. Give your bot a name and a unique username ending in "bot".

  5. BotFather will give you a unique access token for your bot. Make a note of this token, as you'll need it later.

If you follow these steps carefully, you'll end up with this:

Step 2: Setting up the Node.js project

Make sure you have Node.js installed on your system. If not, download and install it from the official site.

Next, create a new directory for your project, access it via your terminal and initialize a Node.js project with the following command :

npm init -y

Next, install the libraries needed to interact with the Telegram API using node-telegram-bot-api :

npm i node-telegram-bot-api

Step 3: Writing code

Create a JavaScript file, for example, index.js, and paste this into your index.js :

const TelegramBot = require('node-telegram-bot-api');

// Replace 'YOUR_TOKEN' with the token you obtained from BotFather.
const token = 'YOUR_TOKEN';

// Initialize the bot with your token.
const bot = new TelegramBot(token, { polling: true });

// Listen for 'new_chat_members' events.
bot.on('new_chat_members', (msg) => {
    const chatId = msg.chat.id;
    const newMembers = msg.new_chat_members;

    newMembers.forEach((member) => {
        bot.sendMessage(chatId, `Welcome, ${member.first_name}!`);
    });
});

// Listen for text messages.
bot.on('text', (msg) => {
    const chatId = msg.chat.id;
    const message = msg.text;

    if (message === '/start') {
        bot.sendMessage(chatId, 'Hello, I\'m Wendy, what can I do for you?.');
    }
});

This code creates a bot that responds to new members joining a group by welcoming them. It also responds to the `/start` message with a welcome message.

Step 4: Launching the Bot

To run your bot, use the following command in your terminal, making sure you're in your project directory:

node index.js

Your bot is now active and will work in the groups you've added it to.

Conclusion

Creating a Telegram bot with Node.js is a relatively straightforward process. You can further customize your bot by adding more features, such as automated responses to specific commands. Feel free to explore the Telegram API and consult the node-telegram-bot-api documentation to find out more about what you can do with your bot. Have fun automating your Telegram chats!

Link to source code.