Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Node.js Bot.on() Method

  • Last Updated : 22 Jan, 2021

The Bot.on() method is used in the Node.js Modern Tele graf Bot Framework. This Framework provides various functions to interact with the official Telegram Bot API. This method is executed when a particular event occurs during the conversation with the bot for eg: Events like Sending text, Sending Photo, etc these operations handle by Context function. 
Syntax: 

TelegrafBot.on(event, Context function)

Parameters: This Method accepts two parameters as mentioned above and described below: 
   1.Event: Occurrence of particular activity during the conversation with bot.  
   2.Context function:  This function encapsulate telegram update information.

Return Type: The return type of the function is void.

Installing Module: Install the module using the following command:
 

npm install telegraf

Steps to get the keys: 
 1.First, get the GET BOT_TOKEN from BOTFATHER in telegram. Just search for BOTFATHER in Telegram and select the verified one as shown  below: 
 



2.Type /start and then click on /newbot as shown below: 
 

 3.Now type the name of the bot and that must be unique. 
 

  4.Now just copy the token From the BotFather. And for deleting the token simply search /delete token in BotFather. 
 

Project structure: 
 

Filename: bot.js 
 

javascript




var token = 'Enter the token';
  
const telegraf =require("telegraf");
var token='Token';
const bot=new telegraf(token); //Creating object of Telegraf
  
bot.on("text",ctx=>{
//"Event of the text"
  
  ctx.reply("This is the text"//context function reply the message
  
})
bot.on("photo",ctx=>{
  
  ctx.reply("This is the photo"); //context function reply the message
  
})
  
  
});

 
 Run bot.js file using the following command: 
 

node bot.js

Output:
 

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!