-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram-bot.js
59 lines (56 loc) · 1.67 KB
/
telegram-bot.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
var request = require('request');
var cheerio = require('cheerio');
var at = require('at-quotes');
var TelegramBot = require('node-telegram-bot-api');
var token = '';
var bot = new TelegramBot(token, {polling: true});
bot.on('message', function(msg) {
var chatId = msg.chat.id;
switch(msg.text.toLowerCase()) {
case '/commands':
case '/start':
var message = 'Available commands:\n';
message += '/commands - list of availalble commands\n';
message += '/xckd - xckd comics\n';
message += '/finn - to get finn quote\n';
message += '/jake - to get jake quote\n';
message += '/iceking - to get iceking quote\n';
message += '_Anything else_ for _anything else_\n';
bot.sendMessage(chatId, message, {parse_mode: 'Markdown'});
break;
case '/xckd':
case 'xckd':
var url = 'http://xckd.com/' + randomInt(1, 1578);
request(url, function(error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
var imgLink = 'http:' + $('div#comic > img').attr('src');
bot.sendMessage(chatId, imgLink);
} else {
bot.sendMessage(chatId, 'Something went wrong :(');
}
});
break;
case '/finn':
case 'finn':
bot.sendMessage(chatId, at.getFinnQuote());
break;
case '/jake':
case 'jake':
bot.sendMessage(chatId, at.getJakeQuote());
break;
case '/iceking':
case 'iceking':
bot.sendMessage(chatId, at.getIceKingQuote());
break;
case 'what time is it?':
case 'what time is it':
bot.sendMessage(chatId, 'Adventure time!');
break;
default:
bot.sendMessage(chatId, at.getQuote());
}
});
function randomInt(low, high) {
return Math.floor(Math.random() * (high - low) + low);
}