-
Notifications
You must be signed in to change notification settings - Fork 17
/
chat.go
312 lines (257 loc) · 7.9 KB
/
chat.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package gorocket
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
)
type Message struct {
Alias string `json:"alias,omitempty"`
Avatar string `json:"avatar,omitempty"`
Channel string `json:"channel,omitempty"`
Emoji string `json:"emoji,omitempty"`
RoomID string `json:"roomId,omitempty"`
Text string `json:"text"`
Attachments []Attachment `json:"attachments"`
}
type Attachment struct {
AudioURL string `json:"audio_url,omitempty"`
AuthorIcon string `json:"author_icon,omitempty"`
AuthorLink string `json:"author_link,omitempty"`
AuthorName string `json:"author_name,omitempty"`
Collapsed bool `json:"collapsed,omitempty"`
Color string `json:"color,omitempty"`
Fields []AttachField `json:"fields,omitempty"`
ImageURL string `json:"image_url,omitempty"`
MessageLink string `json:"message_link,omitempty"`
Text string `json:"text,omitempty"`
ThumbURL string `json:"thumb_url,omitempty"`
Title string `json:"title,omitempty"`
TitleLink string `json:"title_link,omitempty"`
TitleLinkDownload bool `json:"title_link_download,omitempty"`
Ts time.Time `json:"ts,omitempty"`
VideoURL string `json:"video_url,omitempty"`
}
type AttachField struct {
Short bool `json:"short,omitempty"`
Title string `json:"title,omitempty"`
Value string `json:"value,omitempty"`
}
type RespPostMessage struct {
Ts int64 `json:"ts"`
Channel string `json:"channel"`
Message RespMessageData `json:"message"`
Success bool `json:"success"`
Error string `json:"error,omitempty"`
ErrorType string `json:"errorType,omitempty"`
}
type RespMessageData struct {
Alias string `json:"alias,omitempty"`
Msg string `json:"msg,omitempty"`
ParseUrls bool `json:"parseUrls,omitempty"`
Groupable bool `json:"groupable,omitempty"`
Ts time.Time `json:"ts,omitempty"`
U UChat `json:"u,omitempty"`
Rid string `json:"rid,omitempty"`
UpdatedAt time.Time `json:"_updatedAt,omitempty"`
ID string `json:"_id,omitempty"`
}
type UChat struct {
ID string `json:"_id,omitempty"`
Username string `json:"username,omitempty"`
}
type SingleMessageId struct {
MessageId string `json:"messageId"`
}
type GetMessageResponse struct {
Message MessageResp `json:"message"`
Success bool `json:"success"`
}
type MessageResp struct {
ID string `json:"_id"`
Rid string `json:"rid"`
Msg string `json:"msg"`
Ts time.Time `json:"ts"`
U struct {
ID string `json:"_id"`
Username string `json:"username"`
} `json:"u"`
}
type DeleteMessageRequest struct {
RoomID string `json:"roomId"`
MsgID string `json:"msgId"`
AsUser bool `json:"asUser,omitempty"`
}
// todo add the remaining fields
type DeleteMessageResponse struct {
ID string `json:"_id"`
Ts int64 `json:"ts"`
Success bool `json:"success"`
}
type GetPinnedMsgRequest struct {
RoomId string
Count int
Offset int
}
type GetPinnedMsgResponse struct {
Messages []PinnedMessage `json:"messages"`
Count int `json:"count"`
Offset int `json:"offset"`
Total int `json:"total"`
Success bool `json:"success"`
}
type PinnedMessage struct {
ID string `json:"_id"`
Rid string `json:"rid"`
Msg string `json:"msg"`
Ts time.Time `json:"ts"`
U struct {
ID string `json:"_id"`
Username string `json:"username"`
Name string `json:"name"`
} `json:"u"`
Mentions []interface{} `json:"mentions"`
Channels []interface{} `json:"channels"`
UpdatedAt time.Time `json:"_updatedAt"`
Pinned bool `json:"pinned"`
PinnedAt time.Time `json:"pinnedAt"`
PinnedBy struct {
ID string `json:"_id"`
Username string `json:"username"`
} `json:"pinnedBy"`
}
type PinMessageResponse struct {
Message struct {
T string `json:"t"`
Rid string `json:"rid"`
Ts time.Time `json:"ts"`
Msg string `json:"msg"`
U struct {
ID string `json:"_id"`
Username string `json:"username"`
} `json:"u"`
Groupable bool `json:"groupable"`
Attachments []struct {
Text string `json:"text"`
AuthorName string `json:"author_name"`
AuthorIcon string `json:"author_icon"`
Ts time.Time `json:"ts"`
} `json:"attachments"`
UpdatedAt time.Time `json:"_updatedAt"`
ID string `json:"_id"`
} `json:"message"`
Success bool `json:"success"`
}
// PostMessage posts a new chat message.
func (c *Client) PostMessage(msg *Message) (*RespPostMessage, error) {
opt, _ := json.Marshal(msg)
req, err := http.NewRequest("POST",
fmt.Sprintf("%s/%s/chat.postMessage", c.baseURL, c.apiVersion),
bytes.NewBuffer(opt))
if err != nil {
return nil, err
}
res := RespPostMessage{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
// GetMessage retrieves a single chat message by the provided id.
// Callee must have permission to access the room where the message resides.
func (c *Client) GetMessage(param *SingleMessageId) (*GetMessageResponse, error) {
req, err := http.NewRequest("GET",
fmt.Sprintf("%s/%s/chat.getMessage", c.baseURL, c.apiVersion),
nil)
if param.MessageId == "" {
return nil, fmt.Errorf("false parameters")
}
url := req.URL.Query()
if param.MessageId != "" {
url.Add("msgId", param.MessageId)
}
req.URL.RawQuery = url.Encode()
if err != nil {
return nil, err
}
res := GetMessageResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
// DeleteMessage deletes an existing chat message.
func (c *Client) DeleteMessage(param *DeleteMessageRequest) (*DeleteMessageResponse, error) {
opt, _ := json.Marshal(param)
req, err := http.NewRequest("POST",
fmt.Sprintf("%s/%s/chat.delete", c.baseURL, c.apiVersion),
bytes.NewBuffer(opt))
if err != nil {
return nil, err
}
res := DeleteMessageResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
// GetPinnedMessages retrieve pinned messages from a room.
func (c *Client) GetPinnedMessages(param *GetPinnedMsgRequest) (*GetPinnedMsgResponse, error) {
req, err := http.NewRequest("GET",
fmt.Sprintf("%s/%s/chat.getPinnedMessages", c.baseURL, c.apiVersion),
nil)
if param.RoomId == "" {
return nil, fmt.Errorf("false parameters")
}
url := req.URL.Query()
if param.RoomId != "" {
url.Add("roomId", param.RoomId)
}
if param.Offset != 0 {
url.Add("offset", strconv.Itoa(param.Offset))
}
if param.Count != 0 {
url.Add("count", strconv.Itoa(param.Count))
}
req.URL.RawQuery = url.Encode()
if err != nil {
return nil, err
}
res := GetPinnedMsgResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
// PinMessage pins a chat message to the message's channel.
func (c *Client) PinMessage(param *SingleMessageId) (*PinMessageResponse, error) {
opt, _ := json.Marshal(param)
req, err := http.NewRequest("POST",
fmt.Sprintf("%s/%s/chat.pinMessage", c.baseURL, c.apiVersion),
bytes.NewBuffer(opt))
if err != nil {
return nil, err
}
res := PinMessageResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
// UnpinMessage unpins a chat message to the message's channel.
func (c *Client) UnpinMessage(param *SingleMessageId) (*SimpleSuccessResponse, error) {
opt, _ := json.Marshal(param)
req, err := http.NewRequest("POST",
fmt.Sprintf("%s/%s/chat.unPinMessage", c.baseURL, c.apiVersion),
bytes.NewBuffer(opt))
if err != nil {
return nil, err
}
res := SimpleSuccessResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}