-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.go
124 lines (97 loc) · 2.67 KB
/
slack.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
// Copyright 2022 Blues Inc. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"net/http"
"strings"
"github.com/slack-go/slack"
)
// Send a message to Slack. See:
// https://api.slack.com/reference/messaging/payload
// https://github.com/slack-go/slack
func slackSendMessage(message string) (err error) {
payload := &slack.WebhookMessage{
Text: message,
}
return slack.PostWebhook(Config.SlackWebhookURL, payload)
}
// Slack inbound 'slash command' request handler
func inboundWebSlackRequestHandler(w http.ResponseWriter, r *http.Request) {
s, err := slack.SlashCommandParse(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
switch s.Command {
case "/notehub":
responseMarkdown := slackCommandWatcher(s)
if len(responseMarkdown) > 0 && slackUsingBlocksForResponses() {
blocks := slack.Blocks{
BlockSet: []slack.Block{
slack.NewSectionBlock(
&slack.TextBlockObject{
Type: slack.MarkdownType,
Text: responseMarkdown,
},
nil,
nil,
),
},
}
w.Header().Set("Content-type", "application/json")
slackResponse := slack.WebhookMessage{}
slackResponse.Blocks = &blocks
slackResponseJSON, _ := json.Marshal(slackResponse)
w.Write(slackResponseJSON)
} else {
w.Write([]byte(responseMarkdown))
}
default:
w.Write([]byte("unknown command"))
}
}
// True if we're using blocks, which have certain limitations
func slackUsingBlocksForResponses() bool {
return true
}
// Slack /notehub request handler
func slackCommandWatcher(s slack.SlashCommand) (response string) {
// Register flags
f := flag.NewFlagSet("/notehub", flag.ContinueOnError)
// Add options here
// var fTest string
// f.StringVar(&fTest, "test", "", "testing 1-2-3")
// Pre-generate error output
errOutput := bytes.NewBufferString("")
errOutput.WriteString("\nOptions:\n")
defer f.SetOutput(nil)
f.SetOutput(errOutput)
f.PrintDefaults()
// Parse flags
f.Parse(strings.Split(s.Text, " "))
// Server arg is required
if f.Arg(0) == "" {
return "/notehub <server> [<action> [<args>]]"
}
// Dispatch based on primary arg
switch f.Arg(1) {
case "":
return watcherShow(f.Arg(0), "")
case "stats":
statsMaintainNow.Signal()
return "stats maintenance update requested"
case "show":
return watcherShow(f.Arg(0), f.Arg(2))
case "activity":
go watcherActivity(f.Arg(0))
return ""
case "request":
return watcherSendRequest(f.Arg(0), f.Arg(2))
}
return fmt.Sprintf("request '%s' not recognized\n"+errOutput.String(), f.Arg(0))
}