This repository has been archived by the owner on May 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
270 lines (222 loc) · 6.42 KB
/
handler.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
package main
import (
"fmt"
"strings"
"time"
"github.com/boltdb/bolt"
"github.com/ratorx/chumenu-go/facebook"
"github.com/ratorx/chumenu-go/menus"
)
type eventHandler struct {
commandPrefix string
}
// Defined keywords
const (
lunch = "lunch"
dinner = "dinner"
times = "times"
help = "help"
subscribe = "subscribe"
unsubscribe = "unsubscribe"
)
// Common quick replies
var (
standardQR = facebook.NewQuickReplySlice([]string{lunch, dinner, times, help})
subscriptionQR = facebook.NewQuickReplySlice([]string{unsubscribe, lunch, dinner, times, help})
unsubscriptionQR = facebook.NewQuickReplySlice([]string{subscribe, help})
helpQR = facebook.NewQuickReplySlice([]string{lunch, dinner, times, subscribe, unsubscribe})
defQR = facebook.NewQuickReplySlice([]string{help})
)
// standard Messages
const (
// Subscribe Messages
subscribeSuccess = "Subscribed to receive messages."
subscribeFail = "Currently subscribed."
// Unsubscribe Messages
unsubscribeSuccess = "Unsubscribed from receiving messages."
unsubscribeFail = "Not currently subscribed."
// Other defaults
helpMessage = "Available commands:\n*subscribe* - Receive regular menu updates\n*unsubscribe* - Unsubscribe from menu updates\n*lunch* - Get the next lunch menu\n*dinner* - Get the next the dinner menu\n*times* - Get lunch and dinner times"
unrecognised = "Command not recognised. Type *help* for a list of available commands."
unexpected = "Unexpected Error. Will fix ASAP."
)
func responseMessage(r string, text string, qr []facebook.QuickReply) {
if err := cfg.sendClient.SendMessage(r, text, facebook.Response, qr); err != nil {
cfg.debug.Print(err)
}
}
func subscriptionMessage(r string, text string, qr []facebook.QuickReply) {
if err := cfg.sendClient.SendMessage(r, text, facebook.Subscription, qr); err != nil {
cfg.debug.Print(err)
}
}
func subscribeHandler(sender string) {
s := []byte(sender)
err := cfg.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(cfg.userBucket))
if b == nil {
return fmt.Errorf("database corrupted: bucket %v not found", cfg.userBucket)
}
v := b.Get(s)
if v != nil {
go responseMessage(sender, subscribeFail, subscriptionQR)
return nil
}
err := b.Put(s, []byte{})
if err != nil {
go responseMessage(sender, unexpected, standardQR)
return err
}
go responseMessage(sender, subscribeSuccess, subscriptionQR)
return nil
})
if err != nil {
cfg.debug.Print(err)
}
}
func unsubscribeHandler(sender string) {
s := []byte(sender)
err := cfg.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(cfg.userBucket))
if b == nil {
return fmt.Errorf("database corrupted: bucket %v not found", cfg.userBucket)
}
v := b.Get(s)
if v == nil {
go responseMessage(sender, unsubscribeFail, unsubscriptionQR)
return nil
}
err := b.Delete(s)
if err != nil {
go responseMessage(sender, unexpected, standardQR)
return err
}
go responseMessage(sender, unsubscribeSuccess, unsubscriptionQR)
return nil
})
if err != nil {
cfg.debug.Println(err)
}
}
func getMenu(isLunch bool) (string, menus.Meal) {
currentTime := time.Now()
block, _ := menus.GetData(currentTime.Weekday())
currentHM := hourMinute{uint8(currentTime.Hour()), uint8(currentTime.Minute())}
var prefix string
var meal menus.Meal
if isLunch {
if currentHM.IsAfter(lunchTime.End) {
prefix = "Tomorrow's Lunch:"
meal = block.Next.Lunch
} else {
prefix = "Today's Lunch:"
meal = block.Current.Lunch
}
} else {
if currentHM.IsAfter(dinnerTime.End) {
prefix = "Tomorrow's Dinner:"
meal = block.Next.Dinner
} else {
prefix = "Today's Dinner:"
meal = block.Current.Dinner
}
}
return prefix, meal
}
func menuMessage(r string, isLunch bool) {
prefix, meal := getMenu(isLunch)
responseMessage(r, prefix+"\n"+meal.String(), standardQR)
}
func timedMessage(isLunch, forceSend bool) {
prefix, meal := getMenu(isLunch)
if !forceSend && len(meal) == 0 {
cfg.debug.Print("data unavailable for timed message")
return
}
menu := prefix + "\n" + meal.String()
var num uint
var mealName string
if isLunch {
mealName = "lunch"
} else {
mealName = "dinner"
}
err := cfg.db.View(func(tx *bolt.Tx) error { // nolint: errcheck
// Assume bucket exists and has keys
b := tx.Bucket([]byte(cfg.userBucket))
if b == nil {
return fmt.Errorf("database corrupted: bucket %v not found", cfg.userBucket)
}
b.ForEach(func(k, v []byte) error { // nolint: errcheck
go subscriptionMessage(string(k), menu, subscriptionQR)
num++
return nil
})
return nil
})
if err != nil {
cfg.debug.Println(err)
} else {
cfg.debug.Printf("timed message send attempt for %v message to %v users", mealName, num)
}
}
func announceMessage(message string) {
var num uint
err := cfg.db.View(func(tx *bolt.Tx) error { // nolint: errcheck
// Assume bucket exists and has keys
b := tx.Bucket([]byte(cfg.userBucket))
if b == nil {
return fmt.Errorf("database corrupted: bucket %v not found", cfg.userBucket)
}
b.ForEach(func(k, v []byte) error { // nolint: errcheck
go subscriptionMessage(string(k), message, subscriptionQR)
num++
return nil
})
return nil
})
if err != nil {
cfg.debug.Println(err)
} else {
cfg.debug.Printf("timed message send attempt for announce message to %v users", num)
}
}
func defaultHandler(sender, text string) {
cfg.debug.Printf("unrecognised command: %v", text)
responseMessage(sender, unrecognised, defQR)
}
// nolint : gocyclo
func (e eventHandler) HandleEvent(m []facebook.MessagingEvent) {
for i := range m {
r := m[i].Sender.String()
text := strings.TrimSpace(m[i].Message.Text)
text = strings.Trim(text, "*_`")
text = strings.ToLower(text)
if !strings.HasPrefix(text, e.commandPrefix) {
defaultHandler(r, text)
return
}
text = strings.TrimPrefix(text, e.commandPrefix)
// Parse annouce message
if strings.HasPrefix(text, "announce ") {
announceMessage(strings.TrimPrefix(text, "announce "))
continue
}
switch text {
case "subscribe", "s":
subscribeHandler(r)
case "unsubscribe", "u":
unsubscribeHandler(r)
case "help", "h":
responseMessage(r, helpMessage, helpQR)
case "times", "t":
responseMessage(r, fmt.Sprintf("Lunch Time:\n%s\n\nDinner Time:\n%s\n", lunchTime, dinnerTime), standardQR)
case "lunch", "l":
menuMessage(r, true)
case "dinner", "d":
menuMessage(r, false)
default:
defaultHandler(r, text)
}
}
}