2017-01-07 12:44:29 +03:00
|
|
|
/**
|
|
|
|
* This example demonstrates using polling.
|
|
|
|
* It also demonstrates how you would process and send messages.
|
|
|
|
*/
|
2015-06-29 00:37:40 +02:00
|
|
|
|
2017-01-07 12:44:29 +03:00
|
|
|
|
|
|
|
const TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN';
|
|
|
|
const TelegramBot = require('..');
|
|
|
|
const request = require('request');
|
|
|
|
const options = {
|
2015-06-29 00:37:40 +02:00
|
|
|
polling: true
|
|
|
|
};
|
2017-01-07 12:44:29 +03:00
|
|
|
const bot = new TelegramBot(TOKEN, options);
|
2015-06-29 00:37:40 +02:00
|
|
|
|
2015-10-20 00:01:02 +08:00
|
|
|
|
2015-10-20 09:47:58 +02:00
|
|
|
// Matches /photo
|
2017-01-07 12:44:29 +03:00
|
|
|
bot.onText(/\/photo/, function onPhotoText(msg) {
|
|
|
|
// From file path
|
|
|
|
const photo = `${__dirname}/../test/data/photo.gif`;
|
|
|
|
bot.sendPhoto(msg.chat.id, photo, {
|
|
|
|
caption: "I'm a bot!"
|
|
|
|
});
|
2015-10-20 00:01:02 +08:00
|
|
|
});
|
|
|
|
|
2017-01-07 12:44:29 +03:00
|
|
|
|
2015-10-20 09:47:58 +02:00
|
|
|
// Matches /audio
|
2017-01-07 12:44:29 +03:00
|
|
|
bot.onText(/\/audio/, function onAudioText(msg) {
|
|
|
|
// From HTTP request
|
|
|
|
const url = 'https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg';
|
|
|
|
const audio = request(url);
|
|
|
|
bot.sendAudio(msg.chat.id, audio);
|
2015-10-20 00:01:02 +08:00
|
|
|
});
|
|
|
|
|
2017-01-07 12:44:29 +03:00
|
|
|
|
2015-10-20 09:47:58 +02:00
|
|
|
// Matches /love
|
2017-01-07 12:44:29 +03:00
|
|
|
bot.onText(/\/love/, function onLoveText(msg) {
|
|
|
|
const opts = {
|
|
|
|
reply_to_message_id: msg.message_id,
|
|
|
|
reply_markup: JSON.stringify({
|
|
|
|
keyboard: [
|
|
|
|
['Yes, you are the bot of my life ❤'],
|
|
|
|
['No, sorry there is another one...']
|
|
|
|
]
|
|
|
|
})
|
|
|
|
};
|
|
|
|
bot.sendMessage(msg.chat.id, 'Do you love me?', opts);
|
2015-10-20 00:01:02 +08:00
|
|
|
});
|
|
|
|
|
2017-01-07 12:44:29 +03:00
|
|
|
|
|
|
|
// Matches /echo [whatever]
|
|
|
|
bot.onText(/\/echo (.+)/, function onEchoText(msg, match) {
|
|
|
|
const resp = match[1];
|
|
|
|
bot.sendMessage(msg.chat.id, resp);
|
2015-06-29 00:37:40 +02:00
|
|
|
});
|