@ -9,23 +9,32 @@ npm install node-telegram-bot-api
```js
var TelegramBot = require('node-telegram-bot-api');
// replace the value below with the Telegram token you receive from @BotFather
var token = 'YOUR_TELEGRAM_BOT_TOKEN';
// Setup polling way
// Create a bot that uses 'polling' to fetch new updates
var bot = new TelegramBot(token, { polling: true });
// Matches /echo [whatever]
// Matches " /echo [whatever]"
bot.onText(/\/echo (.+)/, function (msg, match) {
var fromId = msg.from.id;
var resp = match[1];
bot.sendMessage(fromId, resp);
// 'msg' is the received Message from Telegram
// 'match' is the result of executing the regexp above on the text content
// of the message
var chatId = msg.chat.id;
var resp = match[1]; // the captured "whatever"
// send back the matched "whatever" to the chat
bot.sendMessage(chatId, resp);
});
// Any kind of message
// Listen for any kind of message. There are different kinds of
// messages.
bot.on('message', function (msg) {
var chatId = msg.chat.id;
// photo can be: a file path, a stream or a Telegram file_id
var photo = 'cats.png';
bot.sendPhoto(chatId, photo, { caption: 'Lovely kittens'} );
// send a message to the chat acknowledging receipt of their message
bot.sendMessage(chatId, "Received your message" );
});
```