DEV Community

Cover image for Lets build a Telegram bot using NodeJS
Muhammad ABir
Muhammad ABir

Posted on

Lets build a Telegram bot using NodeJS

To create a Telegram bot using Node.js, you will need to install the node-telegram-bot-api package. This package provides an easy-to-use interface for interacting with the Telegram Bot API.

Here is an example of a basic Telegram bot that can respond to messages:

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

// replace with your own token
const token = 'YOUR_TOKEN';

const bot = new TelegramBot(token, {polling: true});

bot.on('message', (msg) => {
  const chatId = msg.chat.id;
  bot.sendMessage(chatId, 'Hello World!');
});
Enter fullscreen mode Exit fullscreen mode

In this example, the bot will listen for incoming messages and respond with "Hello World!" when a message is received. The polling: true option tells the bot to use long polling to receive updates from Telegram.

You will need to replace YOUR_TOKEN with the token you received from the Bot Father after creating your bot.

You can also use commands to make the bot do specific things. For example, you can make the bot respond to the command '/start' like this:

bot.onText(/\/start/, (msg) => {
  const chatId = msg.chat.id;
  bot.sendMessage(chatId, 'Welcome! How can I help you today?');
});
Enter fullscreen mode Exit fullscreen mode

This is just a simple example, you can make your bot more complex by using more advanced features of the Telegram Bot API, such as sending photos, audio, etc.

Checkout the github repo

Top comments (0)