2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-08-22 18:07:16 +00:00

93 lines
2.1 KiB
JavaScript
Raw Normal View History

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 = process.env.TELEGRAM_TOKEN || 'YOUR_TELEGRAM_BOT_TOKEN';
2017-01-07 12:44:29 +03:00
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
});
// Matches /editable
bot.onText(/\/editable/, function onEditableText(msg) {
const opts = {
reply_markup: {
inline_keyboard: [
[
{
text: 'Edit Text',
// we shall check for this value when we listen
// for "callback_query"
callback_data: 'edit'
}
]
]
}
};
bot.sendMessage(msg.from.id, 'Original Text', opts);
});
// Handle callback queries
bot.on('callback_query', function onCallbackQuery(callbackQuery) {
const action = callbackQuery.data;
const msg = callbackQuery.message;
const opts = {
chat_id: msg.chat.id,
message_id: msg.message_id,
};
let text;
if (action === 'edit') {
text = 'Edited Text';
}
bot.editMessageText(text, opts);
});