-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathhandler.go
114 lines (101 loc) · 3 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"github.com/nlopes/slack"
)
// interactionHandler handles interactive message response.
type interactionHandler struct {
slackClient *slack.Client
verificationToken string
}
func (h interactionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
log.Printf("[ERROR] Invalid method: %s", r.Method)
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("[ERROR] Failed to read request body: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
jsonStr, err := url.QueryUnescape(string(buf)[8:])
if err != nil {
log.Printf("[ERROR] Failed to unespace request body: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
var message slack.AttachmentActionCallback
if err := json.Unmarshal([]byte(jsonStr), &message); err != nil {
log.Printf("[ERROR] Failed to decode json message from slack: %s", jsonStr)
w.WriteHeader(http.StatusInternalServerError)
return
}
// Only accept message from slack with valid token
if message.Token != h.verificationToken {
log.Printf("[ERROR] Invalid token: %s", message.Token)
w.WriteHeader(http.StatusUnauthorized)
return
}
action := message.Actions[0]
switch action.Name {
case actionSelect:
value := action.SelectedOptions[0].Value
// Overwrite original drop down message.
originalMessage := message.OriginalMessage
originalMessage.Attachments[0].Text = fmt.Sprintf("OK to order %s ?", strings.Title(value))
originalMessage.Attachments[0].Actions = []slack.AttachmentAction{
{
Name: actionStart,
Text: "Yes",
Type: "button",
Value: "start",
Style: "primary",
},
{
Name: actionCancel,
Text: "No",
Type: "button",
Style: "danger",
},
}
w.Header().Add("Content-type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(&originalMessage)
return
case actionStart:
title := ":ok: your order was submitted! yay!"
responseMessage(w, message.OriginalMessage, title, "")
return
case actionCancel:
title := fmt.Sprintf(":x: @%s canceled the request", message.User.Name)
responseMessage(w, message.OriginalMessage, title, "")
return
default:
log.Printf("[ERROR] ]Invalid action was submitted: %s", action.Name)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
// responseMessage response to the original slackbutton enabled message.
// It removes button and replace it with message which indicate how bot will work
func responseMessage(w http.ResponseWriter, original slack.Message, title, value string) {
original.Attachments[0].Actions = []slack.AttachmentAction{} // empty buttons
original.Attachments[0].Fields = []slack.AttachmentField{
{
Title: title,
Value: value,
Short: false,
},
}
w.Header().Add("Content-type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(&original)
}