/
conversations_messages.go
426 lines (362 loc) · 14.9 KB
/
conversations_messages.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
* This code was generated by
* ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __
* | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/
* | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \
*
* Twilio - Conversations
* This is the public Twilio REST API.
*
* NOTE: This class is auto generated by OpenAPI Generator.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package openapi
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"time"
"github.com/twilio/twilio-go/client"
)
// Optional parameters for the method 'CreateConversationMessage'
type CreateConversationMessageParams struct {
// The X-Twilio-Webhook-Enabled HTTP request header
XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"`
// The channel specific identifier of the message's author. Defaults to `system`.
Author *string `json:"Author,omitempty"`
// The content of the message, can be up to 1,600 characters long.
Body *string `json:"Body,omitempty"`
// The date that this resource was created.
DateCreated *time.Time `json:"DateCreated,omitempty"`
// The date that this resource was last updated. `null` if the message has not been edited.
DateUpdated *time.Time `json:"DateUpdated,omitempty"`
// A string metadata field you can use to store any data you wish. The string value must contain structurally valid JSON if specified. **Note** that if the attributes are not set \\\"{}\\\" will be returned.
Attributes *string `json:"Attributes,omitempty"`
// The Media SID to be attached to the new Message.
MediaSid *string `json:"MediaSid,omitempty"`
// The unique ID of the multi-channel [Rich Content](https://www.twilio.com/docs/content-api) template, required for template-generated messages. **Note** that if this field is set, `Body` and `MediaSid` parameters are ignored.
ContentSid *string `json:"ContentSid,omitempty"`
// A structurally valid JSON string that contains values to resolve Rich Content template variables.
ContentVariables *string `json:"ContentVariables,omitempty"`
}
func (params *CreateConversationMessageParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *CreateConversationMessageParams {
params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled
return params
}
func (params *CreateConversationMessageParams) SetAuthor(Author string) *CreateConversationMessageParams {
params.Author = &Author
return params
}
func (params *CreateConversationMessageParams) SetBody(Body string) *CreateConversationMessageParams {
params.Body = &Body
return params
}
func (params *CreateConversationMessageParams) SetDateCreated(DateCreated time.Time) *CreateConversationMessageParams {
params.DateCreated = &DateCreated
return params
}
func (params *CreateConversationMessageParams) SetDateUpdated(DateUpdated time.Time) *CreateConversationMessageParams {
params.DateUpdated = &DateUpdated
return params
}
func (params *CreateConversationMessageParams) SetAttributes(Attributes string) *CreateConversationMessageParams {
params.Attributes = &Attributes
return params
}
func (params *CreateConversationMessageParams) SetMediaSid(MediaSid string) *CreateConversationMessageParams {
params.MediaSid = &MediaSid
return params
}
func (params *CreateConversationMessageParams) SetContentSid(ContentSid string) *CreateConversationMessageParams {
params.ContentSid = &ContentSid
return params
}
func (params *CreateConversationMessageParams) SetContentVariables(ContentVariables string) *CreateConversationMessageParams {
params.ContentVariables = &ContentVariables
return params
}
// Add a new message to the conversation
func (c *ApiService) CreateConversationMessage(ConversationSid string, params *CreateConversationMessageParams) (*ConversationsV1ConversationMessage, error) {
path := "/v1/Conversations/{ConversationSid}/Messages"
path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1)
data := url.Values{}
headers := make(map[string]interface{})
if params != nil && params.Author != nil {
data.Set("Author", *params.Author)
}
if params != nil && params.Body != nil {
data.Set("Body", *params.Body)
}
if params != nil && params.DateCreated != nil {
data.Set("DateCreated", fmt.Sprint((*params.DateCreated).Format(time.RFC3339)))
}
if params != nil && params.DateUpdated != nil {
data.Set("DateUpdated", fmt.Sprint((*params.DateUpdated).Format(time.RFC3339)))
}
if params != nil && params.Attributes != nil {
data.Set("Attributes", *params.Attributes)
}
if params != nil && params.MediaSid != nil {
data.Set("MediaSid", *params.MediaSid)
}
if params != nil && params.ContentSid != nil {
data.Set("ContentSid", *params.ContentSid)
}
if params != nil && params.ContentVariables != nil {
data.Set("ContentVariables", *params.ContentVariables)
}
if params != nil && params.XTwilioWebhookEnabled != nil {
headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled
}
resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
if err != nil {
return nil, err
}
defer resp.Body.Close()
ps := &ConversationsV1ConversationMessage{}
if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
return nil, err
}
return ps, err
}
// Optional parameters for the method 'DeleteConversationMessage'
type DeleteConversationMessageParams struct {
// The X-Twilio-Webhook-Enabled HTTP request header
XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"`
}
func (params *DeleteConversationMessageParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *DeleteConversationMessageParams {
params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled
return params
}
// Remove a message from the conversation
func (c *ApiService) DeleteConversationMessage(ConversationSid string, Sid string, params *DeleteConversationMessageParams) error {
path := "/v1/Conversations/{ConversationSid}/Messages/{Sid}"
path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1)
path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
data := url.Values{}
headers := make(map[string]interface{})
if params != nil && params.XTwilioWebhookEnabled != nil {
headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled
}
resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
// Fetch a message from the conversation
func (c *ApiService) FetchConversationMessage(ConversationSid string, Sid string) (*ConversationsV1ConversationMessage, error) {
path := "/v1/Conversations/{ConversationSid}/Messages/{Sid}"
path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1)
path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
data := url.Values{}
headers := make(map[string]interface{})
resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
if err != nil {
return nil, err
}
defer resp.Body.Close()
ps := &ConversationsV1ConversationMessage{}
if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
return nil, err
}
return ps, err
}
// Optional parameters for the method 'ListConversationMessage'
type ListConversationMessageParams struct {
// The sort order of the returned messages. Can be: `asc` (ascending) or `desc` (descending), with `asc` as the default.
Order *string `json:"Order,omitempty"`
// How many resources to return in each list page. The default is 50, and the maximum is 1000.
PageSize *int `json:"PageSize,omitempty"`
// Max number of records to return.
Limit *int `json:"limit,omitempty"`
}
func (params *ListConversationMessageParams) SetOrder(Order string) *ListConversationMessageParams {
params.Order = &Order
return params
}
func (params *ListConversationMessageParams) SetPageSize(PageSize int) *ListConversationMessageParams {
params.PageSize = &PageSize
return params
}
func (params *ListConversationMessageParams) SetLimit(Limit int) *ListConversationMessageParams {
params.Limit = &Limit
return params
}
// Retrieve a single page of ConversationMessage records from the API. Request is executed immediately.
func (c *ApiService) PageConversationMessage(ConversationSid string, params *ListConversationMessageParams, pageToken, pageNumber string) (*ListConversationMessageResponse, error) {
path := "/v1/Conversations/{ConversationSid}/Messages"
path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1)
data := url.Values{}
headers := make(map[string]interface{})
if params != nil && params.Order != nil {
data.Set("Order", *params.Order)
}
if params != nil && params.PageSize != nil {
data.Set("PageSize", fmt.Sprint(*params.PageSize))
}
if pageToken != "" {
data.Set("PageToken", pageToken)
}
if pageNumber != "" {
data.Set("Page", pageNumber)
}
resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
if err != nil {
return nil, err
}
defer resp.Body.Close()
ps := &ListConversationMessageResponse{}
if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
return nil, err
}
return ps, err
}
// Lists ConversationMessage records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
func (c *ApiService) ListConversationMessage(ConversationSid string, params *ListConversationMessageParams) ([]ConversationsV1ConversationMessage, error) {
response, errors := c.StreamConversationMessage(ConversationSid, params)
records := make([]ConversationsV1ConversationMessage, 0)
for record := range response {
records = append(records, record)
}
if err := <-errors; err != nil {
return nil, err
}
return records, nil
}
// Streams ConversationMessage records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
func (c *ApiService) StreamConversationMessage(ConversationSid string, params *ListConversationMessageParams) (chan ConversationsV1ConversationMessage, chan error) {
if params == nil {
params = &ListConversationMessageParams{}
}
params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
recordChannel := make(chan ConversationsV1ConversationMessage, 1)
errorChannel := make(chan error, 1)
response, err := c.PageConversationMessage(ConversationSid, params, "", "")
if err != nil {
errorChannel <- err
close(recordChannel)
close(errorChannel)
} else {
go c.streamConversationMessage(response, params, recordChannel, errorChannel)
}
return recordChannel, errorChannel
}
func (c *ApiService) streamConversationMessage(response *ListConversationMessageResponse, params *ListConversationMessageParams, recordChannel chan ConversationsV1ConversationMessage, errorChannel chan error) {
curRecord := 1
for response != nil {
responseRecords := response.Messages
for item := range responseRecords {
recordChannel <- responseRecords[item]
curRecord += 1
if params.Limit != nil && *params.Limit < curRecord {
close(recordChannel)
close(errorChannel)
return
}
}
record, err := client.GetNext(c.baseURL, response, c.getNextListConversationMessageResponse)
if err != nil {
errorChannel <- err
break
} else if record == nil {
break
}
response = record.(*ListConversationMessageResponse)
}
close(recordChannel)
close(errorChannel)
}
func (c *ApiService) getNextListConversationMessageResponse(nextPageUrl string) (interface{}, error) {
if nextPageUrl == "" {
return nil, nil
}
resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
ps := &ListConversationMessageResponse{}
if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
return nil, err
}
return ps, nil
}
// Optional parameters for the method 'UpdateConversationMessage'
type UpdateConversationMessageParams struct {
// The X-Twilio-Webhook-Enabled HTTP request header
XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"`
// The channel specific identifier of the message's author. Defaults to `system`.
Author *string `json:"Author,omitempty"`
// The content of the message, can be up to 1,600 characters long.
Body *string `json:"Body,omitempty"`
// The date that this resource was created.
DateCreated *time.Time `json:"DateCreated,omitempty"`
// The date that this resource was last updated. `null` if the message has not been edited.
DateUpdated *time.Time `json:"DateUpdated,omitempty"`
// A string metadata field you can use to store any data you wish. The string value must contain structurally valid JSON if specified. **Note** that if the attributes are not set \\\"{}\\\" will be returned.
Attributes *string `json:"Attributes,omitempty"`
}
func (params *UpdateConversationMessageParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *UpdateConversationMessageParams {
params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled
return params
}
func (params *UpdateConversationMessageParams) SetAuthor(Author string) *UpdateConversationMessageParams {
params.Author = &Author
return params
}
func (params *UpdateConversationMessageParams) SetBody(Body string) *UpdateConversationMessageParams {
params.Body = &Body
return params
}
func (params *UpdateConversationMessageParams) SetDateCreated(DateCreated time.Time) *UpdateConversationMessageParams {
params.DateCreated = &DateCreated
return params
}
func (params *UpdateConversationMessageParams) SetDateUpdated(DateUpdated time.Time) *UpdateConversationMessageParams {
params.DateUpdated = &DateUpdated
return params
}
func (params *UpdateConversationMessageParams) SetAttributes(Attributes string) *UpdateConversationMessageParams {
params.Attributes = &Attributes
return params
}
// Update an existing message in the conversation
func (c *ApiService) UpdateConversationMessage(ConversationSid string, Sid string, params *UpdateConversationMessageParams) (*ConversationsV1ConversationMessage, error) {
path := "/v1/Conversations/{ConversationSid}/Messages/{Sid}"
path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1)
path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
data := url.Values{}
headers := make(map[string]interface{})
if params != nil && params.Author != nil {
data.Set("Author", *params.Author)
}
if params != nil && params.Body != nil {
data.Set("Body", *params.Body)
}
if params != nil && params.DateCreated != nil {
data.Set("DateCreated", fmt.Sprint((*params.DateCreated).Format(time.RFC3339)))
}
if params != nil && params.DateUpdated != nil {
data.Set("DateUpdated", fmt.Sprint((*params.DateUpdated).Format(time.RFC3339)))
}
if params != nil && params.Attributes != nil {
data.Set("Attributes", *params.Attributes)
}
if params != nil && params.XTwilioWebhookEnabled != nil {
headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled
}
resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
if err != nil {
return nil, err
}
defer resp.Body.Close()
ps := &ConversationsV1ConversationMessage{}
if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
return nil, err
}
return ps, err
}