/
messages_api.go
371 lines (302 loc) · 13.1 KB
/
messages_api.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package webexteams
import (
"errors"
"fmt"
"io"
"strings"
"time"
"github.com/go-resty/resty/v2"
"github.com/google/go-querystring/query"
"github.com/peterhellberg/link"
)
// MessagesService is the service to communicate with the Messages API endpoint
type MessagesService service
// Attachment is the object to manage attachments in messages
type Attachment struct {
Content map[string]interface{} `json:"content"`
ContentType string `json:"contentType"`
}
// File is the struct used to define a file that needs to be sent. A file can either be a remote URI
// or an io.Reader. If RemoteFileURI is set, it takes precedence over the Reader.
type File struct {
Name string `json:"fileName,omitempty"` // File Name.
Reader io.Reader `json:"fileReader,omitempty"` // File io.Reader.
ContentType string `json:"contentType,omitempty"` // File Content Type.
RemoteFileURI string `json:"remoteFileURI,omitempty"` // Remote file URI.
}
// MessageCreateRequest is the Create Message Request Parameters
type MessageCreateRequest struct {
RoomID string `json:"roomId,omitempty"` // Room ID.
ParentID string `json:"parentId,omitempty"` // Parent ID
ToPersonID string `json:"toPersonId,omitempty"` // Person ID (for type=direct).
ToPersonEmail string `json:"toPersonEmail,omitempty"` // Person email (for type=direct).
Text string `json:"text,omitempty"` // Message in plain text format.
Markdown string `json:"markdown,omitempty"` // Message in markdown format.
Files []File `json:"files,omitempty"` // File URL array.
Attachments []Attachment `json:"attachments,omitempty"` // Attachment Array
}
// MessageEditRequest is the Edit Message Request Parameters
type MessageEditRequest struct {
RoomID string `json:"roomId,omitempty"` // Room ID.
Text string `json:"text,omitempty"` // Message in plain text format.
Markdown string `json:"markdown,omitempty"` // Message in markdown format.
Attachments []Attachment `json:"attachments,omitempty"` // Attachment Array
}
// Message is the Message definition
type Message struct {
ID string `json:"id,omitempty"` // Message ID.
RoomID string `json:"roomId,omitempty"` // Room ID.
ParentID string `json:"parentId,omitempty"` // Parent ID
RoomType string `json:"roomType,omitempty"` // Room type (group or direct).
ToPersonID string `json:"toPersonId,omitempty"` // Person ID (for type=direct).
ToPersonEmail string `json:"toPersonEmail,omitempty"` // Person email (for type=direct).
Text string `json:"text,omitempty"` // Message in plain text format.
Markdown string `json:"markdown,omitempty"` // Message in markdown format.
HTML string `json:"html,omitempty"` // Message in HTML format.
Files []string `json:"files,omitempty"` // File URL array.
PersonID string `json:"personId,omitempty"` // Person ID.
PersonEmail string `json:"personEmail,omitempty"` // Person Email.
Created time.Time `json:"created,omitempty"` // Message creation date/time.
MentionedPeople []string `json:"mentionedPeople,omitempty"` // Person ID array.
MentionedGroups []string `json:"mentionedGroups,omitempty"` // Groups array.
Attachments []Attachment `json:"attachments,omitempty"` // Attachment array
}
// Messages is the List of Messages
type Messages struct {
Items []Message `json:"items,omitempty"`
}
// AddMessage is used to append a message to a slice of messages
func (messages *Messages) AddMessage(item Message) []Message {
messages.Items = append(messages.Items, item)
return messages.Items
}
func (s *MessagesService) messagesPagination(linkHeader string, size, max int) *Messages {
items := &Messages{}
for _, l := range link.Parse(linkHeader) {
if l.Rel == "next" {
response, err := s.client.R().
SetResult(&Messages{}).
SetError(&Error{}).
Get(l.URI)
if err != nil {
return nil
}
items = response.Result().(*Messages)
if size != 0 {
size = size + len(items.Items)
if size < max {
messages := s.messagesPagination(response.Header().Get("Link"), size, max)
for _, message := range messages.Items {
items.AddMessage(message)
}
}
} else {
messages := s.messagesPagination(response.Header().Get("Link"), size, max)
for _, message := range messages.Items {
items.AddMessage(message)
}
}
}
}
return items
}
// CreateMessage Post a plain text or rich text message, and optionally, a media content attachment, to a room.
/* Post a plain text or rich text message, and optionally, a media content attachment, to a room.
The files parameter is an array, which accepts multiple values to allow for future expansion, but currently only one file may be included with the message.
@param messageCreateRequest
@return Message
*/
func (s *MessagesService) CreateMessage(messageCreateRequest *MessageCreateRequest) (*Message, *resty.Response, error) {
path := "/messages/"
responsePart := s.client.R()
if len(messageCreateRequest.Attachments) <= 0 {
if messageCreateRequest.RoomID != "" {
responsePart.SetMultipartFormData(map[string]string{"roomId": messageCreateRequest.RoomID})
}
if messageCreateRequest.ParentID != "" {
responsePart.SetMultipartFormData(map[string]string{"parentId": messageCreateRequest.ParentID})
}
if messageCreateRequest.Markdown != "" {
responsePart.SetMultipartFormData(map[string]string{"markdown": messageCreateRequest.Markdown})
}
if messageCreateRequest.Text != "" {
responsePart.SetMultipartFormData(map[string]string{"text": messageCreateRequest.Text})
}
if messageCreateRequest.ToPersonEmail != "" {
responsePart.SetMultipartFormData(map[string]string{"toPersonEmail": messageCreateRequest.ToPersonEmail})
}
if messageCreateRequest.ToPersonID != "" {
responsePart.SetMultipartFormData(map[string]string{"toPersonId": messageCreateRequest.ToPersonID})
}
if len(messageCreateRequest.Files) > 1 {
return nil, nil, errors.New("multi file attachment is not supported yet")
}
for _, fileToSend := range messageCreateRequest.Files {
if fileToSend.RemoteFileURI != "" {
responsePart.SetMultipartFormData(map[string]string{"files": fileToSend.RemoteFileURI})
} else if fileToSend.Reader != nil {
responsePart.SetMultipartField("files", fileToSend.Name, fileToSend.ContentType, fileToSend.Reader)
}
}
} else {
if len(messageCreateRequest.Files) > 0 {
return nil, nil, errors.New("sending files with attachment is not supported yet")
}
responsePart.SetBody(messageCreateRequest)
}
response, err := responsePart.
SetResult(&Message{}).
SetError(&Error{}).
Post(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Message)
return result, response, err
}
// EditMessage Puts a plain text or rich text message, and optionally, a media content attachment, to a room.
/* Put a plain text or rich text message, and optionally, a media content attachment, to a room.
The files parameter is an array, which accepts multiple values to allow for future expansion, but currently only one file may be included with the message.
@param messageID Message ID.
@param messageCreateRequest
@return Message
*/
func (s *MessagesService) EditMessage(messageID string, messageEditRequest *MessageEditRequest) (*Message, *resty.Response, error) {
path := "/messages/{messageId}"
path = strings.Replace(path, "{"+"messageId"+"}", fmt.Sprintf("%v", messageID), -1)
response, err := s.client.R().
SetBody(messageEditRequest).
SetResult(&Message{}).
SetError(&Error{}).
Put(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Message)
return result, response, err
}
// DeleteMessage Delete a Message.
/* Deletes a message by ID.
@param messageID Message ID.
@return
*/
func (s *MessagesService) DeleteMessage(messageID string) (*resty.Response, error) {
path := "/messages/{messageId}"
path = strings.Replace(path, "{"+"messageId"+"}", fmt.Sprintf("%v", messageID), -1)
response, err := s.client.R().
SetError(&Error{}).
Delete(path)
if err != nil {
return nil, err
}
return response, err
}
// GetMessage Shows details for a message, by message ID.
/* Shows details for a message, by message ID.
Specify the message ID in the messageID parameter in the URI.
@param messageID Message ID.
@return Message
*/
func (s *MessagesService) GetMessage(messageID string) (*Message, *resty.Response, error) {
path := "/messages/{messageId}"
path = strings.Replace(path, "{"+"messageId"+"}", fmt.Sprintf("%v", messageID), -1)
response, err := s.client.R().
SetResult(&Message{}).
SetError(&Error{}).
Get(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Message)
return result, response, err
}
// ListMessagesQueryParams are the query params for the ListMessages API Call
type ListMessagesQueryParams struct {
RoomID string `url:"roomId,omitempty"` // List messages for a room, by ID.
MentionedPeople string `url:"mentionedPeople,omitempty"` // List messages where the caller is mentioned by specifying *me* or the caller personId.
Before time.Time `url:"before,omitempty"` // List messages sent before a date and time, in ISO8601 format. Format: yyyy-MM-dd'T'HH:mm:ss.SSSZ
BeforeMessage string `url:"beforeMessage,omitempty"` // List messages sent before a message, by ID.
Max int `url:"max,omitempty"` // Limit the maximum number of items in the response.
Paginate bool // Indicates if pagination is needed
}
// ListMessages Lists all messages in a room. Each message will include content attachments if present.
/* Lists all messages in a room. Each message will include content attachments if present.
The list sorts the messages in descending order by creation date.
Long result sets will be split into pages.
@param roomID List messages for a room, by ID.
@param "mentionedPeople" (string) List messages where the caller is mentioned by specifying *me* or the caller personId.
@param "before" (time.Time) List messages sent before a date and time, in ISO8601 format. Format: yyyy-MM-dd'T'HH:mm:ss.SSSZ
@param "beforeMessage" (string) List messages sent before a message, by ID.
@param "max" (int) Limit the maximum number of items in the response.
@param "paginate" (bool) Indicates if pagination is needed
@return Messages
*/
func (s *MessagesService) ListMessages(queryParams *ListMessagesQueryParams) (*Messages, *resty.Response, error) {
path := "/messages/"
queryParamsString, _ := query.Values(queryParams)
response, err := s.client.R().
SetQueryString(queryParamsString.Encode()).
SetResult(&Messages{}).
SetError(&Error{}).
Get(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Messages)
if queryParams.Paginate {
items := s.messagesPagination(response.Header().Get("Link"), 0, 0)
for _, message := range items.Items {
result.AddMessage(message)
}
} else {
if len(result.Items) < queryParams.Max {
items := s.messagesPagination(response.Header().Get("Link"), len(result.Items), queryParams.Max)
for _, message := range items.Items {
result.AddMessage(message)
}
}
}
return result, response, err
}
// DirectMessagesQueryParams are the query params for the ListMessages API Call
type DirectMessagesQueryParams struct {
ParentID string `url:"parentId,omitempty"` // List messages with a parent, by ID.
PersonID string `url:"personId,omitempty"` // List messages in a 1:1 room, by person ID.
PersonEmail string `url:"personEmail,omitempty"` // List messages in a 1:1 room, by person email.
Max int `url:"max,omitempty"` // Limit the maximum number of items in the response.
Paginate bool // Indicates if pagination is needed
}
// GetDirectMessages Lists all messages in a 1:1 (direct) room.
/* Lists all messages in a 1:1 (direct) room.
Use the personId or personEmail query parameter to specify the room.
@param parentId Parent Message ID.
@param personId Person ID.
@param personEmail Person Email.
@return a list of Messages
*/
func (s *MessagesService) GetDirectMessages(queryParams *DirectMessagesQueryParams) (*Messages, *resty.Response, error) {
path := "/messages/direct"
queryParamsString, _ := query.Values(queryParams)
response, err := s.client.R().
SetQueryString(queryParamsString.Encode()).
SetResult(&Messages{}).
Get(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Messages)
if queryParams.Paginate == true {
items := s.messagesPagination(response.Header().Get("Link"), 0, 0)
for _, message := range items.Items {
result.AddMessage(message)
}
} else {
if len(result.Items) < queryParams.Max {
items := s.messagesPagination(response.Header().Get("Link"), len(result.Items), queryParams.Max)
for _, message := range items.Items {
result.AddMessage(message)
}
}
}
return result, response, err
}