-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
383 lines (337 loc) · 11.7 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
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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/magmastonealex/habot/homeassistant"
"github.com/nlopes/slack"
"github.com/nlopes/slack/slackevents"
"io/ioutil"
"net/http"
"regexp"
"strings"
"time"
)
type Confirmation struct {
UserId string
Value bool
}
type InFlightRequest struct {
InitialMessageTs string
CompletionChannel chan Confirmation
TextPart slack.MsgOption
}
type ExecutableAction struct {
ID string `json:"id"`
ChatRegexp string `json:"chatRegexp"`
Message string `json:"message"`
RequiresConfirm bool `json:"requiresConfirm"`
HaSuccessInvoke homeassistant.HaInvoke `json:"haSuccessInvoke"`
HaFailureInvoke homeassistant.HaInvoke `json:"haFailureInvoke"`
}
type ExecutableFetch struct {
ID string `json:"id"`
ChatRegexp string `json:"chatRegexp"`
Ha string `json:"ha"`
}
type GlobalConfiguration struct {
SlackVerifToken string `json:"slackVerifToken"`
SlackBotToken string `json:"slackBotToken"`
SlackChannelRestrict string `json:"slackChannelRestrict"`
HomeAssistantUrl string `json:"homeAssistantUrl"`
HomeAssistantKey string `json:"homeAssistantKey"`
Actions []ExecutableAction `json:"actions"`
ActionsMap map[string]ExecutableAction
Fetches []ExecutableFetch `json:"fetches"`
FetchesMap map[string]ExecutableFetch
}
var flightRequests map[string]*InFlightRequest
//Ugh. I don't like this. But I also don't feel like doing the plumbing work neccessary
// to get this into every single function.
var globalConfig GlobalConfiguration
var api *slack.Client
var ha homeassistant.HomeAssistant
func waitOnRequest(messageType string) {
timer := time.NewTimer(30 * time.Second)
cont := Confirmation{
UserId: "Timer",
Value: true,
}
select {
case <-timer.C:
fmt.Printf("Timer expired %s, continuing request", messageType)
api.UpdateMessage(globalConfig.SlackChannelRestrict, flightRequests[messageType].InitialMessageTs, flightRequests[messageType].TextPart, slack.MsgOptionAttachments(getTimeExpiredAttachment()))
case cont = <-flightRequests[messageType].CompletionChannel:
fmt.Printf("Completed with %d.", cont)
if cont.Value == true {
api.UpdateMessage(globalConfig.SlackChannelRestrict, flightRequests[messageType].InitialMessageTs, flightRequests[messageType].TextPart, slack.MsgOptionAttachments(getConfirmedByAttachment(cont.UserId)))
} else {
api.UpdateMessage(globalConfig.SlackChannelRestrict, flightRequests[messageType].InitialMessageTs, flightRequests[messageType].TextPart, slack.MsgOptionAttachments(getCancelledByAttachment(cont.UserId)))
}
}
var err error
if cont.Value {
err = continueRequest(messageType)
if err != nil {
api.UpdateMessage(globalConfig.SlackChannelRestrict, flightRequests[messageType].InitialMessageTs, flightRequests[messageType].TextPart, slack.MsgOptionAttachments(getConfirmedByAttachment(cont.UserId), getFailureAttachment(err)))
}
} else {
err = cancelRequest(messageType)
if err != nil {
api.UpdateMessage(globalConfig.SlackChannelRestrict, flightRequests[messageType].InitialMessageTs, flightRequests[messageType].TextPart, slack.MsgOptionAttachments(getCancelledByAttachment(cont.UserId), getFailureAttachment(err)))
}
}
timer.Stop()
delete(flightRequests, messageType)
}
func continueRequest(action string) error {
fmt.Printf("Continuing: %s\n", action)
return ha.InvokeService(globalConfig.ActionsMap[action].HaSuccessInvoke)
}
func cancelRequest(action string) error {
fmt.Printf("Canceling: %s\n", action)
return ha.InvokeService(globalConfig.ActionsMap[action].HaFailureInvoke)
}
func getSuccessAttachment() slack.Attachment {
return slack.Attachment{
Text: ":heavy_check_mark: Ran action",
}
}
func getFailureAttachment(err error) slack.Attachment {
return slack.Attachment{
Text: fmt.Sprintf(":x: Failed to execute: %s", err.Error()),
}
}
func getConfirmationAttachment() slack.Attachment {
return slack.Attachment{
Text: "You can override this action if you want",
Fallback: "You are unable to override this action on this device",
CallbackID: "confirmation_1",
Actions: []slack.AttachmentAction{
slack.AttachmentAction{
Name: "confirm",
Text: "I object!",
Style: "danger",
Type: "button",
Value: "no",
},
slack.AttachmentAction{
Name: "confirm",
Text: "Go ahead",
Type: "button",
Value: "yes",
},
},
}
}
func getTimeExpiredAttachment() slack.Attachment {
return slack.Attachment{
Text: ":stopwatch: Time expired. Continuing",
}
}
func getConfirmedByAttachment(userid string) slack.Attachment {
return slack.Attachment{
Text: fmt.Sprintf(":heavy_check_mark: Thanks <@%s> for confirming!", userid),
}
}
func getCancelledByAttachment(userid string) slack.Attachment {
return slack.Attachment{
Text: fmt.Sprintf(":x: <@%s> cancelled!", userid),
}
}
func getCommandedBy(ame *slackevents.AppMentionEvent) string {
if ame == nil {
return "An automation"
}
return fmt.Sprintf("<@%s>", ame.User)
}
func getDataText(about string) string {
return fmt.Sprintf("Here's all you ever wanted to know about %s", about)
}
func startConfirmedRequest(ame *slackevents.AppMentionEvent, typ string) {
textPart := slack.MsgOptionText(fmt.Sprintf("%s %s", getCommandedBy(ame), globalConfig.ActionsMap[typ].Message), false)
if flightRequests[typ] != nil {
flightRequests[typ].CompletionChannel <- Confirmation{
UserId: ame.User,
Value: true,
}
api.PostMessage(globalConfig.SlackChannelRestrict, textPart, slack.MsgOptionText("Someone already made that request! Confirming for you...", false))
} else {
_, msgTs, _ := api.PostMessage(globalConfig.SlackChannelRestrict, textPart, slack.MsgOptionAttachments(getConfirmationAttachment()))
flightRequests[typ] = &InFlightRequest{
InitialMessageTs: msgTs,
CompletionChannel: make(chan Confirmation),
TextPart: textPart,
}
go waitOnRequest(typ)
}
}
func getState(id string) {
var ents []homeassistant.SummarizedEntity
var err error
var friendlyName string
if strings.Contains(id, "group.") {
var group homeassistant.GroupEntity
group, err = ha.GetEntitiesForGroup(id)
ents = group.SubEntities
friendlyName = group.FriendlyName
} else {
var entity homeassistant.SummarizedEntity
entity, err = ha.GetEntityForId(id)
ents = make([]homeassistant.SummarizedEntity, 1)
ents[0] = entity
friendlyName = entity.FriendlyName
}
if err != nil {
fmt.Println("[ERROR] Failed to fetch entities")
fmt.Println(err)
api.PostMessage(globalConfig.SlackChannelRestrict, slack.MsgOptionText("I'm sorry, I encountered an error trying to get that for you.", false))
return
}
attachments := make([]slack.Attachment, len(ents))
for idx, entity := range ents {
attachments[idx] = slack.Attachment{
Text: fmt.Sprintf("%s: %s", entity.FriendlyName, entity.State),
}
}
api.PostMessage(globalConfig.SlackChannelRestrict, slack.MsgOptionText(getDataText(friendlyName), false), slack.MsgOptionAttachments(attachments...))
}
func mentionRouter(ame *slackevents.AppMentionEvent) error {
for _, act := range globalConfig.Actions {
if ok, _ := regexp.MatchString(act.ChatRegexp, ame.Text); ok {
fmt.Printf("Matched with: %s \n", act.ChatRegexp)
if act.RequiresConfirm {
startConfirmedRequest(ame, act.ID)
} else {
err := continueRequest(act.ID)
if err == nil {
api.PostMessage(globalConfig.SlackChannelRestrict, slack.MsgOptionText(act.Message, false))
} else {
api.PostMessage(globalConfig.SlackChannelRestrict, slack.MsgOptionText(fmt.Sprintf("I'm sorry, I encountered an error: %s", err.Error()), false))
}
}
return nil
}
}
for _, act := range globalConfig.Fetches {
if ok, _ := regexp.MatchString(act.ChatRegexp, ame.Text); ok {
go getState(act.Ha)
return nil
}
}
api.PostMessage(globalConfig.SlackChannelRestrict, slack.MsgOptionText("I'm sorry, I don't know what that means", false))
return nil
}
func actionRouter(action string) error {
if _, ok := globalConfig.ActionsMap[action]; ok {
if globalConfig.ActionsMap[action].RequiresConfirm {
startConfirmedRequest(nil, action)
} else {
return continueRequest(action)
}
return nil
} else {
return errors.New("Unknown option")
}
}
func main() {
flightRequests = make(map[string]*InFlightRequest)
jsonConfig, err := ioutil.ReadFile("config.json")
if err != nil {
panic("Can't read config.json!")
}
if err := json.Unmarshal(jsonConfig, &globalConfig); err != nil {
panic(err)
return
}
globalConfig.ActionsMap = make(map[string]ExecutableAction)
globalConfig.FetchesMap = make(map[string]ExecutableFetch)
for _, act := range globalConfig.Actions {
globalConfig.ActionsMap[act.ID] = act
}
for _, ftch := range globalConfig.Fetches {
globalConfig.FetchesMap[ftch.ID] = ftch
}
ha = homeassistant.HomeAssistant{
BaseUrl: globalConfig.HomeAssistantUrl,
ApiKey: globalConfig.HomeAssistantKey,
}
api = slack.New(globalConfig.SlackBotToken)
http.HandleFunc("/events-endpoint", func(w http.ResponseWriter, r *http.Request) {
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
body := buf.String()
eventsAPIEvent, e := slackevents.ParseEvent(json.RawMessage(body), slackevents.OptionVerifyToken(&slackevents.TokenComparator{VerificationToken: globalConfig.SlackVerifToken}))
if e != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Println("error:")
fmt.Println()
fmt.Println(e)
return
}
if eventsAPIEvent.Type == slackevents.URLVerification {
var r *slackevents.ChallengeResponse
err := json.Unmarshal([]byte(body), &r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "text")
w.Write([]byte(r.Challenge))
}
if eventsAPIEvent.Type == slackevents.CallbackEvent {
innerEvent := eventsAPIEvent.InnerEvent
switch ev := innerEvent.Data.(type) {
case *slackevents.AppMentionEvent:
{
if ev.Channel == globalConfig.SlackChannelRestrict {
fmt.Printf("[INFO] Got AppMentionEvent. %s from %s in %s\n", ev.Text, ev.User, ev.Channel)
mentionRouter(ev)
}
}
}
}
w.WriteHeader(http.StatusOK)
})
http.HandleFunc("/bot-interaction", func(w http.ResponseWriter, r *http.Request) {
body := r.PostFormValue("payload")
var decoded slack.InteractionCallback
e := json.Unmarshal([]byte(body), &decoded)
if e != nil {
fmt.Printf("[INFO] Failed to decode interactions %s\n", body)
fmt.Println(e)
w.WriteHeader(http.StatusInternalServerError)
return
}
if decoded.Token != globalConfig.SlackVerifToken {
fmt.Printf("[WARN] Slack token mismatch: %s\n", decoded.Token)
w.WriteHeader(http.StatusBadRequest)
return
}
if decoded.Type == slack.InteractionTypeInteractionMessage {
fmt.Printf("[INFO] Reply received for %s %s %s val\n", decoded.CallbackID, decoded.MessageTs, decoded.ActionTs)
if flightRequests["vacuum"] != nil {
flightRequests["vacuum"].CompletionChannel <- Confirmation{
UserId: decoded.User.ID,
Value: decoded.Actions[0].Value == "yes",
}
w.WriteHeader(http.StatusOK)
} else {
originalMessage := decoded.OriginalMessage
originalMessage.Attachments = []slack.Attachment{}
originalMessage.Text = "I'm not quite sure what you just replied to..."
w.Header().Add("content-type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(&originalMessage)
}
return
} else {
fmt.Printf("[INFO] Unknown interaction type %s\n", decoded.Type)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
})
fmt.Println("[INFO] Server listening")
http.ListenAndServe(":3000", nil)
}