Add Makefile
[vaultmon/.git] / slackapi.go
CommitLineData
9a72579f
SB
1package main
2
3import (
4 "encoding/json"
5
6 log "github.com/sirupsen/logrus"
7)
8
9type SlackClient struct {
10 url string
11 channel string
12 username string
13 icon string
14 link_names int
15}
16
17func NewSlackClient(url, channel, username, icon string, link_names int) *SlackClient {
18 return &SlackClient{
19 url: url,
20 channel: channel,
21 username: username,
22 icon: icon,
23 link_names: link_names,
24 }
25}
26
27func DefaultSlackClient(url, channel string) *SlackClient {
28 return NewSlackClient(url, channel, "theknowledge-bot", ":boom:", 1)
29}
30
31func (s *SlackClient) Send(message string) error {
32
33 cnx := NewConnection(s.url)
34 headers := HeaderMap{
35 "Content-type": "application/json",
36 }
37 cnx.SetHeaders(headers)
38
39 jsonStruct := struct {
40 Text string `json:"text"`
41 Channel string `json:"channel"`
42 Linknames int `json:"link_names"`
43 Username string `json:"username"`
44 Icon string `json:"icon"`
45 }{
46 message,
47 s.channel,
48 s.link_names,
49 s.username,
50 s.icon,
51 }
52
53 payload, err := json.Marshal(jsonStruct)
54 if err != nil {
55 log.Errorf("Marshal failed %s", err)
56 return err
57 }
58
59 log.Debugf("Slack payload '%s' to send", string(payload))
60
61 resp, err := cnx.Post(string(payload))
62 if err != nil {
63 log.Errorf("Post failed %s", err)
64 return err
65 }
66
67 log.Debugf("Post message succeeded %s", resp.Status)
68
69 return nil
70}