-
Notifications
You must be signed in to change notification settings - Fork 1
/
interaction.go
102 lines (87 loc) · 2.37 KB
/
interaction.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/chris-hamper/go-slack-poll/poll"
"github.com/nlopes/slack"
)
// interactionHandler handles interactive message response.
type interactionHandler struct {
signingSecret []byte
}
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
}
// Only accept message from slack with valid token.
if !validateRequest(r, h.signingSecret) {
log.Printf("[ERROR] Message validation failed")
w.WriteHeader(http.StatusUnauthorized)
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
}
action := message.Actions[0]
parts := strings.Split(action.Name, "_")
p := poll.GetPollByID(parts[0])
var response *slack.Msg
switch parts[1] {
case "delete":
if message.User.ID == p.Owner {
p.Delete()
} else {
response = &slack.Msg{
ResponseType: "ephemeral",
ReplaceOriginal: false,
Text: "Sorry, only <@" + message.User.ID + "> can delete this poll.",
}
}
default:
optionIndex, err := strconv.Atoi(parts[1])
if err != nil {
log.Printf("[ERROR] Invalid action name: %s", action.Name)
w.WriteHeader(http.StatusBadRequest)
return
}
p.ToggleVote(message.User.ID, optionIndex)
p.Save()
}
if response == nil {
response = &slack.Msg{
ResponseType: "in_channel",
ReplaceOriginal: true,
Attachments: []slack.Attachment{*p.ToSlackAttachment()},
}
}
encoder := json.NewEncoder(w)
encoder.SetEscapeHTML(false)
w.Header().Set("Content-type", "application/json")
w.WriteHeader(http.StatusOK)
err = encoder.Encode(&response)
if err != nil {
log.Println("[ERROR] JSON Encode failed:", err)
}
}