Create a Telegram Bot with Node.js

Create a Telegram Bot with Node.js
Photo by Rubaitul Azad / Unsplash

A Telegram bot is a program designed to interact with users on the Telegram messaging platform. It is essentially an automated script or application that can receive and process messages, respond to user inputs, and perform various tasks within the Telegram environment. Telegram bots can be created for a wide range of purposes, including customer support, content delivery, automation, and entertainment.

Creating a Telegram bot with Node.js involves several steps. In this tutorial, we'll go through the process of setting up a basic Telegram bot with Node.js, implementing basic commands, and creating a menu.

Prerequisites:

  • Node.js installed on your machine.
  • A Telegram account.

Step 1: Set up a new Telegram bot

  1. Open Telegram and search for the "BotFather" bot.
  2. Start a chat with BotFather and use the /newbot command to create a new bot.
  3. Follow the instructions to set a name and username for your bot.
  4. Once created, BotFather will provide you with a token. Save this token; you'll need it later.

Step 2: Initialize a Node.js project

  1. Open your terminal and create a new folder for your project.
mkdir my-telegram-bot
cd my-telegram-bot
  1. Initialize a new Node.js project.
npm init -y
  1. Install the node-telegram-bot-api package.
npm install node-telegram-bot-api

Step 3: Create your bot script

Create a file named index.js and open it in your favorite code editor.

// Import necessary modules
const TelegramBot = require('node-telegram-bot-api');

// Replace 'YOUR_BOT_TOKEN' with the token you received from BotFather
const token = 'YOUR_BOT_TOKEN';
const bot = new TelegramBot(token, { polling: true });

// Command: /start
bot.onText(/\/start/, (msg) => {
    const chatId = msg.chat.id;
    bot.sendMessage(chatId, 'Welcome to your Telegram Bot! Type /menu to see the menu.');
});

// Command: /menu
bot.onText(/\/menu/, (msg) => {
    const chatId = msg.chat.id;
    const menuOptions = {
        reply_markup: {
            keyboard: [['/hello', '/goodbye']],
            resize_keyboard: true,
            one_time_keyboard: true,
        },
    };

    bot.sendMessage(chatId, 'Choose an option:', menuOptions);
});

// Command: /hello
bot.onText(/\/hello/, (msg) => {
    const chatId = msg.chat.id;
    bot.sendMessage(chatId, 'Hello! 👋');
});

// Command: /goodbye
bot.onText(/\/goodbye/, (msg) => {
    const chatId = msg.chat.id;
    bot.sendMessage(chatId, 'Goodbye! 👋');
});

Step 4: Run your bot

Run your bot script using the following command in your terminal:

node index.js

Step 5: Test your bot

  1. Open Telegram and search for your bot using its username.
  2. Start a chat with your bot and use the /start command to trigger the welcome message.
  3. Use the /menu command to display the menu.
  4. Try out the /hello and /goodbye commands to see the responses.

Congratulations! You've created a basic Telegram bot with Node.js, implemented basic commands, and created a menu. Feel free to expand and customize your bot based on your needs.








Open-source Apps

9,500+

Medical Apps

500+

Lists

450+

Dev. Resources

900+

Read more