-
Notifications
You must be signed in to change notification settings - Fork 0
/
slash.go
121 lines (101 loc) · 2.82 KB
/
slash.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
package pikabot
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"sort"
"strings"
"unicode"
"github.com/nlopes/slack"
)
type PikaSlash struct {
SlackSigningSecret string
ScoreFilepath string
}
func CreatePikaSlash(secret string, scorepath string) PikaSlash {
return PikaSlash{
SlackSigningSecret: secret,
ScoreFilepath: scorepath,
}
}
func (pika *PikaSlash) ParseMarkCommand(command slack.SlashCommand) *slack.Msg {
response := []rune(command.Text)
for index, character := range response {
if rand.Int()%2 == 0 {
response[index] = unicode.ToUpper(character)
} else {
response[index] = unicode.ToLower(character)
}
}
return &slack.Msg{
Text: string(response),
ResponseType: slack.ResponseTypeInChannel,
}
}
func (pika *PikaSlash) ParseScoreCommand(command slack.SlashCommand) *slack.Msg {
scorelist := CreateScoreList()
scorelist.Read(pika.ScoreFilepath)
name := command.Text[:len(command.Text)-2]
count := command.Text[len(command.Text)-2:]
text := "invalid command"
if command.Text == "top" {
sort.Sort(sort.Reverse(scorelist))
var topScores []string
for index := 0; index < 3; index++ {
topScores = append(topScores, fmt.Sprintf("%s: %d", scorelist.Scores[index].Name, scorelist.Scores[index].Count))
}
text = strings.Join(topScores, "\n")
} else if count == "++" {
score := scorelist.Increment(name)
text = fmt.Sprintf("%s: %d", score.Name, score.Count)
} else if count == "--" {
score := scorelist.Decrement(name)
text = fmt.Sprintf("%s: %d", score.Name, score.Count)
}
log.Println("Writing scores to file:", pika.ScoreFilepath)
scorelist.Write(pika.ScoreFilepath)
return &slack.Msg{
Text: text,
ResponseType: slack.ResponseTypeInChannel,
}
}
func (pika *PikaSlash) SlashHandler(response http.ResponseWriter, request *http.Request) {
verifier, err := slack.NewSecretsVerifier(request.Header, pika.SlackSigningSecret)
if err != nil {
response.WriteHeader(http.StatusInternalServerError)
return
}
request.Body = ioutil.NopCloser(io.TeeReader(request.Body, &verifier))
command, err := slack.SlashCommandParse(request)
if err != nil {
log.Println("Slack error:", err)
response.WriteHeader(http.StatusInternalServerError)
return
}
if err = verifier.Ensure(); err != nil {
log.Println("Slack error:", err)
response.WriteHeader(http.StatusUnauthorized)
return
}
log.Println("Command:", command.Command, command.Text)
var params *slack.Msg
switch command.Command {
case "/mark":
params = pika.ParseMarkCommand(command)
case "/score":
params = pika.ParseScoreCommand(command)
}
if params != nil {
data, err := json.Marshal(params)
if err == nil {
response.Header().Set("Content-Type", "application/json")
response.Write(data)
return
}
}
response.WriteHeader(http.StatusInternalServerError)
}