-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
448 lines (417 loc) · 13.5 KB
/
index.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
var fs = require('fs')
var urlExpander = require('expand-url')
var apiKeys = JSON.parse(fs.readFileSync('apiKeys.json', 'utf8'))
var acadCalandar = JSON.parse(fs.readFileSync('academicCalendar.json', 'utf8'))
const AcadYear = '2017/2018'
var TelegramBot = require('node-telegram-bot-api')
var bot = new TelegramBot(apiKeys.telegramKey, { polling: true })
var nusmodsApi = require('./nusmodsApiModule.js')
var sqliteApi = require('./sqlLiteModule.js')
const numOfDays = 5
const numOfHours = 16
const dayMonday = 0
const dayTuesday = 1
const dayWednesday = 2
const dayThursday = 3
const dayFriday = 4
bot.on('message', (msg) => {
if (msg.text) {
if (msg.chat.type.toLowerCase().indexOf('private') === 0) {
handlePrivate(msg)
} else {
handlePublic(msg)
}
}
})
// handles messages sent 1 on 1 to the bot
function handlePrivate (msg) {
if (msg.text.toLowerCase().indexOf('/start') === 0) {
handlePrivateStart(msg)
} else if (msg.text.toLowerCase().indexOf('/test') === 0) {
handlePrivateTest(msg)
} else {
handlePrivateOthers(msg)
}
}
// handles messages sent in a group chat
function handlePublic (msg) {
if (msg.text.toLowerCase().indexOf('/join') === 0) {
handlePublicStart(msg)
} else if (msg.text.toLowerCase().indexOf('/test') === 0) {
handlePublicTest(msg)
} else if (msg.text.toLowerCase().indexOf('/whofreenoschool') === 0) {
freeIgnoreSchool(msg)
} else if (msg.text.toLowerCase().indexOf('/whofree') === 0) {
freeNow(msg)
} else if (msg.text.toLowerCase().indexOf('/whoschool') === 0) {
inSchoolToday(msg)
} else if (msg.text.toLowerCase().indexOf('/help') === 0) {
var toSend = 'Tutorial:'
toSend += '\n1. Start private chat with me'
toSend += '\n2. Enter your nusmods short url'
toSend += '\n3. type /join for whichever group you want to share your timetable with!'
toSend += '\n4. type /whoFree in a group to find out who\'s free!'
bot.sendMessage(msg.chat.id, toSend, {parse_mode: 'HTML'})
}
}
function handlePublicStart (msg) {
sqliteApi.insertUserToChat(msg.from.id, msg.chat.id).then(
function (value) {
bot.sendMessage(msg.chat.id, 'Added to group!', {parse_mode: 'HTML'})
},
function (err) {
if (err) {
bot.sendMessage(msg.chat.id, 'Failed to add you into group!', {parse_mode: 'HTML'})
}
}
)
}
function handlePrivateStart (msg) {
var toSend = 'Please send me your nusmods timetable url!'
bot.sendMessage(msg.chat.id, toSend, {parse_mode: 'HTML'})
}
function handlePrivateOthers (msg) {
urlExpander.expand(msg.text, function (err, longUrl) {
if (err) {
var toSend = 'There was some error deciphering your message, check if you\'ve copied the right url! '
bot.sendMessage(msg.chat.id, toSend, {parse_mode: 'HTML'})
} else {
parseLongUrl(longUrl, msg)
}
})
}
// converts the shortened URL into a long url, replaces escaped characters to more readable characters
function parseLongUrl (longUrl, msg) {
var urlSubStrs = longUrl.split('/')
if (urlSubStrs.length === 6) {
if (urlSubStrs[2] === 'nusmods.com') {
var acadYear = urlSubStrs[4]
var toParse = urlSubStrs[5]
var parseModuleStrings = toParse.split('?')
if (parseModuleStrings.length === 2) {
var semester = parseModuleStrings[0].substring(3)
var modInfo = parseModuleStrings[1].replaceAll('%5B', '[').replaceAll('%5D', ']')
var parsedMods = parseModStr(modInfo)
var promises = []
for (var i = 0; i < parsedMods.length; i++) {
promises.push(nusmodsApi.getModuleInformation(acadYear, semester, parsedMods[i].moduleCode))
}
Promise.all(promises).then(values => {
var oddWeek = []
var evenWeek = []
var len = numOfDays * numOfHours
evenWeek = new Array(len).fill('0')
oddWeek = new Array(len).fill('0')
getTimeSlots(parsedMods, values, evenWeek, oddWeek)
sqliteApi.insertUser(msg.from.id, msg.from.first_name, oddWeek.join(''), evenWeek.join(''), msg.from.username).then(
function (value) {
if (value) {
bot.sendMessage(msg.chat.id, 'Your timetable has been uploaded!', {parse_mode: 'HTML'})
}
},
function (err) {
if (err) {
bot.sendMessage(msg.chat.id, 'Failed to upload timetable!', {parse_mode: 'HTML'})
}
}
)
})
}
}
}
}
// parses the string from the url and converts into a json
function parseModStr (inputStr) {
var modList = []
var inputArr = inputStr.split('&')
for (var i = 0; i < inputArr.length; i++) {
var slotInfo = inputArr[i]
var moduleCode = slotInfo.split('[')[0]
var slotType = slotInfo.split('[').pop().split(']').shift()
var slotValue = slotInfo.split('=').pop()
var modExists = false
for (var j = 0; j < modList.length; j++) {
var toCompareMod = modList[j]
if (toCompareMod.moduleCode === moduleCode) {
modExists = true
modList[j].moduleSlots.push({
slotType: slotType,
slotValue: slotValue
})
}
}
if (!modExists) {
var newMod = {
moduleCode: moduleCode,
moduleSlots: [
{
slotType: slotType,
slotValue: slotValue
}
]
}
modList.push(newMod)
}
}
return modList
}
function getTimeSlots (parsedMods, values, evenWeek, oddWeek) {
for (var j = 0; j < parsedMods.length; j++) {
var oneMod = parsedMods[j]
for (var k = 0; k < oneMod.moduleSlots.length; k++) {
var slotType = oneMod.moduleSlots[k].slotType
var slotValue = oneMod.moduleSlots[k].slotValue
// find what type it is
slotType = getSlotType(slotType)
var timing = values[j].Timetable
// get all the time associated to the slot type
for (var m = 0; m < timing.length; m++) {
var oneTimeSlot = timing[m]
if (oneTimeSlot.LessonType === slotType && oneTimeSlot.ClassNo === slotValue) {
// add start and time end to database
var startTime = oneTimeSlot.StartTime
var endTime = oneTimeSlot.EndTime
var day = oneTimeSlot.DayText
var weekType = oneTimeSlot.WeekText
getSlotArray(startTime, endTime, day, weekType, evenWeek, oddWeek)
}
}
}
}
}
function getSlotType (slotType) {
switch (slotType) {
case 'LEC':
slotType = 'Lecture'
break
case 'TUT':
slotType = 'Tutorial'
break
case 'LAB':
slotType = 'Laboratory'
break
case 'SEM':
slotType = 'Seminar-Style Module Class'
break
case 'SEC':
slotType = 'Sectional Teaching'
break
case 'REC':
slotType = 'Recitation'
break
default:
console.error('cannot find slot type')
}
return slotType
}
function getSlotArray (startTime, endTime, day, weekType, evenWeek, oddWeek) {
var timeArray = ['0800', '0900', '1000', '1100', '1200', '1300', '1400', '1500', '1600', '1700', '1800', '1900', '2000', '2100', '2200', '2300', '0000']
var startTimeFound = false
var index = getSlotPositionByDay(day)
var rowPosition = index * numOfHours
for (var i = 0; i < (timeArray.length - 1); i++) {
var timeTableStart = timeArray[i]
var timeTableEnd = timeArray[i + 1]
var position = rowPosition + i
// finding the starting slot
if (startTime >= timeTableStart && !startTimeFound && startTime < timeTableEnd) {
changeSlot(weekType, evenWeek, oddWeek, position)
if (endTime <= timeTableEnd) {
// for one slot
break
} else {
// span over more than one slot
startTimeFound = true
continue
}
} else if (startTimeFound) {
// finding the ending slot
changeSlot(weekType, evenWeek, oddWeek, position)
if (endTime <= timeTableEnd) {
// found the end time slot
break
}
} else {
continue
}
}
}
function getSlotPositionByDay (day) {
var index = -1
switch (day) {
case 'Monday':
index = dayMonday
break
case 'Tuesday':
index = dayTuesday
break
case 'Wednesday':
index = dayWednesday
break
case 'Thursday':
index = dayThursday
break
case 'Friday':
index = dayFriday
break
default:
console.error('unable to find the day')
}
return index
}
function changeSlot (weekType, evenWeek, oddWeek, position) {
if (weekType === 'Even Week') {
evenWeek[position] = '1'
} else if (weekType === 'Odd Week') {
oddWeek[position] = '1'
} else {
evenWeek[position] = '1'
oddWeek[position] = '1'
}
}
function freeIgnoreSchool (msg) {
var testPromise = sqliteApi.getUsersInChat(msg.chat.id)
testPromise.then(function (values) {
var freeStudents = []
console.log('freeIgnoreSchool', values)
var time = new Date()
var slot = getCurrentSlot(time)
for (var i = 0; i < values.length; i++) {
if (isEvenWeek(time)) {
console.log(time)
console.log(slot)
console.log(values[i].eventimetable.charAt(slot), '0')
if (values[i].eventimetable.charAt(slot) === '0') {
// free
console.log('testVal', values[i])
freeStudents.push(values[i])
}
} else {
if (values[i].oddtimetable.charAt(slot) === '0') {
// free
freeStudents.push(values[i])
}
}
}
if (freeStudents.length <= 0) {
bot.sendMessage(msg.chat.id, 'No one is free now :(', {parse_mode: 'HTML'})
} else {
var toSend = '<b>Students who are free now:</b>'
for (i = 0; i < freeStudents.length; i++) {
toSend += '\n' + freeStudents[i].firstname
}
bot.sendMessage(msg.chat.id, toSend, {parse_mode: 'HTML'})
}
})
}
function freeNow (msg) {
var testPromise = sqliteApi.getUsersInChat(msg.chat.id)
testPromise.then(function (values) {
var freeStudents = []
console.log('inSchoolToday', values)
var time = new Date()
var slot = getCurrentSlot(time)
var day = time.getDay() - 1
for (var i = 0; i < values.length; i++) {
if (isEvenWeek(time)) {
var subStr = values[i].eventimetable.substring(day * numOfHours, (day + 1) * numOfHours)
console.log(subStr)
if ((subStr.indexOf('1') > -1) && (values[i].eventimetable.charAt(slot) === '0')) {
// free
freeStudents.push(values[i])
}
} else {
subStr = values[i].oddtimetable.substring(day * numOfHours, (day + 1) * numOfHours)
if ((subStr.indexOf('1') > -1) && (values[i].oddtimetable.charAt(slot) === '0')) {
// free
freeStudents.push(values[i])
}
}
}
if (freeStudents.length <= 0) {
bot.sendMessage(msg.chat.id, 'No one is free now :(', {parse_mode: 'HTML'})
} else {
var toSend = '<b>Students have school today and Free Now:</b>'
for (i = 0; i < freeStudents.length; i++) {
toSend += '\n' + freeStudents[i].firstname
}
bot.sendMessage(msg.chat.id, toSend, {parse_mode: 'HTML'})
}
})
}
function inSchoolToday (msg) {
var testPromise = sqliteApi.getUsersInChat(msg.chat.id)
testPromise.then(function (values) {
var freeStudents = []
var time = new Date()
var day = time.getDay() - 1
console.log('freeNow', values)
console.log('day', day)
console.log()
for (var i = 0; i < values.length; i++) {
if (isEvenWeek(time)) {
var subStr = values[i].eventimetable.substring(day * numOfHours, (day + 1) * numOfHours)
console.log('freeNow', subStr)
if (subStr.indexOf('1') > -1) {
freeStudents.push(values[i])
}
} else {
subStr = values[i].oddtimetable.substring(day * numOfHours, (day + 1) * numOfHours)
if (subStr.indexOf('1') > -1) {
freeStudents.push(values[i])
}
}
}
if (freeStudents.length <= 0) {
bot.sendMessage(msg.chat.id, 'No one who has school today :(', {parse_mode: 'HTML'})
} else {
var toSend = '<b>Students have school today:</b>'
for (i = 0; i < freeStudents.length; i++) {
toSend += '\n' + freeStudents[i].firstname
}
bot.sendMessage(msg.chat.id, toSend, {parse_mode: 'HTML'})
}
})
}
// unused functions for now
function handlePublicTest (msg) {
console.log('public test')
}
function isEvenWeek (today) {
const divideWeek = 604800000
var acadYear = acadCalandar[AcadYear]
var sem1 = new Date()
var sem2 = new Date()
sem1.setFullYear(acadYear[`1`].start[0], acadYear[`1`].start[1], acadYear[`1`].start[2])
sem2.setFullYear(acadYear[`2`].start[0], acadYear[`2`].start[1], acadYear[`2`].start[2])
if (today >= sem2) {
// settle sem 2
var week = Math.round((today - sem2) / divideWeek) + 1
return week % 2 === 0
} else {
// settle sem 1
week = Math.round((today - sem1) / divideWeek) + 1
return week % 2 === 0
}
}
function getCurrentSlot (time) {
var day = time.getDay() - 1
var timeStr = ('0' + time.getHours()).slice(-2) + '00'
console.log('currTime', timeStr)
var timeArray = ['0800', '0900', '1000', '1100', '1200', '1300', '1400', '1500', '1600', '1700', '1800', '1900', '2000', '2100', '2200', '2300', '0000']
var slot = -1
for (var i = 0; i < timeArray.length; i++) {
if (timeStr === timeArray[i]) {
slot = i + (numOfHours * day)
}
}
return slot
}
function handlePrivateTest (msg) {
console.log('private test!')
}
// overload for String
String.prototype.replaceAll = function (search, replacement) { // eslint-disable-line
var target = this
return target.split(search).join(replacement)
}