Node.js Telegram Bot API

Telegram Bot API or The Bot API is an HTTP based interface. It is built for developers who build bots for Telegram. It allows you to send to and receive messages from your bot users using a simple function. This doesn’t need any updates of the library since the library doesn’t validate any parameters. You can send anything to the library that will be sent to servers. Creating new bot

  You can create a new bot with BotFather. Contact @BotFather from Telegram messenger. Send a message saying /newbot. You will be asked to choose a name for your bot. Choose a name that ends with bot, eg. my_new_bot. If the name is available you will get a token. Save that token. Your bot is created, set a description for your bot in the middle of the page where it says what can this bot do?. Send /setdescription to BotFather, select your bot and change the description.

Message

We are going to send our first message. Create a Node.js project to install your bot api.


npm install --save node-telegram-bot-api


In your index.js file require node-telegram-bot-api.


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


Now assign the token you got as shown below.


const token = 'YOUR_TELEGRAM_BOT_TOKEN';


Create new bot.


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


The following code will allow you to receive messages from your user.


bot.on('message', (msg) => {

    

     //anything

     

});


To put it all together.


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

const token = 'YOUR_TELEGRAM_BOT_TOKEN';

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

bot.on('message', (msg) => {

    

var Hi = "hi";

if (msg.text.toString().toLowerCase().indexOf(Hi) === 0) {

bot.sendMessage(msg.chat.id,"Hello dear user");

    

});


Open your command prompt and type: node index.js to see how it works.

To start messaging type /start and send. You are all set. For more information go to bot tutorials from npm.