-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.go
44 lines (36 loc) · 952 Bytes
/
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
// Copyright (c) 2020 by Wojciech Adam Koszek <wojciech@koszek.com>
package main
import (
"bytes"
"encoding/json"
"errors"
"log"
"net/http"
"time"
)
type slackRequestBody struct {
Text string `json:"text"`
}
// MsgSlack sends a message on Slack
func MsgSlack(ctx *Context, msg string) error {
cfg := ctx.Config
webhookURL := "https://hooks.slack.com/services/" + cfg.Slacks[*ctx.ArgProfileName].Key
log.Printf("webhook: %s", webhookURL)
slackBody, _ := json.Marshal(slackRequestBody{Text: msg})
req, err := http.NewRequest(http.MethodPost, webhookURL, bytes.NewBuffer(slackBody))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
return err
}
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
if buf.String() != "ok" {
return errors.New("Non-ok response returned from Slack")
}
return nil
}