-
Notifications
You must be signed in to change notification settings - Fork 13
/
slack.go
48 lines (37 loc) · 967 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
45
46
47
48
package main
import (
"fmt"
"net/http"
"strings"
)
type slackClient struct {
webHook string
name string
face string
}
func newSlackClient(webHook string) *slackClient {
return &slackClient{
webHook: webHook,
name: "Notifier",
face: "robot_face"}
}
func (slack *slackClient) Name(name string) {
slack.name = name
}
func (slack *slackClient) Face(face string) {
slack.face = face
}
func (slack slackClient) Notify(text string) error {
json := buildRequest(text, slack)
req, err := http.NewRequest("POST", slack.webHook, strings.NewReader(json))
if err != nil {
return fmt.Errorf("Can't connect to host %s: %s", slack.webHook, err.Error())
}
req.Header.Set("Content-Type", "application/json")
client := http.Client{}
_, err = client.Do(req)
return err
}
func buildRequest(text string, slack slackClient) string {
return fmt.Sprintf(`{"text":"%s", "username": "%s", "icon_emoji": ":%s:"}`, text, slack.name, slack.face)
}