-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
136 lines (116 loc) · 3 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
package main
import (
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"os"
"strings"
"time"
"unicode"
log "github.com/sirupsen/logrus"
"github.com/slack-go/slack"
)
func main() {
apiKey, ok := os.LookupEnv("CLAPBOT_SLACK_API_KEY")
if !ok {
log.Fatal("missing API key")
}
signingSecret, ok := os.LookupEnv("CLAPBOT_SLACK_SIGNING_SECRET")
if !ok {
log.Fatal("missing signing key")
}
api := slack.New(apiKey)
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { return })
http.HandleFunc("/readyz", func(w http.ResponseWriter, r *http.Request) { return })
http.HandleFunc("/clap", func(w http.ResponseWriter, r *http.Request) {
verifier, err := slack.NewSecretsVerifier(r.Header, signingSecret)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
r.Body = ioutil.NopCloser(io.TeeReader(r.Body, &verifier))
s, err := slack.SlashCommandParse(r)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if err = verifier.Ensure(); err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusUnauthorized)
return
}
user, err := api.GetUserInfo(s.UserID)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
switch s.Command {
case "/clap":
_, _, err = api.PostMessage(s.ChannelID,
slack.MsgOptionUsername(user.RealName),
slack.MsgOptionIconURL(user.Profile.ImageOriginal),
slack.MsgOptionText(addClap(s.Text), true))
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(200)
case "/randomcase":
_, _, err = api.PostMessage(s.ChannelID,
slack.MsgOptionUsername(user.RealName),
slack.MsgOptionIconURL(user.Profile.ImageOriginal),
slack.MsgOptionText(randomCase(s.Text, time.Now().Unix()), true))
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(200)
default:
fmt.Println("unknown command")
w.WriteHeader(http.StatusInternalServerError)
return
}
})
fmt.Println("[INFO] Server listening on 8080")
http.ListenAndServe(":8080", nil)
}
func addClap(text string) string {
newText := ""
for i, s := range text {
// The clap never belongs at the start of the sentence
if i == 0 {
newText = fmt.Sprintf("%s%c", newText, s)
continue
}
if i == len(text)-1 {
newText = fmt.Sprintf("%s%c :clap:", newText, s)
continue
}
if s == ' ' && (unicode.IsLetter(rune(text[i-1])) || unicode.IsPunct(rune(text[i-1]))) {
newText = fmt.Sprintf("%s :clap:%c", newText, s)
} else {
newText = fmt.Sprintf("%s%c", newText, s)
}
}
return newText
}
func randomCase(text string, seed int64) string {
rand.Seed(seed)
newText := ""
for _, s := range text {
randInt := rand.Intn(2)
if randInt == 0 {
newText = fmt.Sprintf("%s%s", newText, strings.ToUpper(string(s)))
} else {
newText = fmt.Sprintf("%s%s", newText, string(s))
}
}
return newText
}