Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Node.js Bot.onText() API for Telegram Bot

  • Difficulty Level : Medium
  • Last Updated : 30 Sep, 2020

The Bot.onText() method is used in the Node.js Telegram Bot API. This Node.js module to interact with the official Telegram Bot API. This method is used to reply when the users interact with the Telegram BOT.


TelegramBot.onText(regexp, callback)

Parameters: This Method accepts two parameters as mentioned above and described below:

  • regexp: It’s the regular expression, the message must contains /echo.
  • callback: It is the callback function which is passed as parameter and called when the function call is executed.

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

Installing Module: Install the module using the following command:

npm i telegram-bot-api

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

var token = 'Enter the token';
const TelegramBot = require('node-telegram-bot-api');
const bot = new TelegramBot(token, {polling: true});
// Matches "/echo [whatever]"
bot.onText(/\/echo(.+)/, (msg, match) => {
 // The 'msg' is the received Message from Telegram
 // and 'match' is the result of executing the regexp 
 // above on the text content of the message
 const chatId =;
 // The captured "whatever"
 const resp = match[1]; 
 // send back the matched "whatever" to the chat

Run bot.js file using the following command:

node bot.js


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!