/
main.go
253 lines (208 loc) · 5.88 KB
/
main.go
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
// ﷽
// (Bismillahirrahmanirrahim)
// NUSWhereBot Beta
// Author: Faruq Rasid <me@ruqqq.sg>
//
// A Telegram bot which retrieves the map for the queried location.
// When running, make sure to specify TELEGRAM_SECRET env variable
// with the bot's secret key.
//
// Retrieve the required libraries below via go get
package main
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/franela/goreq"
"github.com/tucnak/telebot"
)
// Structs definitions
// Query string for calling StreetDirectory API (search via keyword)
type SDApiQuery struct {
Mode string `url:"mode"`
Act string `url:"act"`
Output string `url:"output"`
Limit int `url:"limit"`
Country string `url:"country"`
Profile string `url:"profile"`
Q string `url:"q"`
}
// Query string for calling StreetDirectory API (get png of map given a coord)
type SDMapQuery struct {
Level int `url:"level"`
Lon float64 `url:"lon"`
Lat float64 `url:"lat"`
SizeX int `url:"sizex"`
SizeY int `url:"sizey"`
Star int `url:"star"`
}
// Struct to store location name, lat, lng
type LocationInfo struct {
Name string
Lng float64
Lat float64
}
// Just a request counter
var requestCount int64
// Folder to store the map cache
const MAP_CACHE_DIR = "maps"
const BOT_NICK = "@NUSWhereBot"
func main() {
bot, err := telebot.NewBot(os.Getenv("TELEGRAM_SECRET"))
if err != nil {
panic(err)
}
messages := make(chan telebot.Message)
bot.Listen(messages, 1*time.Second)
fmt.Println("Bot started.")
// Message handler
for message := range messages {
if message.Text == "/help" || message.Text == "/start" {
bot.SendMessage(message.Chat, "Hello, "+message.Sender.FirstName+"! To get the location of a place in NUS, just type the keywords of the place you're looking for.", nil)
} else {
// Perform case insensitive search and remove bot mention in case it's a mention
locations, err := getLocationInfoNUS(strings.Replace(strings.ToLower(message.Text), strings.ToLower(BOT_NICK)+" ", "", 1))
if len(locations) != 0 && err != nil {
bot.SendMessage(message.Chat, "Oops! I encountered an error while searching for the location you requested. Please try again later.", nil)
fmt.Printf("Error while retrieving location: %s\n", err.Error())
continue
}
if len(locations) == 0 {
bot.SendMessage(message.Chat, "Oops! I cannot find any result with your search query.", nil)
continue
}
for _, location := range locations {
bot.SendMessage(message.Chat, "Location found! Sending you the map...", nil)
photo, err := getLocationMap(location)
if err != nil {
bot.SendMessage(message.Chat, "Oops! I encountered an error while searching for the location you requested. Please try again later.", nil)
fmt.Printf("Error while retrieving map: %s\n", err.Error())
continue
}
bot.SendPhoto(message.Chat, photo, nil)
}
}
// Keep track of number of requests (no particular reason to)
requestCount++
fmt.Printf("Total Requests: %d\n", requestCount)
}
}
// Get location (name, lat and lng)
// via NUS Web
func getLocationInfoNUS(query string) ([]LocationInfo, error) {
url := fmt.Sprintf("http://map.nus.edu.sg/index.php/search/by/%s", query)
doc, err := goquery.NewDocument(url)
if err != nil {
return nil, err
}
var locations []LocationInfo
s := doc.Find("#search_list a[href=\"javascript:void(0)\"]").First()
onclick, _ := s.Attr("onclick")
regex := regexp.MustCompile("long=([0-9\\.]+?)&lat=([0-9\\.]+?)'")
matches := regex.FindAllStringSubmatch(onclick, -1)
if len(matches) == 0 || len(matches[0]) != 3 {
return nil, fmt.Errorf("Can't find lat and lng from query: %s", query)
}
x, _ := strconv.ParseFloat(matches[0][1], 64)
y, _ := strconv.ParseFloat(matches[0][2], 64)
location := LocationInfo{
Name: s.Text(),
Lng: x,
Lat: y,
}
locations = append(locations, location)
return locations, nil
}
// Get location (name, lat and lng)
// via StreetDirectory
func getLocationInfo(query string) ([]LocationInfo, error) {
qs := SDApiQuery{
Mode: "search",
Act: "all",
Output: "json",
Limit: 1,
Country: "sg",
Profile: "template_1",
Q: fmt.Sprintf("%s nus", query),
}
res, err := goreq.Request{
Uri: "http://www.streetdirectory.com/api/",
QueryString: qs,
Compression: goreq.Gzip(),
ShowDebug: true,
}.Do()
if err != nil {
return nil, err
}
var resJSON []map[string]interface{}
err = res.Body.FromJsonTo(&resJSON)
if err != nil {
return nil, err
}
var locations []LocationInfo
if resJSON[0]["total"].(float64) > 0 {
for ind, obj := range resJSON {
// Skip first result
if ind == 0 {
continue
}
location := LocationInfo{
Name: obj["t"].(string),
Lng: obj["x"].(float64),
Lat: obj["y"].(float64),
}
locations = append(locations, location)
}
}
return locations, nil
}
// Get map PNG from StreetDirectory
func getLocationMap(location LocationInfo) (*telebot.Photo, error) {
filepath := fmt.Sprintf("%s/%f_%f.png", MAP_CACHE_DIR, location.Lng, location.Lat)
// if map does not exist in our cache, retrieve!
if _, err := os.Stat(filepath); err != nil {
qs := SDMapQuery{
Level: 14,
SizeX: 500,
SizeY: 500,
Lon: location.Lng,
Lat: location.Lat,
Star: 1,
}
res, err := goreq.Request{
Uri: "http://www.streetdirectory.com/api/map/world.cgi",
QueryString: qs,
Compression: goreq.Gzip(),
}.Do()
if err != nil {
return nil, err
}
data, err := res.Body.ToString()
if err != nil {
return nil, err
}
err = ioutil.WriteFile(filepath, []byte(data), 0644)
if err != nil {
return nil, err
}
}
file, err := telebot.NewFile(filepath)
if err != nil {
return nil, err
}
thumbnail := telebot.Thumbnail{
File: file,
Width: 500,
Height: 500,
}
photo := telebot.Photo{
Thumbnail: thumbnail,
Caption: location.Name,
}
return &photo, nil
}