This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
garçon.go
434 lines (378 loc) · 14 KB
/
garçon.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
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
package main
import (
"fmt"
"log"
"strings"
"github.com/jasonmoo/ghostmates"
"github.com/nlopes/slack"
"golang.org/x/net/context"
"googlemaps.github.io/maps"
)
const (
atGarconPattern = "((ok|okay)( |, )?)?<@(?P<user>[0-9A-Z]{9})>(:|,)?(\\s*?)"
orderInitiationPattern = "(we'd|we would) (like to) (place an)? ?(order) (for|from)? ?(?P<restaurant>.*)"
abortCommandPattern = "(abort|go away|leave|shut up)"
helpRequestPattern = "(help|help me|help us)(!)?"
orderPlacingPattern = "((I would|I'd) like|I'll have)(\\s*?)(?P<item>.*)"
orderStatusRequestPattern = "(what does|what's) our order look like( so far)??"
orderConfirmationRequestPattern = "I think (we are|we're) ready( now)?"
)
// Garcon is our order taking bot! ヽ(゜∇゜)ノ
type Garcon struct {
debug bool
SelfName string
SelfID string
Stage string
AllowedChannels []string
InterlocutorID string
RequestedRestaurant string
ActualRestaurant *maps.PlaceDetailsResult
Order map[string]string
Patrons map[string]slack.User
MessageTypeFuncs map[string]func(slack.Msg) (string, error)
ReactionFuncs map[string]map[string]func(slack.Msg) []slack.OutgoingMessage
CommandExamples map[string][]string
PostmatesClient *ghostmates.Client
OrderDestination *ghostmates.DeliverySpot
GoogleMapsClient *maps.Client
}
// FindBotSlackID iterates over all the slack users and figures out what
// Garcon's ID is
func (g *Garcon) FindBotSlackID() {
for id, p := range g.Patrons {
if strings.ToLower(p.Name) == strings.ToLower(g.SelfName) {
g.SelfID = id
}
}
}
// MessageAddressesGarcon returns whether or not the message began with some variant
// of "ok, @garcon"
func (g *Garcon) MessageAddressesGarcon(m slack.Msg) bool {
match, _ := findElementsInString(atGarconPattern, []string{"user"}, m.Text)
user := match["user"]
if _, ok := g.Patrons[user]; ok {
if strings.ToLower(g.Patrons[user].Name) == "garcon" {
return true
}
}
return false
}
// Reset wipes the state of Garcon
func (g *Garcon) Reset() {
g.Stage = "uninitiated"
g.InterlocutorID = ""
g.RequestedRestaurant = ""
g.Order = make(map[string]string)
g.ActualRestaurant = &maps.PlaceDetailsResult{}
}
// RespondToMessage TODO: Document
func (g *Garcon) RespondToMessage(m slack.Msg) (responses []slack.OutgoingMessage) {
if m.User == g.SelfID || len(m.User) == 0 {
log.Printf("I've received a message, but I can't respond to it because something's up with the user who sent it: '%v'", m.User)
return
}
if g.debug {
log.Printf("I've received this message:\n\t'%v'\nand I'm going to try to determine what type it is\n", m.Text)
}
mt, err := g.MessageTypeFuncs[g.Stage](m)
if err != nil && g.debug {
log.Printf("I encountered this error determining the message type of that message:\n\t%v\n", err)
}
if g.debug {
log.Printf("I've determined this message:\n\t%v\nto be %v", m.Text, mt)
}
if _, ok := g.ReactionFuncs[g.Stage][mt]; ok {
responses = g.ReactionFuncs[g.Stage][mt](m)
if g.debug {
responseMessages := []string{}
for _, r := range responses {
responseMessages = append(responseMessages, r.Text)
}
debugResponses := strings.Join(responseMessages, "\n\t")
log.Printf("I've decided to respond with the following responses:\n\t%v\n", debugResponses)
}
}
return
}
// ItemAddedToOrder TODO: Document
func (g Garcon) itemAddedToOrder(m slack.Msg) (orderPlaced bool) {
messageAddressesGarcon := g.MessageAddressesGarcon(m)
messageIsPlacingAnOrder := stringFitsPattern(orderPlacingPattern, m.Text)
if messageAddressesGarcon && messageIsPlacingAnOrder {
orderPlaced = true
} else {
if g.debug {
if messageAddressesGarcon {
log.Printf("I think this message is meant for me: %v", m.Text)
} else {
log.Printf("This message wasn't meant for me: %v", m.Text)
}
if messageIsPlacingAnOrder {
log.Printf("I think this message is trying to place an order: %v", m.Text)
} else {
log.Printf("I don't think this message is trying to place an order: %v", m.Text)
}
}
}
return
}
// TODO: Make these more generic
// CancellationCommandIssued returns whether or not the most recent command
// is a show-stopping cancellation command directed at Garcon
func (g Garcon) cancellationCommandIssued(m slack.Msg) bool {
return g.MessageAddressesGarcon(m) && stringFitsPattern(abortCommandPattern, m.Text)
}
// OrderStatusCheckRequested TODO: Document
func (g Garcon) orderStatusCheckRequested(m slack.Msg) bool {
return stringFitsPattern(orderStatusRequestPattern, m.Text)
}
// ReadyToPlaceOrder TODO: Document
func (g Garcon) readyToPlaceOrder(m slack.Msg) bool {
return stringFitsPattern(orderConfirmationRequestPattern, m.Text)
}
// HelpRequested TODO: Document
func (g Garcon) helpRequested(m slack.Msg) bool {
return g.MessageAddressesGarcon(m) && stringFitsPattern(helpRequestPattern, m.Text)
}
func (g *Garcon) suggestHelpCommandResponse(m slack.Msg) []slack.OutgoingMessage {
t := fmt.Sprintf("I'm sorry, @%v, I couldn't understand what you said. For help, say \"@garcon, help me!\"", g.Patrons[m.User].Name)
return []slack.OutgoingMessage{slack.OutgoingMessage{Channel: m.Channel, Text: t}}
}
func (g *Garcon) genericHelpResponse(m slack.Msg) []slack.OutgoingMessage {
s := "\n • "
examples := append(g.CommandExamples[g.Stage], g.CommandExamples["always"]...)
t := fmt.Sprintf("I'm sorry, @%v, I couldn't understand what you said. Here are some things I might understand:%v%v\n", g.Patrons[m.User].Name, s, strings.Join(examples, s))
return []slack.OutgoingMessage{slack.OutgoingMessage{Channel: m.Channel, Text: t}}
}
func (g *Garcon) helloGarcon(m slack.Msg) []slack.OutgoingMessage {
t := fmt.Sprintf("Hi, @%v! Would you like to place an order?", g.Patrons[m.User].Name)
g.InterlocutorID = m.User
g.Stage = "prompted"
return []slack.OutgoingMessage{
slack.OutgoingMessage{Channel: m.Channel, Text: t},
}
}
func (g *Garcon) validateRestaurant(m slack.Msg) []slack.OutgoingMessage {
match, err := findElementsInString(orderInitiationPattern, []string{"restaurant"}, m.Text)
g.RequestedRestaurant = match["restaurant"]
if err != nil || len(g.RequestedRestaurant) == 0 {
return g.suggestHelpCommandResponse(m)
}
gr := &maps.GeocodingRequest{Address: g.OrderDestination.Address}
gresp, err := g.GoogleMapsClient.Geocode(context.TODO(), gr)
if err != nil {
log.Printf("fatal error geocoding Location: %s", err)
return g.suggestHelpCommandResponse(m)
}
nsr := &maps.NearbySearchRequest{
Location: &gresp[0].Geometry.Location,
Radius: 10000, // In meters, this is completely arbitrary
Keyword: g.RequestedRestaurant,
Name: g.RequestedRestaurant,
OpenNow: true,
MinPrice: "0",
MaxPrice: "4",
Type: "restaurant",
}
places, err := g.GoogleMapsClient.NearbySearch(context.TODO(), nsr)
log.Printf("I was able to find %v restaurants matching the requested name.", len(places.Results))
if err != nil {
log.Printf("fatal error conducting search: %s", err)
em := "I'm sorry, I can't find a restaurant like that."
return []slack.OutgoingMessage{slack.OutgoingMessage{Channel: m.Channel, Text: em}}
}
pdr := &maps.PlaceDetailsRequest{PlaceID: places.Results[0].PlaceID}
place, err := g.GoogleMapsClient.PlaceDetails(context.TODO(), pdr)
g.ActualRestaurant = &place
log.Printf("I decided to go with the following restaurant:\n%v\n%v\n", g.ActualRestaurant.Name, g.ActualRestaurant.Vicinity)
t := fmt.Sprintf("Okay, what would everyone like from %v?", g.ActualRestaurant.Name)
g.Stage = "ordering"
return []slack.OutgoingMessage{slack.OutgoingMessage{Channel: m.Channel, Text: t}}
}
func (g *Garcon) validateOrder(m slack.Msg) []slack.OutgoingMessage {
g.Stage = "confirmation"
if g.debug {
log.Printf("I'm going to attempt to confirm the group order.")
}
return []slack.OutgoingMessage{
slack.OutgoingMessage{Channel: m.Channel, Text: "Alright, then!"},
g.orderStatusResponse(m)[0],
slack.OutgoingMessage{Channel: m.Channel, Text: "Is that correct?"},
}
}
func (g *Garcon) addItemToGroupOrder(m slack.Msg) []slack.OutgoingMessage {
matches, err := findElementsInString(orderPlacingPattern, []string{"item"}, m.Text)
item := strings.TrimSpace(matches["item"])
if g.debug {
log.Printf("I've received an order for '%v', and will be adding it to the group order", item)
}
if err != nil || len(item) == 0 {
return g.genericHelpResponse(m)
}
g.Order[g.Patrons[m.User].Name] = item
t := fmt.Sprintf("Okay @%v, I've got your order.", g.Patrons[m.User].Name)
return []slack.OutgoingMessage{slack.OutgoingMessage{Channel: m.Channel, Text: t}}
}
func (g *Garcon) orderIsIncorrect(m slack.Msg) []slack.OutgoingMessage {
t := fmt.Sprintf("Okay, I'll start over.")
g.Stage = "ordering"
g.RequestedRestaurant = ""
g.Order = make(map[string]string)
return []slack.OutgoingMessage{
slack.OutgoingMessage{Channel: m.Channel, Text: t},
}
}
func (g *Garcon) placeOrder(m slack.Msg) []slack.OutgoingMessage {
restaurantName := g.ActualRestaurant.Name
restaurantAddress := g.ActualRestaurant.Vicinity
restaurantPhone := g.ActualRestaurant.FormattedPhoneNumber
t := "Okay, I'll send this order off!"
manifest := *ghostmates.NewManifest(g.createOrderString(), "Group Order")
from := *ghostmates.NewDeliverySpot(restaurantName, restaurantAddress, restaurantPhone)
to := g.OrderDestination
quote, err := g.PostmatesClient.GetQuote(from.Address, to.Address)
if err != nil {
t = "I'm having some problems placing this order, please check the logs."
log.Println("Error creating quote")
log.Fatal(err)
}
err = g.PostmatesClient.CreateDelivery(&manifest, &from, to, quote)
if err != nil {
t = "I'm having some problems placing this order, please check the logs."
log.Println("Error creating delivery")
log.Fatal(err)
}
g.Reset()
return []slack.OutgoingMessage{slack.OutgoingMessage{Channel: m.Channel, Text: t}}
}
func (g *Garcon) createOrderString() string {
orders := ""
for user, order := range g.Order {
orders = fmt.Sprintf("%v%v: %v\n", orders, strings.Title(user), order)
}
return strings.TrimSpace(orders)
}
func (g *Garcon) orderStatusResponse(m slack.Msg) []slack.OutgoingMessage {
statusTemplate := "Here's what I have for your order from %v:\n```\n%v\n```"
statusMessage := fmt.Sprintf(statusTemplate, g.RequestedRestaurant, g.createOrderString())
return []slack.OutgoingMessage{slack.OutgoingMessage{Channel: m.Channel, Text: statusMessage}}
}
func (g *Garcon) genericCancelReponse(m slack.Msg) []slack.OutgoingMessage {
g.Reset()
t := "Very well then, I'll disappear for now!"
return []slack.OutgoingMessage{slack.OutgoingMessage{Channel: m.Channel, Text: t}}
}
// NewGarcon constructs a new instance of Garcon and establishes all the behavior functions
// Note that while it is possible for us to make this code broader and more reusable, I don't
// have an intense interest in doing that right now, and I think such a structure would render
// the code either hilariously unreadable, complicated, and most likely both
func NewGarcon() *Garcon {
g := &Garcon{
SelfName: "garcon",
Stage: "uninitiated",
Order: make(map[string]string),
}
g.CommandExamples = map[string][]string{
"prompted": []string{
"We'd like to place an order for the Chili's at 45th & Lamar",
"We would like to order from the Chili's at 45th & Lamar",
},
"ordering": []string{
"@garcon, I'd like a banana",
"@garcon I'll have the tuna melt",
"@garcon, what's our order look like so far?",
},
"confirmation": []string{
"yes",
"no",
},
"always": []string{
"@garcon, go away",
"@garcon, help!",
},
}
// possible returns: affirmative, negative, additive, cancelling, irrelevant, insufficient
g.MessageTypeFuncs = map[string]func(slack.Msg) (string, error){
"uninitiated": func(m slack.Msg) (string, error) {
if g.cancellationCommandIssued(m) {
return "cancelling", nil
}
if strings.ToLower(m.Text) == "oh, garçon?" || strings.ToLower(m.Text) == "oh, garcon?" {
return "affirmative", nil
}
return "irrelevant", nil
},
"prompted": func(m slack.Msg) (string, error) {
if g.cancellationCommandIssued(m) {
return "cancelling", nil
}
if g.helpRequested(m) {
return "insufficient", nil
}
if responseIsNegative(m.Text) || m.User != g.InterlocutorID {
return "negative", nil
}
if stringFitsPattern(orderInitiationPattern, m.Text) {
return "affirmative", nil
}
return "insufficient", nil
},
"ordering": func(m slack.Msg) (string, error) {
if g.cancellationCommandIssued(m) {
return "cancelling", nil
}
if g.helpRequested(m) {
return "insufficient", nil
}
if g.itemAddedToOrder(m) {
return "contributing", nil
}
if g.orderStatusCheckRequested(m) {
return "status", nil
}
if g.readyToPlaceOrder(m) {
return "affirmative", nil
}
return "indeterminable", nil
},
"confirmation": func(m slack.Msg) (string, error) {
if g.cancellationCommandIssued(m) {
return "cancelling", nil
}
if g.helpRequested(m) {
return "insufficient", nil
}
if responseIsAffirmative(m.Text) {
return "affirmative", nil
}
if responseIsNegative(m.Text) {
return "negative", nil
}
return "insufficient", nil
},
}
g.ReactionFuncs = map[string]map[string]func(slack.Msg) []slack.OutgoingMessage{
"uninitiated": map[string]func(m slack.Msg) []slack.OutgoingMessage{
"affirmative": g.helloGarcon,
},
"prompted": map[string]func(m slack.Msg) []slack.OutgoingMessage{
"affirmative": g.validateRestaurant,
"insufficient": g.genericHelpResponse,
"cancelling": g.genericCancelReponse,
},
"ordering": map[string]func(m slack.Msg) []slack.OutgoingMessage{
"affirmative": g.validateOrder,
"contributing": g.addItemToGroupOrder,
"insufficient": g.genericHelpResponse,
"cancelling": g.genericCancelReponse,
"status": g.orderStatusResponse,
},
"confirmation": map[string]func(m slack.Msg) []slack.OutgoingMessage{
"affirmative": g.placeOrder,
"negative": g.orderIsIncorrect,
"cancelling": g.genericCancelReponse,
"insufficient": g.genericHelpResponse,
},
}
return g
}