-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
polling.js
92 lines (76 loc) · 2.05 KB
/
polling.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* This example demonstrates using polling.
* It also demonstrates how you would process and send messages.
*/
const TOKEN = process.env.TELEGRAM_TOKEN || 'YOUR_TELEGRAM_BOT_TOKEN';
const TelegramBot = require('..');
const request = require('request');
const options = {
polling: true
};
const bot = new TelegramBot(TOKEN, options);
// Matches /photo
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!"
});
});
// Matches /audio
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);
});
// Matches /love
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);
});
// Matches /echo [whatever]
bot.onText(/\/echo (.+)/, function onEchoText(msg, match) {
const resp = match[1];
bot.sendMessage(msg.chat.id, resp);
});
// 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);
});