A Comprehensive Guide to node telegram bot api for Efficient Bot Development

The node-telegram-bot-api is a powerful and popular library used to interface with the Telegram Bot API, enabling developers to easily create bots for Telegram. This library supports various types of messages and commands, making it a versatile choice for bot development.

Getting Started with node-telegram-bot-api

To get started, you need to install the node-telegram-bot-api package:

 $ npm install node-telegram-bot-api 

Here’s a simple example of how to create a basic bot:

 const TelegramBot = require('node-telegram-bot-api'); const token = 'YOUR_TELEGRAM_BOT_TOKEN'; const bot = new TelegramBot(token, {polling: true});
bot.on('message', (msg) => {
  const chatId = msg.chat.id;
  bot.sendMessage(chatId, 'Hello world');
}); 

Useful APIs & Features

1. Sending Messages

Sending a text message to a user is straightforward:

 bot.sendMessage(chatId, 'Your message here'); 

2. Sending Photos

You can also send photos easily:

 bot.sendPhoto(chatId, 'path/to/photo.jpg'); 

3. Inline Keyboard

Creating an inline keyboard:

 const options = {
  reply_markup: {
    inline_keyboard: [
      [
        { text: 'Button 1', callback_data: '1' },
        { text: 'Button 2', callback_data: '2' },
      ],
    ],
  },
};
bot.sendMessage(chatId, 'Choose one:', options); 

4. Handling Callbacks

Handle responses from inline keyboard buttons:

 bot.on('callback_query', (query) => {
  const data = query.data;
  const msg = query.message;
  bot.sendMessage(msg.chat.id, `You clicked button ${data}`);
}); 

5. Sending Documents

To send a document:

 bot.sendDocument(chatId, 'path/to/document.pdf'); 

Example App

Here is a simple bot application that uses some of the covered APIs:

 const TelegramBot = require('node-telegram-bot-api'); const token = 'YOUR_TELEGRAM_BOT_TOKEN'; const bot = new TelegramBot(token, {polling: true});
bot.on('message', (msg) => {
  const chatId = msg.chat.id;
  if (msg.text.toLowerCase() === 'photo') {
    bot.sendPhoto(chatId, 'path/to/photo.jpg');
  } else if (msg.text.toLowerCase() === 'document') {
    bot.sendDocument(chatId, 'path/to/document.pdf');
  } else if (msg.text.toLowerCase() === 'keyboard') {
    const options = {
      reply_markup: {
        inline_keyboard: [
          [
            { text: 'Button 1', callback_data: '1' },
            { text: 'Button 2', callback_data: '2' },
          ],
        ],
      },
    };
    bot.sendMessage(chatId, 'Choose one:', options);
  } else {
    bot.sendMessage(chatId, 'I received your message');
  }
});
bot.on('callback_query', (query) => {
  const data = query.data;
  const msg = query.message;
  bot.sendMessage(msg.chat.id, `You clicked button ${data}`);
}); 

With these examples and an understanding of the node-telegram-bot-api library, you can create highly interactive and efficient Telegram bots. Happy coding!

Hash: 8daf8c21dc6e07e65e8a5d59467e30b3fb27138b7db161a8896c1eef156b8241

Leave a Reply

Your email address will not be published. Required fields are marked *