-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord.go
70 lines (55 loc) · 1.1 KB
/
discord.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
package discord
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/csmith/goplum"
)
var client = http.Client{Timeout: 20 * time.Second}
type Plugin struct{}
func (p Plugin) Alert(kind string) goplum.Alert {
switch kind {
case "message":
return MessageAlert{}
default:
return nil
}
}
func (p Plugin) Check(_ string) goplum.Check {
return nil
}
type MessageAlert struct {
Url string
}
func (m MessageAlert) Send(details goplum.AlertDetails) error {
payload, err := json.Marshal(struct {
Content string `json:"content"`
}{
details.Text,
})
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, m.Url, bytes.NewReader(payload))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode >= 400 {
return fmt.Errorf("bad response from Discord: HTTP %d", res.StatusCode)
}
return nil
}
func (m MessageAlert) Validate() error {
if len(m.Url) == 0 {
return fmt.Errorf("missing required argument: url")
}
return nil
}