forked from negbie/telefonist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
43 lines (35 loc) · 863 Bytes
/
webhook.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
package main
import (
"bytes"
"fmt"
"net/http"
"strings"
"time"
)
const warnEmo = ":warning: "
const fireEmo = ":fire: "
func page(webhookUrl string, level, msg string) error {
if level == "warning" {
msg = warnEmo + msg
} else if level == "error" {
msg = fireEmo + msg
}
text := "{\"text\":" + fmt.Sprintf("%q", msg) + ",\"username\":\"telefonist\"}"
req, err := http.NewRequest(http.MethodPost, webhookUrl, strings.NewReader(text))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
client := &http.Client{Timeout: 2 * time.Second}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
if buf.String() != "ok" {
return fmt.Errorf("webhook returned HTTP status %s (%d)", resp.Status, resp.StatusCode)
}
return nil
}