-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbot.js
132 lines (107 loc) · 4.8 KB
/
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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Created by Evgenii Mironichev, Copyright 2016,
// based on this awesome tutorial: https://mvalipour.github.io/node.js/2015/11/10/build-telegram-bot-nodejs-heroku/
var config = require('./config'); // rename config.js.example into config.js and set keys and tokens inside it
var Bot = require('node-telegram-bot-api');
var bot;
if(process.env.NODE_ENV === 'production') {
bot = new Bot(config.TelegramToken);
bot.setWebHook(config.TelegramProductionURL + bot.token);
}
else {
bot = new Bot(config.TelegramToken, { polling: true });
}
//var Bot = require('node-telegram-bot-api'),
// bot = new Bot(config.TelegramToken, { polling: true });
console.log('secon-bot server started...');
// Make sure it is public or set to Anyone with link can view
// "od6" is the fist worksheet in the spreadsheet
var url = "https://spreadsheets.google.com/feeds/list/" + config.googleSheetKey + "/od6/public/values?alt=json";
var moment = require('moment-timezone');
bot.onText(/(.+)$/, function (msg, match) {
// keywords are anything typed in
var keywords = match[1];
var request = require("request");
// send request to retrieve the spreadsheet as the JSON
request(url, function (error, response, body) {
if (error || response.statusCode != 200) {
console.log('Error: '+error); // Show the error
console.log('Status code: ' + response.statusCode); // Show the error
return;
}
var parsed = JSON.parse(body);
var targetTime = NaN;
if (!isNaN(keywords)) // isNaN returns false if the value is number
{
try{
targetTime = parseInt(keywords, 10);
}
catch(e){
targetTime = NaN;
}
}
if (isNaN(targetTime))
targetTime = -1;
var formattedAnswer = "";
// debug purposes: echo from id:
// formattedAnswer += "\nMsg.from.id=" + msg.from.id + "\n";
var currentHours = parseInt(moment().tz(config.confTimeZone).format('HH'),10);
var currentMinutes = parseInt(moment().tz(config.confTimeZone).format('mm'),10);
// console.log("Current hours: " + currentHours);
var currentAnswer = "";
var itemsFound = 0;
// sending answers
parsed.feed.entry.forEach(function(item){
// get the time(in hours) from the very first column
var itemTime = NaN;
var itemTitle = item.title.$t
try{
itemTime = parseInt(itemTitle, 10);
}
catch(e)
{
itemTime = NaN;
}
if (
(!isNaN(itemTime) && itemTime == targetTime) ||
(isNaN(itemTime) && itemTitle.toLowerCase().trim() == keywords.toLowerCase().trim())
)
{
// add the line break if not the first answer
if (itemsFound==0)
formattedAnswer += "At " + targetTime + " these talks will take place:\n\n";
else
formattedAnswer += "\n\n";
itemsFound++;
formattedAnswer += '\u27a1' + item.content.$t; // add item content, '\u27a1' is the arrow emoji
}
else if (currentHours == itemTime) // else collect items for the current hour
{
if (currentAnswer == '')
currentAnswer == 'Starting from ' + currentHours + " h the following talks are goinf:\n\n";
else
currentAnswer += "\n\n";
currentAnswer += '\u27a1' + item.content.$t; // get item content, '\u27a1' is the arrow emoji
}
// else doing nothing
});
// if no items were found for the given time
if (itemsFound == 0)
{
if (targetTime<0 || targetTime>24)
formattedAnswer = "Enter the time to show talks or write 'Hi'.\n\n";
else
formattedAnswer = "Can't find events for the given time ( " + targetTime+ " ч)";
// output current answer
if (currentAnswer != '')
{
formattedAnswer += "Hi! As of " + currentHours + ":" + currentMinutes + " " + config.confTimeZone+ " these talks are going:\n";
formattedAnswer += currentAnswer;
}
}
// send message telegram finally
bot.sendMessage(msg.chat.id, formattedAnswer).then(function () {
// reply sent!
});
});
});
module.exports = bot;