1const TelegramBot = require('node-telegram-bot-api');
2
3// replace the value below with the Telegram token you receive from @BotFather
4const token = 'YOUR_TELEGRAM_BOT_TOKEN';
5
6// Create a bot that uses 'polling' to fetch new updates
7const bot = new TelegramBot(token, {polling: true});
8
9// Matches "/echo [whatever]"
10bot.onText(/\/echo (.+)/, (msg, match) => {
11 // 'msg' is the received Message from Telegram
12 // 'match' is the result of executing the regexp above on the text content
13 // of the message
14
15 const chatId = msg.chat.id;
16 const resp = match[1]; // the captured "whatever"
17
18 // send back the matched "whatever" to the chat
19 bot.sendMessage(chatId, resp);
20});
21
22// Listen for any kind of message. There are different kinds of
23// messages.
24bot.on('message', (msg) => {
25 const chatId = msg.chat.id;
26
27 // send a message to the chat acknowledging receipt of their message
28 bot.sendMessage(chatId, 'Received your message');
29});