forked from trinchan/slackbot
/
main.go
262 lines (227 loc) · 6.95 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strings"
"github.com/gistia/slackbot/db"
_ "github.com/gistia/slackbot/importer"
"github.com/gistia/slackbot/robots"
"github.com/gistia/slackbot/userbot"
"github.com/gistia/slackbot/utils"
"github.com/gistia/slackbot/web"
"github.com/gorilla/mux"
"github.com/gorilla/schema"
"github.com/yvasiyarov/gorelic"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/slack", slashCommandHandler)
r.HandleFunc("/slack_hook", hookHandler)
r.HandleFunc("/callback", callbackHandler)
http.Handle("/", r)
pokerRouter := mux.NewRouter()
pokerRouter.Methods("GET").Path("/poker").HandlerFunc(web.NewPokerStories)
pokerRouter.Methods("POST").Path("/poker").HandlerFunc(web.CreatePokerStories)
http.Handle("/poker", pokerRouter)
go db.SetupDB()
if os.Getenv("RUN_BOT") != "" {
go startBot()
}
startServer()
}
func startNewRelic() {
key := os.Getenv("NEW_RELIC_LICENSE_KEY")
agent := gorelic.NewAgent()
agent.Verbose = true
agent.NewrelicLicense = key
fmt.Println("Starting NewRelic for " + key)
agent.Run()
}
func startBot() {
userbot.Start()
}
func hookHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
d := schema.NewDecoder()
command := new(robots.OutgoingWebHook)
err = d.Decode(command, r.PostForm)
if err != nil {
log.Println("Couldn't parse post request:", err)
}
// if command.Text == "" || command.Token != os.Getenv(fmt.Sprintf("%s_OUT_TOKEN", strings.ToUpper(command.TeamDomain))) {
// log.Printf("[DEBUG] Ignoring request from unidentified source: %s - %s", command.Token, r.Host)
// w.WriteHeader(http.StatusBadRequest)
// return
// }
fmt.Println(command.Text)
fmt.Println(command.TriggerWord)
com := strings.TrimPrefix(command.Text, command.TriggerWord) // +" ")
c := strings.Split(com, " ")
command.Robot = c[0]
command.Text = strings.Join(c[1:], " ")
robots := getRobots(command.Robot)
if len(robots) == 0 {
msg := fmt.Sprintf("No robot for %s (%s)", command.Robot, command.Text)
jsonResp(w, msg)
return
}
resp := ""
for _, robot := range robots {
resp += fmt.Sprintf("\n%s", robot.Run(&command.Payload))
}
w.WriteHeader(http.StatusOK)
jsonResp(w, strings.TrimSpace(resp))
}
func slashCommandHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
d := schema.NewDecoder()
command := new(robots.SlashCommand)
err = d.Decode(command, r.PostForm)
if err != nil {
log.Println("Couldn't parse post request:", err)
}
if command.Command == "" || command.Token == "" {
log.Printf("[DEBUG] Ignoring request from unidentified source: %s - %s", command.Token, r.Host)
w.WriteHeader(http.StatusBadRequest)
return
}
command.Robot = command.Command[1:]
if token := os.Getenv(fmt.Sprintf("%s_SLACK_TOKEN", strings.ToUpper(command.Robot))); token != "" && token != command.Token {
log.Printf("[DEBUG] Ignoring request from unidentified source: %s - %s", command.Token, r.Host)
w.WriteHeader(http.StatusBadRequest)
}
robots := getRobots(command.Robot)
if len(robots) == 0 {
plainResp(w, "No robot for that command yet :(")
return
}
resp := ""
for _, robot := range robots {
resp += fmt.Sprintf("\n%s", robot.Run(&command.Payload))
}
plainResp(w, strings.TrimSpace(resp))
}
type MvnAuthResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
}
func mvnError(domain string, chanId string, user string, s string) {
msg := fmt.Sprintf(
"There was an error authenticating @%s with Mavenlink: %s",
user, s)
MvnSend(domain, chanId, msg)
}
func callbackHandler(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
domain := params.Get("domain")
user := params.Get("user")
chanId := params.Get("channel")
fmt.Println("callbackHandler", params)
if params.Get("error") != "" {
mvnError(domain, chanId, user, params.Get("error_description"))
return
}
// client_id is the ID assigned to your application by Mavenlink
// client_secret is the secret token assigned to your application by Mavenlink
// grant_type must be set to "authorization_code" in order to exchange a code for an access token
// code is the value that was returned in the code query parameter when Mavenlink redirected back to your redirect_uri
// redirect_uri is the exact same value that you used in the original request to /oauth/authorize
// 199c12d7ebc29800ec202fbad0b2585a1f84950536851527a95a024881adbb2b
code := params.Get("code")
callback := os.Getenv("MAVENLINK_CALLBACK")
callback = fmt.Sprintf("%s?domain=%s&user=%s&channel=%s",
callback, domain, user, chanId)
rp := url.Values{}
rp.Set("client_id", os.Getenv("MAVENLINK_APP_ID"))
rp.Set("client_secret", os.Getenv("MAVENLINK_SECRET"))
rp.Set("grant_type", "authorization_code")
rp.Set("code", code)
rp.Set("redirect_uri", callback)
fmt.Println("Sending", rp)
resp, err := utils.Request(
"POST", "https://app.mavenlink.com/oauth/token",
rp, map[string]string{})
if err != nil {
mvnError(domain, chanId, user, err.Error())
return
}
fmt.Println("Response from Mavenlink:", string(resp))
var b *MvnAuthResponse
err = json.Unmarshal(resp, &b)
if err != nil {
fmt.Println("Error unmarshal", err.Error())
mvnError(domain, chanId, user, err.Error())
return
}
if b.Error != "" {
mvnError(domain, chanId, user, b.ErrorDescription)
return
}
err = db.SetSetting(user, "MAVENLINK_TOKEN", b.AccessToken)
if err != nil {
mvnError(domain, chanId, user, err.Error())
return
}
msg := fmt.Sprintf("Saved Mavenlink authentication token for @%s", user)
MvnSend(domain, chanId, msg)
w.WriteHeader(http.StatusOK)
}
func MvnSend(domain string, chanId string, s string) {
response := &robots.IncomingWebhook{
Domain: domain,
Channel: chanId,
Username: "Mavenlink Bot",
Text: s,
IconEmoji: ":chart_with_upwards_trend:",
UnfurlLinks: true,
Parse: robots.ParseStyleFull,
}
response.Send()
}
func jsonResp(w http.ResponseWriter, msg string) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
resp := map[string]string{"text": msg}
r, err := json.Marshal(resp)
if err != nil {
log.Println("Couldn't marshal hook response:", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(r)
}
func plainResp(w http.ResponseWriter, msg string) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Write([]byte(msg))
}
func startServer() {
port := os.Getenv("PORT")
if port == "" {
log.Fatal("PORT not set")
}
log.Printf("Starting HTTP server on %s", port)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
log.Fatal("Server start error: ", err)
}
}
func getRobots(command string) []robots.Robot {
fmt.Println("Robots:", robots.Robots)
if r, ok := robots.Robots[command]; ok {
return r
}
return nil
}