/
slack.go
233 lines (213 loc) · 5.69 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
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
package slack
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"strings"
"time"
log "github.com/cantara/bragi"
)
type slackMessage struct {
SlackId string `json:"channel"`
TS string `json:"thread_ts"`
Text string `json:"text"`
// Username string `json:"username"`
Pinned bool `json:"pinned"`
// Attachments []string `json:"attachments"`
}
type slackRespons struct {
Ok bool `json:"ok"`
SlackId string `json:"channel"`
TS string `json:"ts"`
Message Message `json:"message"`
Warning string `json:"warning"`
ResponseMetadata interface{} `json:"response_metadata"`
}
type Message struct {
BotId string `json:"bot_id"`
Type string `json:"type"`
Text string `json:"text"`
User string `json:"user"`
TS string `json:"ts"`
Team string `json:"team"`
BotProfile BotProfile `json:"bot_profile"`
Deleted bool `json:"deleted"`
Updated int `json:"updated"`
TeamId string `json:"team_id"`
}
type slackFile struct {
Channels []string `json:"channels"`
TS string `json:"ts"`
Text string `json:"initial_comment"`
File []byte `json:"file"`
Name string `json:"name"`
}
type BotProfile struct {
/*
"id": "B02V186UMM5",
"app_id": "A02V959QU94",
"name": "Nerthus",
"icons": {
"image_36": "https:\\/\\/a.slack-edge.com\\/80588\\/img\\/plugins\\/app\\/bot_36.png",
"image_48": "https:\\/\\/a.slack-edge.com\\/80588\\/img\\/plugins\\/app\\/bot_48.png",
"image_72": "https:\\/\\/a.slack-edge.com\\/80588\\/img\\/plugins\\/app\\/service_72.png"
*/
}
type client struct {
baseurl string
token string
secretChannel string
statusChennel string
commandChannel string
messageChan chan Message
}
var c client
var statusMessageChan chan string
func NewClient(authToken, secretChannel, statusChennel, commandChannel string) (err error) {
c = client{
baseurl: "https://slack.com",
token: authToken,
secretChannel: secretChannel,
statusChennel: statusChennel,
commandChannel: commandChannel,
messageChan: make(chan Message, 10),
}
statusMessageChan = make(chan string, 50)
go statusMessageWatcher()
return nil
}
func sendMessage(message, slackId, ts string) (resp slackRespons, err error) {
return resp, PostAuth(c.baseurl+"/api/chat.postMessage", slackMessage{
SlackId: slackId,
TS: ts,
Text: ":ghost:" + message,
Pinned: false,
}, &resp)
}
func SendBase(message string) (id string, err error) {
return SendFollowup(message, "")
}
func SendFollowup(message, id string) (idOut string, err error) {
resp, err := sendMessage(message, c.secretChannel, id)
if err != nil {
return
}
idOut = resp.TS
return
}
func SendFollowupWFile(name, message, id string, file []byte) (idOut string, err error) {
resp, err := sendFile(c.secretChannel, id, message, name, file)
if err != nil {
return
}
idOut = resp.TS
return
}
func sendFile(channel, ts, message, name string, file []byte) (resp slackRespons, err error) {
return resp, PostFormAuth(c.baseurl+"/api/files.upload", slackFile{
Channels: []string{channel},
Text: message,
File: file,
TS: ts,
Name: name,
}, &resp)
}
func statusMessageWatcher() {
ticker := time.NewTicker(5 * time.Second)
builder := strings.Builder{}
failCount := 0
for {
select {
case <-ticker.C:
if builder.Len() <= 0 {
continue
}
_, err := sendMessage(builder.String(), c.statusChennel, "")
if err != nil {
if failCount >= 3 {
failCount = 0
builder.Reset()
continue
}
failCount++
time.Sleep(3 * time.Second)
continue
}
builder.Reset()
case message := <-statusMessageChan:
builder.WriteString(message)
builder.WriteString("\n")
}
}
}
func SendStatus(message string) {
statusMessageChan <- message
}
func PostAuth(uri string, data interface{}, out interface{}) (err error) {
jsonValue, _ := json.Marshal(data)
client := &http.Client{}
req, err := http.NewRequest("POST", uri, bytes.NewBuffer(jsonValue))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+c.token)
resp, err := client.Do(req)
if err != nil || out == nil {
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return
}
err = json.Unmarshal(body, out)
if err != nil {
log.AddError(err).Warning(fmt.Sprintf("%s\t%s", body, data))
}
return
}
func PostFormAuth(uri string, file slackFile, out interface{}) (err error) {
var b bytes.Buffer
mp := multipart.NewWriter(&b)
f, err := mp.CreateFormFile("file", file.Name)
if err != nil {
return
}
f.Write(file.File)
mp.Close()
data := url.Values{}
data.Set("channels", strings.Join(file.Channels, ","))
data.Set("initial_comment", file.Text)
if file.TS != "" {
data.Set("ts", file.TS)
}
req, err := http.NewRequest("POST", uri, &b)
req.Header.Set("Content-Type", mp.FormDataContentType())
req.Header.Set("Authorization", "Bearer "+c.token)
req.URL.RawQuery = data.Encode()
cl := &http.Client{}
resp, err := cl.Do(req)
if err != nil || out == nil {
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return
}
err = json.Unmarshal(body, out)
if err != nil {
log.AddError(err).Warning(fmt.Sprintf("%s\t%s", body, data))
}
return
}
func SendCommand(endpoint, body string) (err error) {
_, err = sendMessage(fmt.Sprintf(`%[1]scurl --header "Content-Type: application/json" \
--header "Authorization: Basic <base64 username and password>" \
--request POST \
--data '%s' \
baseurl/%s%[1]s`, "```", body, endpoint), c.commandChannel, "")
return
}