-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
147 lines (137 loc) · 4.57 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
var fs = require('fs')
var c2j = require('csv-to-json')
var apiKeys = JSON.parse(fs.readFileSync('apiKeys.json', 'utf8'))
// var rates = JSON.parse(fs.readFileSync('rates.json', 'utf8'))
var lotModule = require('./lotAvailability.js')
var rates = []
initRates('formatted.csv')
lotModule.getCarparkAvail()
lotModule.startLoop()
var TelegramBot = require('node-telegram-bot-api')
var bot = new TelegramBot(apiKeys.telegramKey, { polling: true })
var nNearest = 5
bot.on('message', (msg) => {
// console.log(msg);
if (msg.text) {
if (msg.text.toLowerCase().indexOf('/start') === 0) {
handleStart(msg)
}
if (msg.text.toLowerCase().indexOf('/info') === 0) {
handleInfo(msg)
}
if (msg.text.toLowerCase().indexOf('/test') === 0) {
handleTest(msg)
}
if (msg.text.toLowerCase().indexOf('/help') === 0) {
handleHelp(msg)
}
}
if (msg.location) {
// console.log(msg)
getLocation(msg, nNearest)
}
})
function getLocation (msg, n) {
var userLat = msg.location.latitude
var userLng = msg.location.longitude
var topn = []
for (var i = 0; i < rates.length; i++) {
var rate = rates[i]
var latDiff = rate.Location_Lat - userLat
var lngDiff = rate.Location_Lng - userLng
var eucDist = Math.sqrt((latDiff * latDiff) + (lngDiff * lngDiff))
topn.push({eucDist: eucDist, rate: rate})
topn.sort(function (a, b) {
return parseFloat(a.eucDist) - parseFloat(b.eucDist)
})
while (topn.length > n) {
topn.pop()
}
}
// console.log(topn);
var toSend = '<b>The '
toSend += n
toSend += ' nearest carparks are: </b>\n'
for (i = 0; i < topn.length; i++) {
toSend += '<a href="http://maps.google.com?q='
toSend += topn[i].rate.Location_Lat + ',' + topn[i].rate.Location_Lng
toSend += '">'
toSend += topn[i].rate.CarPark
toSend += '</a>'
if (topn[i].rate.Lot_Avail_ID !== '-') {
toSend += '\n<b>Lots Available: </b>'
// toSend += topn[i].rate.Lot_Avail_ID;
// get lots avail here
// console.log(topn[i])
var lotAvailStr = lotModule.getLotAvail(topn[i].rate.Lot_Avail_ID)
toSend += lotAvailStr
}
toSend += '\n<code>Weekdays: </code>\n'
toSend += topn[i].rate.WeekDays_Rate_1
if (topn[i].rate.WeekDays_Rate_2 !== '-') {
toSend += '\n'
toSend += topn[i].rate.WeekDays_Rate_2
}
if (topn[i].rate.Saturday_Rate !== '-') {
toSend += '\n<code>Saturdays: </code>\n'
toSend += topn[i].rate.Saturday_Rate
}
if (topn[i].rate.Sunday_PublicHoliday_Rate !== '-') {
toSend += '\n<code>Sun and PH: </code>\n'
toSend += topn[i].rate.Sunday_PublicHoliday_Rate
}
toSend += '\n\n'
// 'WeekDays_Rate_1':'Daily: $1.30 / 30 Mins','WeekDays_Rate_2':'-','Saturday_Rate':'-','Sunday_PublicHoliday_Rate':'-'
}
bot.sendMessage(msg.chat.id, toSend, {parse_mode: 'HTML'})
}
function handleStart (msg) {
var toSend = 'Hello! Send me your location (paperclip logo, bottom right) and I\'ll find the '
toSend += nNearest
toSend += 'nearest carparks to you, with their rates! \n \n'
toSend += 'Developed for public use by *Ten Zhi-Yang* July 2017'
bot.sendMessage(msg.chat.id, toSend, {parse_mode: 'Markdown'})
}
function handleInfo (msg) {
var toSend = 'Sg-carparks-bots databanks updated at: \n'
toSend += '`1st August 2017`. \n \n'
toSend += 'Data of exactly \n*'
toSend += rates.length
toSend += '* carpark `rates` and \n'
toSend += '*'
toSend += lotModule.carparksArrLen()
toSend += '* carparks\' `lot availibility` have been collated from mytransport.sg datamall. \n \n'
toSend += 'Developed for public use by *Ten Zhi-Yang* \n\n'
toSend += 'any bugs, missing carparks or suggestions? submit an issue or pull request on my [github!](https://github.com/Tzyinc/sg-carpark-bot)'
bot.sendMessage(msg.chat.id, toSend, {parse_mode: 'Markdown'})
}
function handleTest (msg) {
lotModule.test()
}
function handleHelp (msg) {
bot.sendPhoto(msg.chat.id, 'images/helpimage.png')
}
function initRates (filename) {
var fileObj = {
filename: filename
}
var parseCallback = function (err, json) {
if (err) {
console.log(err)
} else {
console.log('csv success!')
// need to remove quotes from results
json = json.map((each) => {
var newStr = JSON.stringify(each)
return JSON.parse(newStr.replaceAll('\\"', ''))
})
rates = json
// console.log(rates[0]);
}
}
c2j.parse(fileObj, parseCallback)
}
String.prototype.replaceAll = function (search, replacement) { // eslint-disable-line
var target = this
return target.split(search).join(replacement)
}