/
hipchat.go
69 lines (54 loc) · 1.79 KB
/
hipchat.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
59
60
61
62
63
64
65
66
67
68
69
package hipchat
import (
"fmt"
"net/http"
"net/url"
"github.com/hailo-platform/H2O/service/config"
"github.com/mreiferson/go-httpclient"
)
const apiUrl = "https://api.hipchat.com/v1/rooms/message"
var defaultNotifier = NewHttpNotifier()
// HttpNotifier is a notifier that talks to the Hipchat API via HTTP
type HttpNotifier struct {
transport *httpclient.Transport
client *http.Client
authToken string
}
// NewHttpNotifier mints an HTTP notifier, configuring it from the config service
func NewHttpNotifier() *HttpNotifier {
n := &HttpNotifier{}
n.transport = &httpclient.Transport{}
n.transport.ConnectTimeout = config.AtPath("hailo", "service", "hipchat", "connectTimeout").AsDuration("1s")
n.transport.RequestTimeout = config.AtPath("hailo", "service", "hipchat", "requestTimeout").AsDuration("10s")
n.transport.ResponseHeaderTimeout = config.AtPath("hailo", "service", "hipchat", "responseHeaderTimeout").AsDuration("5s")
n.authToken = config.AtPath("hailo", "service", "hipchat", "authToken").AsString("811e7e99296f4b2310b3e0a9bc6206")
n.client = &http.Client{Transport: n.transport}
return n
}
// Notify sends a message
func (n *HttpNotifier) Notify(msg *Message) error {
if err := msg.Valid(); err != nil {
return err
}
data := url.Values{}
data.Add("room_id", msg.Room)
data.Add("from", msg.From)
data.Add("message", msg.Body)
data.Add("message_format", string(msg.Format))
if msg.Notify {
data.Add("notify", "1")
}
data.Add("color", string(msg.Colour))
data.Add("format", "json")
data.Add("auth_token", n.authToken)
rsp, err := n.client.PostForm(apiUrl, data)
if err != nil {
return fmt.Errorf("HTTP POST failed: %v", err)
}
defer rsp.Body.Close()
return nil
}
// Notify wraps DefaultNotifier.Notify
func Notify(msg *Message) error {
return defaultNotifier.Notify(msg)
}