github.com/twilio/twilio-go@v1.20.1/rest/conversations/v1/services_conversations_messages.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Conversations 8 * This is the public Twilio REST API. 9 * 10 * NOTE: This class is auto generated by OpenAPI Generator. 11 * https://openapi-generator.tech 12 * Do not edit the class manually. 13 */ 14 15 package openapi 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "net/url" 21 "strings" 22 "time" 23 24 "github.com/twilio/twilio-go/client" 25 ) 26 27 // Optional parameters for the method 'CreateServiceConversationMessage' 28 type CreateServiceConversationMessageParams struct { 29 // The X-Twilio-Webhook-Enabled HTTP request header 30 XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"` 31 // The channel specific identifier of the message's author. Defaults to `system`. 32 Author *string `json:"Author,omitempty"` 33 // The content of the message, can be up to 1,600 characters long. 34 Body *string `json:"Body,omitempty"` 35 // The date that this resource was created. 36 DateCreated *time.Time `json:"DateCreated,omitempty"` 37 // The date that this resource was last updated. `null` if the message has not been edited. 38 DateUpdated *time.Time `json:"DateUpdated,omitempty"` 39 // 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. 40 Attributes *string `json:"Attributes,omitempty"` 41 // The Media SID to be attached to the new Message. 42 MediaSid *string `json:"MediaSid,omitempty"` 43 // The unique ID of the multi-channel [Rich Content](https://www.twilio.com/docs/content) template, required for template-generated messages. **Note** that if this field is set, `Body` and `MediaSid` parameters are ignored. 44 ContentSid *string `json:"ContentSid,omitempty"` 45 // A structurally valid JSON string that contains values to resolve Rich Content template variables. 46 ContentVariables *string `json:"ContentVariables,omitempty"` 47 // The subject of the message, can be up to 256 characters long. 48 Subject *string `json:"Subject,omitempty"` 49 } 50 51 func (params *CreateServiceConversationMessageParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *CreateServiceConversationMessageParams { 52 params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled 53 return params 54 } 55 func (params *CreateServiceConversationMessageParams) SetAuthor(Author string) *CreateServiceConversationMessageParams { 56 params.Author = &Author 57 return params 58 } 59 func (params *CreateServiceConversationMessageParams) SetBody(Body string) *CreateServiceConversationMessageParams { 60 params.Body = &Body 61 return params 62 } 63 func (params *CreateServiceConversationMessageParams) SetDateCreated(DateCreated time.Time) *CreateServiceConversationMessageParams { 64 params.DateCreated = &DateCreated 65 return params 66 } 67 func (params *CreateServiceConversationMessageParams) SetDateUpdated(DateUpdated time.Time) *CreateServiceConversationMessageParams { 68 params.DateUpdated = &DateUpdated 69 return params 70 } 71 func (params *CreateServiceConversationMessageParams) SetAttributes(Attributes string) *CreateServiceConversationMessageParams { 72 params.Attributes = &Attributes 73 return params 74 } 75 func (params *CreateServiceConversationMessageParams) SetMediaSid(MediaSid string) *CreateServiceConversationMessageParams { 76 params.MediaSid = &MediaSid 77 return params 78 } 79 func (params *CreateServiceConversationMessageParams) SetContentSid(ContentSid string) *CreateServiceConversationMessageParams { 80 params.ContentSid = &ContentSid 81 return params 82 } 83 func (params *CreateServiceConversationMessageParams) SetContentVariables(ContentVariables string) *CreateServiceConversationMessageParams { 84 params.ContentVariables = &ContentVariables 85 return params 86 } 87 func (params *CreateServiceConversationMessageParams) SetSubject(Subject string) *CreateServiceConversationMessageParams { 88 params.Subject = &Subject 89 return params 90 } 91 92 // Add a new message to the conversation in a specific service 93 func (c *ApiService) CreateServiceConversationMessage(ChatServiceSid string, ConversationSid string, params *CreateServiceConversationMessageParams) (*ConversationsV1ServiceConversationMessage, error) { 94 path := "/v1/Services/{ChatServiceSid}/Conversations/{ConversationSid}/Messages" 95 path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1) 96 path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1) 97 98 data := url.Values{} 99 headers := make(map[string]interface{}) 100 101 if params != nil && params.Author != nil { 102 data.Set("Author", *params.Author) 103 } 104 if params != nil && params.Body != nil { 105 data.Set("Body", *params.Body) 106 } 107 if params != nil && params.DateCreated != nil { 108 data.Set("DateCreated", fmt.Sprint((*params.DateCreated).Format(time.RFC3339))) 109 } 110 if params != nil && params.DateUpdated != nil { 111 data.Set("DateUpdated", fmt.Sprint((*params.DateUpdated).Format(time.RFC3339))) 112 } 113 if params != nil && params.Attributes != nil { 114 data.Set("Attributes", *params.Attributes) 115 } 116 if params != nil && params.MediaSid != nil { 117 data.Set("MediaSid", *params.MediaSid) 118 } 119 if params != nil && params.ContentSid != nil { 120 data.Set("ContentSid", *params.ContentSid) 121 } 122 if params != nil && params.ContentVariables != nil { 123 data.Set("ContentVariables", *params.ContentVariables) 124 } 125 if params != nil && params.Subject != nil { 126 data.Set("Subject", *params.Subject) 127 } 128 129 if params != nil && params.XTwilioWebhookEnabled != nil { 130 headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled 131 } 132 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 133 if err != nil { 134 return nil, err 135 } 136 137 defer resp.Body.Close() 138 139 ps := &ConversationsV1ServiceConversationMessage{} 140 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 141 return nil, err 142 } 143 144 return ps, err 145 } 146 147 // Optional parameters for the method 'DeleteServiceConversationMessage' 148 type DeleteServiceConversationMessageParams struct { 149 // The X-Twilio-Webhook-Enabled HTTP request header 150 XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"` 151 } 152 153 func (params *DeleteServiceConversationMessageParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *DeleteServiceConversationMessageParams { 154 params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled 155 return params 156 } 157 158 // Remove a message from the conversation 159 func (c *ApiService) DeleteServiceConversationMessage(ChatServiceSid string, ConversationSid string, Sid string, params *DeleteServiceConversationMessageParams) error { 160 path := "/v1/Services/{ChatServiceSid}/Conversations/{ConversationSid}/Messages/{Sid}" 161 path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1) 162 path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1) 163 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 164 165 data := url.Values{} 166 headers := make(map[string]interface{}) 167 168 if params != nil && params.XTwilioWebhookEnabled != nil { 169 headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled 170 } 171 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 172 if err != nil { 173 return err 174 } 175 176 defer resp.Body.Close() 177 178 return nil 179 } 180 181 // Fetch a message from the conversation 182 func (c *ApiService) FetchServiceConversationMessage(ChatServiceSid string, ConversationSid string, Sid string) (*ConversationsV1ServiceConversationMessage, error) { 183 path := "/v1/Services/{ChatServiceSid}/Conversations/{ConversationSid}/Messages/{Sid}" 184 path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1) 185 path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1) 186 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 187 188 data := url.Values{} 189 headers := make(map[string]interface{}) 190 191 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 192 if err != nil { 193 return nil, err 194 } 195 196 defer resp.Body.Close() 197 198 ps := &ConversationsV1ServiceConversationMessage{} 199 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 200 return nil, err 201 } 202 203 return ps, err 204 } 205 206 // Optional parameters for the method 'ListServiceConversationMessage' 207 type ListServiceConversationMessageParams struct { 208 // The sort order of the returned messages. Can be: `asc` (ascending) or `desc` (descending), with `asc` as the default. 209 Order *string `json:"Order,omitempty"` 210 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 211 PageSize *int `json:"PageSize,omitempty"` 212 // Max number of records to return. 213 Limit *int `json:"limit,omitempty"` 214 } 215 216 func (params *ListServiceConversationMessageParams) SetOrder(Order string) *ListServiceConversationMessageParams { 217 params.Order = &Order 218 return params 219 } 220 func (params *ListServiceConversationMessageParams) SetPageSize(PageSize int) *ListServiceConversationMessageParams { 221 params.PageSize = &PageSize 222 return params 223 } 224 func (params *ListServiceConversationMessageParams) SetLimit(Limit int) *ListServiceConversationMessageParams { 225 params.Limit = &Limit 226 return params 227 } 228 229 // Retrieve a single page of ServiceConversationMessage records from the API. Request is executed immediately. 230 func (c *ApiService) PageServiceConversationMessage(ChatServiceSid string, ConversationSid string, params *ListServiceConversationMessageParams, pageToken, pageNumber string) (*ListServiceConversationMessageResponse, error) { 231 path := "/v1/Services/{ChatServiceSid}/Conversations/{ConversationSid}/Messages" 232 233 path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1) 234 path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1) 235 236 data := url.Values{} 237 headers := make(map[string]interface{}) 238 239 if params != nil && params.Order != nil { 240 data.Set("Order", *params.Order) 241 } 242 if params != nil && params.PageSize != nil { 243 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 244 } 245 246 if pageToken != "" { 247 data.Set("PageToken", pageToken) 248 } 249 if pageNumber != "" { 250 data.Set("Page", pageNumber) 251 } 252 253 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 254 if err != nil { 255 return nil, err 256 } 257 258 defer resp.Body.Close() 259 260 ps := &ListServiceConversationMessageResponse{} 261 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 262 return nil, err 263 } 264 265 return ps, err 266 } 267 268 // Lists ServiceConversationMessage records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 269 func (c *ApiService) ListServiceConversationMessage(ChatServiceSid string, ConversationSid string, params *ListServiceConversationMessageParams) ([]ConversationsV1ServiceConversationMessage, error) { 270 response, errors := c.StreamServiceConversationMessage(ChatServiceSid, ConversationSid, params) 271 272 records := make([]ConversationsV1ServiceConversationMessage, 0) 273 for record := range response { 274 records = append(records, record) 275 } 276 277 if err := <-errors; err != nil { 278 return nil, err 279 } 280 281 return records, nil 282 } 283 284 // Streams ServiceConversationMessage records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 285 func (c *ApiService) StreamServiceConversationMessage(ChatServiceSid string, ConversationSid string, params *ListServiceConversationMessageParams) (chan ConversationsV1ServiceConversationMessage, chan error) { 286 if params == nil { 287 params = &ListServiceConversationMessageParams{} 288 } 289 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 290 291 recordChannel := make(chan ConversationsV1ServiceConversationMessage, 1) 292 errorChannel := make(chan error, 1) 293 294 response, err := c.PageServiceConversationMessage(ChatServiceSid, ConversationSid, params, "", "") 295 if err != nil { 296 errorChannel <- err 297 close(recordChannel) 298 close(errorChannel) 299 } else { 300 go c.streamServiceConversationMessage(response, params, recordChannel, errorChannel) 301 } 302 303 return recordChannel, errorChannel 304 } 305 306 func (c *ApiService) streamServiceConversationMessage(response *ListServiceConversationMessageResponse, params *ListServiceConversationMessageParams, recordChannel chan ConversationsV1ServiceConversationMessage, errorChannel chan error) { 307 curRecord := 1 308 309 for response != nil { 310 responseRecords := response.Messages 311 for item := range responseRecords { 312 recordChannel <- responseRecords[item] 313 curRecord += 1 314 if params.Limit != nil && *params.Limit < curRecord { 315 close(recordChannel) 316 close(errorChannel) 317 return 318 } 319 } 320 321 record, err := client.GetNext(c.baseURL, response, c.getNextListServiceConversationMessageResponse) 322 if err != nil { 323 errorChannel <- err 324 break 325 } else if record == nil { 326 break 327 } 328 329 response = record.(*ListServiceConversationMessageResponse) 330 } 331 332 close(recordChannel) 333 close(errorChannel) 334 } 335 336 func (c *ApiService) getNextListServiceConversationMessageResponse(nextPageUrl string) (interface{}, error) { 337 if nextPageUrl == "" { 338 return nil, nil 339 } 340 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 341 if err != nil { 342 return nil, err 343 } 344 345 defer resp.Body.Close() 346 347 ps := &ListServiceConversationMessageResponse{} 348 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 349 return nil, err 350 } 351 return ps, nil 352 } 353 354 // Optional parameters for the method 'UpdateServiceConversationMessage' 355 type UpdateServiceConversationMessageParams struct { 356 // The X-Twilio-Webhook-Enabled HTTP request header 357 XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"` 358 // The channel specific identifier of the message's author. Defaults to `system`. 359 Author *string `json:"Author,omitempty"` 360 // The content of the message, can be up to 1,600 characters long. 361 Body *string `json:"Body,omitempty"` 362 // The date that this resource was created. 363 DateCreated *time.Time `json:"DateCreated,omitempty"` 364 // The date that this resource was last updated. `null` if the message has not been edited. 365 DateUpdated *time.Time `json:"DateUpdated,omitempty"` 366 // 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. 367 Attributes *string `json:"Attributes,omitempty"` 368 // The subject of the message, can be up to 256 characters long. 369 Subject *string `json:"Subject,omitempty"` 370 } 371 372 func (params *UpdateServiceConversationMessageParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *UpdateServiceConversationMessageParams { 373 params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled 374 return params 375 } 376 func (params *UpdateServiceConversationMessageParams) SetAuthor(Author string) *UpdateServiceConversationMessageParams { 377 params.Author = &Author 378 return params 379 } 380 func (params *UpdateServiceConversationMessageParams) SetBody(Body string) *UpdateServiceConversationMessageParams { 381 params.Body = &Body 382 return params 383 } 384 func (params *UpdateServiceConversationMessageParams) SetDateCreated(DateCreated time.Time) *UpdateServiceConversationMessageParams { 385 params.DateCreated = &DateCreated 386 return params 387 } 388 func (params *UpdateServiceConversationMessageParams) SetDateUpdated(DateUpdated time.Time) *UpdateServiceConversationMessageParams { 389 params.DateUpdated = &DateUpdated 390 return params 391 } 392 func (params *UpdateServiceConversationMessageParams) SetAttributes(Attributes string) *UpdateServiceConversationMessageParams { 393 params.Attributes = &Attributes 394 return params 395 } 396 func (params *UpdateServiceConversationMessageParams) SetSubject(Subject string) *UpdateServiceConversationMessageParams { 397 params.Subject = &Subject 398 return params 399 } 400 401 // Update an existing message in the conversation 402 func (c *ApiService) UpdateServiceConversationMessage(ChatServiceSid string, ConversationSid string, Sid string, params *UpdateServiceConversationMessageParams) (*ConversationsV1ServiceConversationMessage, error) { 403 path := "/v1/Services/{ChatServiceSid}/Conversations/{ConversationSid}/Messages/{Sid}" 404 path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1) 405 path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1) 406 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 407 408 data := url.Values{} 409 headers := make(map[string]interface{}) 410 411 if params != nil && params.Author != nil { 412 data.Set("Author", *params.Author) 413 } 414 if params != nil && params.Body != nil { 415 data.Set("Body", *params.Body) 416 } 417 if params != nil && params.DateCreated != nil { 418 data.Set("DateCreated", fmt.Sprint((*params.DateCreated).Format(time.RFC3339))) 419 } 420 if params != nil && params.DateUpdated != nil { 421 data.Set("DateUpdated", fmt.Sprint((*params.DateUpdated).Format(time.RFC3339))) 422 } 423 if params != nil && params.Attributes != nil { 424 data.Set("Attributes", *params.Attributes) 425 } 426 if params != nil && params.Subject != nil { 427 data.Set("Subject", *params.Subject) 428 } 429 430 if params != nil && params.XTwilioWebhookEnabled != nil { 431 headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled 432 } 433 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 434 if err != nil { 435 return nil, err 436 } 437 438 defer resp.Body.Close() 439 440 ps := &ConversationsV1ServiceConversationMessage{} 441 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 442 return nil, err 443 } 444 445 return ps, err 446 }