-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (50 loc) · 992 Bytes
/
main.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
)
const (
EnvSlackWebhook = "SLACK_WEBHOOK"
EnvSlackMessage = "SLACK_MESSAGE"
)
type Webhook struct {
Text string `json:"text,omitempty"`
}
func main() {
endpoint := os.Getenv(EnvSlackWebhook)
if endpoint == "" {
fmt.Fprintln(os.Stderr, "URL is required")
os.Exit(1)
}
text := os.Getenv(EnvSlackMessage)
if text == "" {
fmt.Fprintln(os.Stderr, "Message is required")
os.Exit(1)
}
msg := Webhook{
Text: text,
}
if err := send(endpoint, msg); err != nil {
fmt.Fprintf(os.Stderr, "Error sending message: %s\n", err)
os.Exit(2)
}
}
func send(endpoint string, msg Webhook) error {
enc, err := json.Marshal(msg)
if err != nil {
return err
}
b := bytes.NewBuffer(enc)
res, err := http.Post(endpoint, "application/json", b)
if err != nil {
return err
}
if res.StatusCode >= 299 {
return fmt.Errorf("Error on message: %s\n", res.Status)
}
fmt.Println(res.Status)
return nil
}