-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.go
114 lines (95 loc) · 2.24 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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"strconv"
"strings"
)
var webhook = "https://hooks.slack.com/services/"
type message struct {
Text string `json:"text,omitempty"`
Blocks []block `json:"blocks"`
}
type block struct {
Kind string `json:"type"`
Text text `json:"text"`
}
type text struct {
Kind string `json:"type"`
Text string `json:"text"`
}
func sendMessage(msg message, token string, dryRun bool) error {
log.Printf("sending message")
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(&msg); err != nil {
return fmt.Errorf("failed to encode message: %v", err)
}
url := fmt.Sprintf("%s%s", webhook, token)
req, err := http.NewRequest(http.MethodPost, url, buf)
if err != nil {
return fmt.Errorf("failed to create request: %v", err)
}
req.Header.Add("Content-Type", "application/json")
if dryRun {
log.Println("skipping Slack message")
return nil
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed to send request: %v", err)
}
if s := res.StatusCode; s > 299 {
buf = new(bytes.Buffer)
_, err = buf.ReadFrom(res.Body)
if err != nil {
return fmt.Errorf("failed to read response: %v", err)
}
switch resp := buf.String(); resp {
case "no_text":
return errors.New("no text")
case "invalid_payload":
return errors.New("invalid payload")
default:
log.Printf("slack response: %v", resp)
return errors.New(resp)
}
}
return nil
}
func makeMessage(u userFile) message {
t := makeMessageString(u.username, u.filenames)
m := block{
Kind: "section",
Text: text{
Kind: "mrkdwn",
Text: t,
},
}
f := block{
Kind: "section",
Text: text{
Kind: "mrkdwn",
Text: fmt.Sprintf("<https://drive.google.com/drive/u/0/folders/%s|Gå til utleggsmappen og se alle utleggene til %v>", u.id, u.username),
},
}
return message{
Blocks: []block{m, f},
}
}
func makeMessageString(name string, files []string) string {
msg := strings.Builder{}
msg.WriteString(name)
msg.WriteString(" har lastet opp ")
msg.WriteString(strconv.Itoa(len(files)))
msg.WriteString(" nye utlegg:\n ")
for _, f := range files {
msg.WriteString("* ")
msg.WriteString(f)
msg.WriteString("\n")
}
return msg.String()
}