forked from devvmh-forks/plan-b-ot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
planbot.go
54 lines (44 loc) · 1.29 KB
/
planbot.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
// This file is part of the Plan-B-ot package.
// Copyright (c) 2015 Martin Schenck
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
package main
import (
"encoding/json"
"net/http"
"strings"
"./bot"
)
// Runs the server listening for the requests
func main() {
err := bot.ReadConfig("./config/config.json")
if err != nil {
panic(err.Error())
}
http.HandleFunc(bot.Config.Route, planbotHandler)
http.ListenAndServe(":"+bot.Config.Port, nil)
}
// planbotHandler handles incoming requests
// can be task, vote, or result requests
func planbotHandler(w http.ResponseWriter, r *http.Request) {
if r.FormValue("token") != bot.Config.Token {
http.Error(w, "Invalid token in slashcommand setup!", http.StatusBadRequest)
return
}
userName := r.FormValue("user_name")
text := r.FormValue("text")
words := strings.Fields(text)
response, status := bot.HandleRequest(userName, words)
responseJson, err := json.Marshal(response)
if err != nil {
http.Error(w, "Cannot marshal JSON in plan-b-ot", http.StatusInternalServerError)
return
}
if status != http.StatusOK {
http.Error(w, response.Text, status)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
w.Write(responseJson)
}