-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages_api.go
229 lines (185 loc) · 7.91 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
package webexteams
import (
"fmt"
"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"`
}
// MessageCreateRequest is the Create Message Request Parameters
type MessageCreateRequest struct {
RoomID string `json:"roomId,omitempty"` // Room 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 []string `json:"files,omitempty"` // File URL array.
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.
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 messagesPagination(linkHeader string, size, max int) *Messages {
items := &Messages{}
for _, l := range link.Parse(linkHeader) {
if l.Rel == "next" {
response, err := RestyClient.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 := messagesPagination(response.Header().Get("Link"), size, max)
for _, message := range messages.Items {
items.AddMessage(message)
}
}
} else {
messages := 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/"
response, err := RestyClient.R().
SetBody(messageCreateRequest).
SetResult(&Message{}).
SetError(&Error{}).
Post(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 := RestyClient.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 := RestyClient.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 := RestyClient.R().
SetQueryString(queryParamsString.Encode()).
SetResult(&Messages{}).
SetError(&Error{}).
Get(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Messages)
if queryParams.Paginate == true {
items := messagesPagination(response.Header().Get("Link"), 0, 0)
for _, message := range items.Items {
result.AddMessage(message)
}
} else {
if len(result.Items) < queryParams.Max {
items := messagesPagination(response.Header().Get("Link"), len(result.Items), queryParams.Max)
for _, message := range items.Items {
result.AddMessage(message)
}
}
}
return result, response, err
}