forked from usdevs/cinnabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.go
347 lines (293 loc) · 10.7 KB
/
basic.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
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
package cinnabot
import (
"net/http"
"strconv"
"strings"
"encoding/json"
"io/ioutil"
"log"
"math"
"regexp"
"cinnabot/model"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"gopkg.in/telegram-bot-api.v4"
)
//Test functions [Not meant to be used in bot]
// SayHello says hi.
func (cb *Cinnabot) SayHello(msg *message) {
cb.SendTextMessage(int(msg.Chat.ID), "Hello there, "+msg.From.FirstName+"!")
}
// Echo parrots back the argument given by the user.
func (cb *Cinnabot) Echo(msg *message) {
if len(msg.Args) == 0 {
replyMsg := tgbotapi.NewMessage(int64(msg.Message.From.ID), "/echo Cinnabot Parrot Mode 🤖\nWhat do you want me to parrot?\n\n")
replyMsg.BaseChat.ReplyToMessageID = msg.MessageID
replyMsg.ReplyMarkup = tgbotapi.ForceReply{ForceReply: true, Selective: true}
cb.SendMessage(replyMsg)
return
}
response := "🤖: " + strings.Join(msg.Args, " ")
cb.SendTextMessage(int(msg.Chat.ID), response)
}
// Capitalize returns a capitalized form of the input string.
func (cb *Cinnabot) Capitalize(msg *message) {
cb.SendTextMessage(int(msg.Chat.ID), strings.ToUpper(strings.Join(msg.Args, " ")))
}
//Start initializes the bot
func (cb *Cinnabot) Start(msg *message) {
text := "Hello there " + msg.From.FirstName + "!\n\n" +
"Im Cinnabot🤖. I am made by my owners to serve the residents of Cinnamon college!\n" +
"Im always here to /help if you need it!"
cb.SendTextMessage(int(msg.Chat.ID), text)
}
// Help gives a list of handles that the user may call along with a description of them
func (cb *Cinnabot) Help(msg *message) {
if len(msg.Args) > 0 {
if msg.Args[0] == "spaces" {
text :=
"To use the '/spaces' command, type one of the following:\n" +
"'/spaces' : to view all bookings for today\n'/spaces now' : to view bookings active at this very moment\n" +
"'/spaces week' : to view all bookings for this week\n'/spaces dd/mm(/yy)' : to view all bookings on a specific day\n" +
"'/spaces dd/mm(/yy) dd/mm(/yy)' : to view all bookings in a specific range of dates"
cb.SendTextMessage(int(msg.Chat.ID), text)
return
} else if msg.Args[0] == "cbs" {
text :=
"/subscribe <tag>: subscribe to a tag\n" +
"/unsubscribe <tag>: unsubscribe from a tag\n" +
"/broadcast <tag>: broadcast to a tag [admin]\n" +
"Alternatively you can just type:\n" +
"/subscribe for a button list\n" +
"/unsubscribe for a button list\n"
cb.SendTextMessage(int(msg.Chat.ID), text)
return
} else if msg.Args[0] == "resources" {
text :=
"/resources <tag>: searches resources for a specific tag\n" +
"/resources: returns all tags"
cb.SendTextMessage(int(msg.Chat.ID), text)
return
} else if msg.Args[0] == "publicbus" {
text :=
"/publicbus : publicbus\n" +
"Sending your location (ignore the buttons) after running the above command will allow to get bus timings for bus stops around any location."
cb.SendTextMessage(int(msg.Chat.ID), text)
return
}
}
text :=
"Here are a list of functions to get you started 🤸 \n" +
"/about: to find out more about me\n" +
"/cbs: cinnamon broadcast system\n" +
"/publicbus: public bus timings for bus stops around your location\n" +
"/nusbus: nus bus timings for bus stops around your location\n" +
"/weather: 2h weather forecast\n" +
"/resources: list of important resources!\n" +
"/spaces: list of space bookings\n" +
"/feedback: to give feedback\n\n" +
"_*My creator actually snuck in a few more functions🕺 *_\n" +
"Try using /help <func name> to see what I can _really_ do"
cb.SendTextMessage(int(msg.Chat.ID), text)
}
// About returns a link to Cinnabot's source code.
func (cb *Cinnabot) About(msg *message) {
cb.SendTextMessage(int(msg.Chat.ID), "Touch me: https://github.com/pengnam/Cinnabot")
}
//Link returns useful resources
func (cb *Cinnabot) Resources(msg *message) {
resources := make(map[string]string)
resources["usplife"] = "[fb page](https://www.facebook.com/groups/usplife/)"
resources["food"] = "@rcmealbot"
resources["spaces"] = "[spaces web](http://www.nususc.com/Spaces.aspx)"
resources["usc"] = "[usc web](http://www.nususc.com/MainPage.aspx)"
resources["study groups"] = "@USPhonebook\\_bot"
var key string = strings.ToLower(strings.Join(msg.Args, " "))
log.Print(key)
_, ok := resources[key]
if ok {
cb.SendTextMessage(int(msg.Chat.ID), resources[key])
} else {
var values string = ""
for key, _ := range resources {
values += key + " : " + resources[key] + "\n"
}
msg := tgbotapi.NewMessage(msg.Chat.ID, values)
msg.DisableWebPagePreview = true
msg.ParseMode = "markdown"
cb.SendMessage(msg)
}
}
//Structs for weather forecast function
type WeatherForecast struct {
AM []AreaMetadata `json:"area_metadata"`
FD []ForecastData `json:"items"`
}
type AreaMetadata struct {
Name string `json:"name"`
Loc tgbotapi.Location `json:"label_location"`
}
type ForecastData struct {
FMD []ForecastMetadata `json:"forecasts"`
}
type ForecastMetadata struct {
Name string `json:"area"`
Forecast string `json:"forecast"`
}
//Weather checks the weather based on given location
func (cb *Cinnabot) Weather(msg *message) {
//Check if weather was sent with location, if not reply with markup
if len(msg.Args) == 0 || !cb.CheckArgCmdPair("/weather", msg.Args) {
opt1 := tgbotapi.NewKeyboardButtonRow(tgbotapi.NewKeyboardButton("Cinnamon"))
opt2B := tgbotapi.NewKeyboardButton("Here")
opt2B.RequestLocation = true
opt2 := tgbotapi.NewKeyboardButtonRow(opt2B)
options := tgbotapi.NewReplyKeyboard(opt1, opt2)
replyMsg := tgbotapi.NewMessage(int64(msg.Message.From.ID), "🤖: Where are you?\n\n")
replyMsg.ReplyMarkup = options
cb.SendMessage(replyMsg)
return
}
//Default loc: Cinnamon
loc := &tgbotapi.Location{Latitude: 1.306671, Longitude: 103.773556}
if msg.Location != nil {
loc = msg.Location
}
//Send request to api.data.gov.sg for weather data
client := &http.Client{}
req, _ := http.NewRequest("GET", "https://api.data.gov.sg/v1/environment/2-hour-weather-forecast", nil)
req.Header.Set("api-key", "d1Y8YtThOpkE5QUfQZmvuA3ktrHa1uWP")
resp, _ := client.Do(req)
responseData, _ := ioutil.ReadAll(resp.Body)
wf := WeatherForecast{}
if err := json.Unmarshal(responseData, &wf); err != nil {
log.Fatal(err)
return
}
lowestDistance := distanceBetween(wf.AM[0].Loc, *loc)
nameMinLoc := wf.AM[0].Name
for i := 1; i < len(wf.AM); i++ {
currDistance := distanceBetween(wf.AM[i].Loc, *loc)
if currDistance < lowestDistance {
lowestDistance = currDistance
nameMinLoc = wf.AM[i].Name
}
}
log.Print("The closest location is " + nameMinLoc)
var forecast string
for i, _ := range wf.FD[0].FMD {
if wf.FD[0].FMD[i].Name == nameMinLoc {
forecast = wf.FD[0].FMD[i].Forecast
break
}
}
//Parsing forecast
words := strings.Fields(forecast)
forecast = strings.ToLower(strings.Join(words[:len(words)-1], " "))
responseString := "🤖: The 2h forecast is " + forecast + " for " + nameMinLoc
returnMsg := tgbotapi.NewMessage(msg.Chat.ID, responseString)
returnMsg.ParseMode = "Markdown"
returnMsg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
cb.SendMessage(returnMsg)
}
//Helper funcs for weather
func distanceBetween(Loc1 tgbotapi.Location, Loc2 tgbotapi.Location) float64 {
x := math.Pow((float64(Loc1.Latitude - Loc2.Latitude)), 2)
y := math.Pow((float64(Loc1.Longitude - Loc2.Longitude)), 2)
return x + y
}
//Broadcast broadcasts a message after checking for admin status [trial]
//Admins are to first send a message with tags before sending actual message
func (cb *Cinnabot) Broadcast(msg *message) {
val := checkAdmin(cb, msg)
if !val {
cb.SendTextMessage(int(msg.Chat.ID), "🤖: Im sorry! You do not seem to be one of my overlords")
return
}
if len(msg.Args) == 0 {
text := "🤖: Please do /broadcast <tag>\n*Tags:*\n"
for i := 0; i < len(cb.allTags); i += 2 {
text += cb.allTags[i] + "\n"
}
cb.SendTextMessage(int(msg.Chat.ID), text)
return
}
//Used to initialize tags in a mark-up. Ensure that people check their tags
if msg.ReplyToMessage == nil {
//Scan for tags
r := regexp.MustCompile(`\/\w*`)
locReply := r.FindStringIndex(msg.Text)
tags := strings.Fields(strings.ToLower(msg.Text[locReply[1]:]))
//Filter for valid tags
var checkedTags []string
for i := 0; i < len(tags); i++ {
if cb.db.CheckTagExists(int(msg.Chat.ID), tags[i]) {
checkedTags = append(checkedTags, tags[i])
}
}
if tags[0] == "all" {
checkedTags = append(checkedTags, "all")
}
if len(checkedTags) == 0 {
cb.SendTextMessage(int(msg.Chat.ID), "🤖: No valid tags found")
return
}
reminderMsg := tgbotapi.NewMessage(msg.Chat.ID, "REMINDER: Please include tag at start of message. \n Format: #<tagname1> #<tagname2> <msg>")
cb.SendMessage(reminderMsg)
//Send in mark-up
replyMsg := tgbotapi.NewMessage(msg.Chat.ID, "/broadcast "+strings.Join(checkedTags, " "))
replyMsg.BaseChat.ReplyToMessageID = msg.MessageID
replyMsg.ReplyMarkup = tgbotapi.ForceReply{ForceReply: true, Selective: true}
cb.SendMessage(replyMsg)
return
}
//Tags to send to
r := regexp.MustCompile(`\/\w*`)
locReply := r.FindStringIndex(msg.ReplyToMessage.Text)
tags := strings.Fields(msg.ReplyToMessage.Text[locReply[1]:])
userGroup := cb.db.UserGroup(tags)
//Forwards message to everyone in the group
for j := 0; j < len(userGroup); j++ {
forwardMess := tgbotapi.NewForward(int64(userGroup[j].UserID), msg.Chat.ID, msg.MessageID)
cb.SendMessage(forwardMess)
}
return
}
func checkAdmin(cb *Cinnabot, msg *message) bool {
for _, admin := range cb.keys.Admins {
if admin == msg.From.ID {
return true
} else if admin == int(msg.Chat.ID) {
return true
}
}
return false
}
// function to count number of users and messages
func (cb *Cinnabot) GetStats(msg *message) {
db := model.InitializeDB()
if cb.CheckArgCmdPair("/stats", msg.Args) {
key := msg.Args[0]
countUsers, countMessages := db.CountUsersAndMessages(key)
mostUsedCommand := db.GetMostUsedCommand(key)
extraString := ""
if key != "forever" {
extraString = " for the " + key
}
cb.SendTextMessage(int(msg.From.ID), "🤖: Here are some stats"+
extraString+"!\n\n"+
"Number of users registered on bot: "+strconv.Itoa(countUsers)+"\n"+
"Numbery of messages typed: "+strconv.Itoa(countMessages)+"\n"+
"Most used command: "+mostUsedCommand)
return
}
opt1 := tgbotapi.NewKeyboardButtonRow(tgbotapi.NewKeyboardButton("Week"))
opt2 := tgbotapi.NewKeyboardButtonRow(tgbotapi.NewKeyboardButton("Month"))
opt3 := tgbotapi.NewKeyboardButtonRow(tgbotapi.NewKeyboardButton("Year"))
opt4 := tgbotapi.NewKeyboardButtonRow(tgbotapi.NewKeyboardButton("Forever"))
options := tgbotapi.NewReplyKeyboard(opt1, opt2, opt3, opt4)
replyMsg := tgbotapi.NewMessage(int64(msg.Message.From.ID),
"🤖: Please select the time period.")
replyMsg.ReplyMarkup = options
cb.SendMessage(replyMsg)
return
}