This repository has been archived by the owner on Jan 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
86 lines (67 loc) · 1.91 KB
/
app.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/clintjedwards/snark/config"
"github.com/clintjedwards/snark/helpers/httputil"
"github.com/nlopes/slack"
)
//We define two slackClients because each token has different permissions
// that do not and cannot overlap
type app struct {
config *config.Config
slackAppClient *slack.Client
slackBotClient *slack.Client
messages map[string]*trackedMessage //map with messageID as the key
}
func newApp() *app {
config, err := config.FromEnv()
if err != nil {
log.Fatal(err)
}
slackAppClient := slack.New(config.Slack.AppToken)
slackBotClient := slack.New(config.Slack.BotToken)
return &app{
config: config,
slackAppClient: slackAppClient,
slackBotClient: slackBotClient,
messages: make(map[string]*trackedMessage),
}
}
func (app *app) isValidEmoji(messageID, emoji string) bool {
for _, validEmoji := range app.messages[messageID].ValidEmojis {
if validEmoji == emoji {
return true
}
}
return false
}
func (app *app) sendEvent(messageEvent messageEvent) error {
trackedMessage, err := app.getMessage(messageEvent.ID)
if err != nil {
return err
}
jsonString, _ := json.Marshal(&messageEvent)
headers := map[string]string{
"content-type": "application/json",
"snark-auth-token": trackedMessage.AuthToken,
}
response, err := httputil.SendHTTPPOSTRequest(trackedMessage.CallbackURL, headers, jsonString, app.config.Debug)
if err != nil {
return err
}
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNoContent {
err := fmt.Errorf("callback URL did not return correct status code; recieved %d", response.StatusCode)
return err
}
return nil
}
func (app *app) getVersionHandler(w http.ResponseWriter, req *http.Request) {
response := struct {
Version string `json:"Version"`
}{Version: "0.1.0"}
sendResponse(w, http.StatusOK, response, false)
return
}