forked from mullwar/telebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKittyBot.js
68 lines (50 loc) · 1.6 KB
/
KittyBot.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
/*
KittyBot
Shows random Kitty pictures and gifs.
See this code in action, by visiting @KittyBot on Telegram!
*/
const TeleBot = require('../');
const bot = new TeleBot('-PASTEYOURTELEGRAMBOTAPITOKENHERE-');
// Great API for this bot
const API = 'https://thecatapi.com/api/images/get?format=src&type=';
// Command keyboard
const markup = bot.keyboard([
['/kitty', '/kittygif']
], { resize: true, once: false });
// Log every text message
bot.on('text', function(msg) {
console.log(`[text] ${ msg.chat.id } ${ msg.text }`);
});
// On command "start" or "help"
bot.on(['/start', '/help'], function(msg) {
return bot.sendMessage(msg.chat.id,
'😺 Use commands: /kitty, /kittygif and /about', { markup }
);
});
// On command "about"
bot.on('/about', function(msg) {
let text = '😽 This bot is powered by TeleBot library ' +
'https://github.com/kosmodrey/telebot Go check the source code!';
return bot.sendMessage(msg.chat.id, text);
});
// On command "kitty" or "kittygif"
bot.on(['/kitty', '/kittygif'], function(msg) {
let promise;
let id = msg.chat.id;
let cmd = msg.text.split(' ')[0];
// Photo or gif?
if (cmd == '/kitty') {
promise = bot.sendPhoto(id, API + 'jpg', { fileName: 'kitty.jpg' });
} else {
promise = bot.sendDocument(id, API + 'gif', { fileName: 'kitty.gif' });
}
// Send "uploading photo" action
bot.sendAction(id, 'upload_photo');
return promise.catch(error => {
console.log('[error]', error);
// Send an error
bot.sendMessage(id, `😿 An error ${ error } occurred, try again.`);
});
});
// Start getting updates
bot.connect();