forked from harness/gitness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hipchat.go
181 lines (154 loc) · 4.49 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Package hipchat provides a client library for the Hipchat REST API.
package hipchat
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
const (
baseURL = "https://api.hipchat.com/v1"
ColorYellow = "yellow"
ColorRed = "red"
ColorGreen = "green"
ColorPurple = "purple"
ColorGray = "gray"
ColorRandom = "random"
FormatText = "text"
FormatHTML = "html"
ResponseStatusSent = "sent"
)
type MessageRequest struct {
// Required. ID or name of the room.
RoomId string
// Required. Name the message will appear to be sent from. Must be less
// than 15 characters long. May contain letters, numbers, -, _, and spaces.
From string
// Required. The message body. 10,000 characters max.
Message string
// Determines how the message is treated by our server and rendered
// inside HipChat applications.
// html - Message is rendered as HTML and receives no special treatment.
// Must be valid HTML and entities must be escaped (e.g.: & instead of &).
// May contain basic tags: a, b, i, strong, em, br, img, pre, code.
// Special HipChat features such as @mentions, emoticons, and image previews
// are NOT supported when using this format.
// text - Message is treated just like a message sent by a user. Can include
// @mentions, emoticons, pastes, and auto-detected URLs (Twitter, YouTube, images, etc).
// (default: html)
MessageFormat string
// Whether or not this message should trigger a notification for people
// in the room (change the tab color, play a sound, etc). Each recipient's
// notification preferences are taken into account. 0 = false, 1 = true.
// (default: 0)
Notify bool
// Background color for message. One of "yellow", "red", "green",
// "purple", "gray", or "random".
// (default: yellow)
Color string
}
type ErrorResponse struct {
Error struct {
Code int
Type string
Message string
}
}
type Client struct {
AuthToken string
}
func urlValuesFromMessageRequest(req MessageRequest) (url.Values, error) {
if len(req.RoomId) == 0 || len(req.From) == 0 || len(req.Message) == 0 {
return nil, errors.New("The RoomId, From, and Message fields are all required.")
}
payload := url.Values{
"room_id": {req.RoomId},
"from": {req.From},
"message": {req.Message},
}
if req.Notify == true {
payload.Add("notify", "1")
}
if len(req.Color) > 0 {
payload.Add("color", req.Color)
}
if len(req.MessageFormat) > 0 {
payload.Add("message_format", req.MessageFormat)
}
return payload, nil
}
func (c *Client) PostMessage(req MessageRequest) error {
uri := fmt.Sprintf("%s/rooms/message?auth_token=%s", baseURL, url.QueryEscape(c.AuthToken))
payload, err := urlValuesFromMessageRequest(req)
if err != nil {
return err
}
resp, err := http.PostForm(uri, payload)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
msgResp := &struct{ Status string }{}
if err := json.Unmarshal(body, msgResp); err != nil {
return err
}
if msgResp.Status != ResponseStatusSent {
return errors.New("PostMessage: response 'status' field was not 'sent'.")
}
return nil
}
func (c *Client) RoomHistory(id, date, tz string) ([]Message, error) {
uri := fmt.Sprintf("%s/rooms/history?auth_token=%s&room_id=%s&date=%s&timezone=%s",
baseURL, url.QueryEscape(c.AuthToken), url.QueryEscape(id), url.QueryEscape(date), url.QueryEscape(tz))
resp, err := http.Get(uri)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
var errResp ErrorResponse
if err := json.Unmarshal(body, &errResp); err != nil {
return nil, err
}
return nil, errors.New(errResp.Error.Message)
}
msgResp := &struct{ Messages []Message }{}
if err := json.Unmarshal(body, msgResp); err != nil {
return nil, err
}
return msgResp.Messages, nil
}
func (c *Client) RoomList() ([]Room, error) {
uri := fmt.Sprintf("%s/rooms/list?auth_token=%s", baseURL, url.QueryEscape(c.AuthToken))
resp, err := http.Get(uri)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
var errResp ErrorResponse
if err := json.Unmarshal(body, &errResp); err != nil {
return nil, err
}
return nil, errors.New(errResp.Error.Message)
}
roomsResp := &struct{ Rooms []Room }{}
if err := json.Unmarshal(body, roomsResp); err != nil {
return nil, err
}
return roomsResp.Rooms, nil
}