github.com/twilio/twilio-go@v1.20.1/rest/conversations/v1/conversations.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 'CreateConversation' 28 type CreateConversationParams struct { 29 // The X-Twilio-Webhook-Enabled HTTP request header 30 XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"` 31 // The human-readable name of this conversation, limited to 256 characters. Optional. 32 FriendlyName *string `json:"FriendlyName,omitempty"` 33 // An application-defined string that uniquely identifies the resource. It can be used to address the resource in place of the resource's `sid` in the URL. 34 UniqueName *string `json:"UniqueName,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. 38 DateUpdated *time.Time `json:"DateUpdated,omitempty"` 39 // The unique ID of the [Messaging Service](https://www.twilio.com/docs/messaging/api/service-resource) this conversation belongs to. 40 MessagingServiceSid *string `json:"MessagingServiceSid,omitempty"` 41 // An optional 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. 42 Attributes *string `json:"Attributes,omitempty"` 43 // 44 State *string `json:"State,omitempty"` 45 // ISO8601 duration when conversation will be switched to `inactive` state. Minimum value for this timer is 1 minute. 46 TimersInactive *string `json:"Timers.Inactive,omitempty"` 47 // ISO8601 duration when conversation will be switched to `closed` state. Minimum value for this timer is 10 minutes. 48 TimersClosed *string `json:"Timers.Closed,omitempty"` 49 // The default email address that will be used when sending outbound emails in this conversation. 50 BindingsEmailAddress *string `json:"Bindings.Email.Address,omitempty"` 51 // The default name that will be used when sending outbound emails in this conversation. 52 BindingsEmailName *string `json:"Bindings.Email.Name,omitempty"` 53 } 54 55 func (params *CreateConversationParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *CreateConversationParams { 56 params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled 57 return params 58 } 59 func (params *CreateConversationParams) SetFriendlyName(FriendlyName string) *CreateConversationParams { 60 params.FriendlyName = &FriendlyName 61 return params 62 } 63 func (params *CreateConversationParams) SetUniqueName(UniqueName string) *CreateConversationParams { 64 params.UniqueName = &UniqueName 65 return params 66 } 67 func (params *CreateConversationParams) SetDateCreated(DateCreated time.Time) *CreateConversationParams { 68 params.DateCreated = &DateCreated 69 return params 70 } 71 func (params *CreateConversationParams) SetDateUpdated(DateUpdated time.Time) *CreateConversationParams { 72 params.DateUpdated = &DateUpdated 73 return params 74 } 75 func (params *CreateConversationParams) SetMessagingServiceSid(MessagingServiceSid string) *CreateConversationParams { 76 params.MessagingServiceSid = &MessagingServiceSid 77 return params 78 } 79 func (params *CreateConversationParams) SetAttributes(Attributes string) *CreateConversationParams { 80 params.Attributes = &Attributes 81 return params 82 } 83 func (params *CreateConversationParams) SetState(State string) *CreateConversationParams { 84 params.State = &State 85 return params 86 } 87 func (params *CreateConversationParams) SetTimersInactive(TimersInactive string) *CreateConversationParams { 88 params.TimersInactive = &TimersInactive 89 return params 90 } 91 func (params *CreateConversationParams) SetTimersClosed(TimersClosed string) *CreateConversationParams { 92 params.TimersClosed = &TimersClosed 93 return params 94 } 95 func (params *CreateConversationParams) SetBindingsEmailAddress(BindingsEmailAddress string) *CreateConversationParams { 96 params.BindingsEmailAddress = &BindingsEmailAddress 97 return params 98 } 99 func (params *CreateConversationParams) SetBindingsEmailName(BindingsEmailName string) *CreateConversationParams { 100 params.BindingsEmailName = &BindingsEmailName 101 return params 102 } 103 104 // Create a new conversation in your account's default service 105 func (c *ApiService) CreateConversation(params *CreateConversationParams) (*ConversationsV1Conversation, error) { 106 path := "/v1/Conversations" 107 108 data := url.Values{} 109 headers := make(map[string]interface{}) 110 111 if params != nil && params.FriendlyName != nil { 112 data.Set("FriendlyName", *params.FriendlyName) 113 } 114 if params != nil && params.UniqueName != nil { 115 data.Set("UniqueName", *params.UniqueName) 116 } 117 if params != nil && params.DateCreated != nil { 118 data.Set("DateCreated", fmt.Sprint((*params.DateCreated).Format(time.RFC3339))) 119 } 120 if params != nil && params.DateUpdated != nil { 121 data.Set("DateUpdated", fmt.Sprint((*params.DateUpdated).Format(time.RFC3339))) 122 } 123 if params != nil && params.MessagingServiceSid != nil { 124 data.Set("MessagingServiceSid", *params.MessagingServiceSid) 125 } 126 if params != nil && params.Attributes != nil { 127 data.Set("Attributes", *params.Attributes) 128 } 129 if params != nil && params.State != nil { 130 data.Set("State", *params.State) 131 } 132 if params != nil && params.TimersInactive != nil { 133 data.Set("Timers.Inactive", *params.TimersInactive) 134 } 135 if params != nil && params.TimersClosed != nil { 136 data.Set("Timers.Closed", *params.TimersClosed) 137 } 138 if params != nil && params.BindingsEmailAddress != nil { 139 data.Set("Bindings.Email.Address", *params.BindingsEmailAddress) 140 } 141 if params != nil && params.BindingsEmailName != nil { 142 data.Set("Bindings.Email.Name", *params.BindingsEmailName) 143 } 144 145 if params != nil && params.XTwilioWebhookEnabled != nil { 146 headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled 147 } 148 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 149 if err != nil { 150 return nil, err 151 } 152 153 defer resp.Body.Close() 154 155 ps := &ConversationsV1Conversation{} 156 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 157 return nil, err 158 } 159 160 return ps, err 161 } 162 163 // Optional parameters for the method 'DeleteConversation' 164 type DeleteConversationParams struct { 165 // The X-Twilio-Webhook-Enabled HTTP request header 166 XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"` 167 } 168 169 func (params *DeleteConversationParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *DeleteConversationParams { 170 params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled 171 return params 172 } 173 174 // Remove a conversation from your account's default service 175 func (c *ApiService) DeleteConversation(Sid string, params *DeleteConversationParams) error { 176 path := "/v1/Conversations/{Sid}" 177 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 178 179 data := url.Values{} 180 headers := make(map[string]interface{}) 181 182 if params != nil && params.XTwilioWebhookEnabled != nil { 183 headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled 184 } 185 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 186 if err != nil { 187 return err 188 } 189 190 defer resp.Body.Close() 191 192 return nil 193 } 194 195 // Fetch a conversation from your account's default service 196 func (c *ApiService) FetchConversation(Sid string) (*ConversationsV1Conversation, error) { 197 path := "/v1/Conversations/{Sid}" 198 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 199 200 data := url.Values{} 201 headers := make(map[string]interface{}) 202 203 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 204 if err != nil { 205 return nil, err 206 } 207 208 defer resp.Body.Close() 209 210 ps := &ConversationsV1Conversation{} 211 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 212 return nil, err 213 } 214 215 return ps, err 216 } 217 218 // Optional parameters for the method 'ListConversation' 219 type ListConversationParams struct { 220 // Specifies the beginning of the date range for filtering Conversations based on their creation date. Conversations that were created on or after this date will be included in the results. The date must be in ISO8601 format, specifically starting at the beginning of the specified date (YYYY-MM-DDT00:00:00Z), for precise filtering. This parameter can be combined with other filters. If this filter is used, the returned list is sorted by latest conversation creation date in descending order. 221 StartDate *string `json:"StartDate,omitempty"` 222 // Defines the end of the date range for filtering conversations by their creation date. Only conversations that were created on or before this date will appear in the results. The date must be in ISO8601 format, specifically capturing up to the end of the specified date (YYYY-MM-DDT23:59:59Z), to ensure that conversations from the entire end day are included. This parameter can be combined with other filters. If this filter is used, the returned list is sorted by latest conversation creation date in descending order. 223 EndDate *string `json:"EndDate,omitempty"` 224 // State for sorting and filtering list of Conversations. Can be `active`, `inactive` or `closed` 225 State *string `json:"State,omitempty"` 226 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 227 PageSize *int `json:"PageSize,omitempty"` 228 // Max number of records to return. 229 Limit *int `json:"limit,omitempty"` 230 } 231 232 func (params *ListConversationParams) SetStartDate(StartDate string) *ListConversationParams { 233 params.StartDate = &StartDate 234 return params 235 } 236 func (params *ListConversationParams) SetEndDate(EndDate string) *ListConversationParams { 237 params.EndDate = &EndDate 238 return params 239 } 240 func (params *ListConversationParams) SetState(State string) *ListConversationParams { 241 params.State = &State 242 return params 243 } 244 func (params *ListConversationParams) SetPageSize(PageSize int) *ListConversationParams { 245 params.PageSize = &PageSize 246 return params 247 } 248 func (params *ListConversationParams) SetLimit(Limit int) *ListConversationParams { 249 params.Limit = &Limit 250 return params 251 } 252 253 // Retrieve a single page of Conversation records from the API. Request is executed immediately. 254 func (c *ApiService) PageConversation(params *ListConversationParams, pageToken, pageNumber string) (*ListConversationResponse, error) { 255 path := "/v1/Conversations" 256 257 data := url.Values{} 258 headers := make(map[string]interface{}) 259 260 if params != nil && params.StartDate != nil { 261 data.Set("StartDate", *params.StartDate) 262 } 263 if params != nil && params.EndDate != nil { 264 data.Set("EndDate", *params.EndDate) 265 } 266 if params != nil && params.State != nil { 267 data.Set("State", *params.State) 268 } 269 if params != nil && params.PageSize != nil { 270 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 271 } 272 273 if pageToken != "" { 274 data.Set("PageToken", pageToken) 275 } 276 if pageNumber != "" { 277 data.Set("Page", pageNumber) 278 } 279 280 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 281 if err != nil { 282 return nil, err 283 } 284 285 defer resp.Body.Close() 286 287 ps := &ListConversationResponse{} 288 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 289 return nil, err 290 } 291 292 return ps, err 293 } 294 295 // Lists Conversation records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 296 func (c *ApiService) ListConversation(params *ListConversationParams) ([]ConversationsV1Conversation, error) { 297 response, errors := c.StreamConversation(params) 298 299 records := make([]ConversationsV1Conversation, 0) 300 for record := range response { 301 records = append(records, record) 302 } 303 304 if err := <-errors; err != nil { 305 return nil, err 306 } 307 308 return records, nil 309 } 310 311 // Streams Conversation records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 312 func (c *ApiService) StreamConversation(params *ListConversationParams) (chan ConversationsV1Conversation, chan error) { 313 if params == nil { 314 params = &ListConversationParams{} 315 } 316 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 317 318 recordChannel := make(chan ConversationsV1Conversation, 1) 319 errorChannel := make(chan error, 1) 320 321 response, err := c.PageConversation(params, "", "") 322 if err != nil { 323 errorChannel <- err 324 close(recordChannel) 325 close(errorChannel) 326 } else { 327 go c.streamConversation(response, params, recordChannel, errorChannel) 328 } 329 330 return recordChannel, errorChannel 331 } 332 333 func (c *ApiService) streamConversation(response *ListConversationResponse, params *ListConversationParams, recordChannel chan ConversationsV1Conversation, errorChannel chan error) { 334 curRecord := 1 335 336 for response != nil { 337 responseRecords := response.Conversations 338 for item := range responseRecords { 339 recordChannel <- responseRecords[item] 340 curRecord += 1 341 if params.Limit != nil && *params.Limit < curRecord { 342 close(recordChannel) 343 close(errorChannel) 344 return 345 } 346 } 347 348 record, err := client.GetNext(c.baseURL, response, c.getNextListConversationResponse) 349 if err != nil { 350 errorChannel <- err 351 break 352 } else if record == nil { 353 break 354 } 355 356 response = record.(*ListConversationResponse) 357 } 358 359 close(recordChannel) 360 close(errorChannel) 361 } 362 363 func (c *ApiService) getNextListConversationResponse(nextPageUrl string) (interface{}, error) { 364 if nextPageUrl == "" { 365 return nil, nil 366 } 367 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 368 if err != nil { 369 return nil, err 370 } 371 372 defer resp.Body.Close() 373 374 ps := &ListConversationResponse{} 375 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 376 return nil, err 377 } 378 return ps, nil 379 } 380 381 // Optional parameters for the method 'UpdateConversation' 382 type UpdateConversationParams struct { 383 // The X-Twilio-Webhook-Enabled HTTP request header 384 XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"` 385 // The human-readable name of this conversation, limited to 256 characters. Optional. 386 FriendlyName *string `json:"FriendlyName,omitempty"` 387 // The date that this resource was created. 388 DateCreated *time.Time `json:"DateCreated,omitempty"` 389 // The date that this resource was last updated. 390 DateUpdated *time.Time `json:"DateUpdated,omitempty"` 391 // An optional 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. 392 Attributes *string `json:"Attributes,omitempty"` 393 // The unique ID of the [Messaging Service](https://www.twilio.com/docs/messaging/api/service-resource) this conversation belongs to. 394 MessagingServiceSid *string `json:"MessagingServiceSid,omitempty"` 395 // 396 State *string `json:"State,omitempty"` 397 // ISO8601 duration when conversation will be switched to `inactive` state. Minimum value for this timer is 1 minute. 398 TimersInactive *string `json:"Timers.Inactive,omitempty"` 399 // ISO8601 duration when conversation will be switched to `closed` state. Minimum value for this timer is 10 minutes. 400 TimersClosed *string `json:"Timers.Closed,omitempty"` 401 // An application-defined string that uniquely identifies the resource. It can be used to address the resource in place of the resource's `sid` in the URL. 402 UniqueName *string `json:"UniqueName,omitempty"` 403 // The default email address that will be used when sending outbound emails in this conversation. 404 BindingsEmailAddress *string `json:"Bindings.Email.Address,omitempty"` 405 // The default name that will be used when sending outbound emails in this conversation. 406 BindingsEmailName *string `json:"Bindings.Email.Name,omitempty"` 407 } 408 409 func (params *UpdateConversationParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *UpdateConversationParams { 410 params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled 411 return params 412 } 413 func (params *UpdateConversationParams) SetFriendlyName(FriendlyName string) *UpdateConversationParams { 414 params.FriendlyName = &FriendlyName 415 return params 416 } 417 func (params *UpdateConversationParams) SetDateCreated(DateCreated time.Time) *UpdateConversationParams { 418 params.DateCreated = &DateCreated 419 return params 420 } 421 func (params *UpdateConversationParams) SetDateUpdated(DateUpdated time.Time) *UpdateConversationParams { 422 params.DateUpdated = &DateUpdated 423 return params 424 } 425 func (params *UpdateConversationParams) SetAttributes(Attributes string) *UpdateConversationParams { 426 params.Attributes = &Attributes 427 return params 428 } 429 func (params *UpdateConversationParams) SetMessagingServiceSid(MessagingServiceSid string) *UpdateConversationParams { 430 params.MessagingServiceSid = &MessagingServiceSid 431 return params 432 } 433 func (params *UpdateConversationParams) SetState(State string) *UpdateConversationParams { 434 params.State = &State 435 return params 436 } 437 func (params *UpdateConversationParams) SetTimersInactive(TimersInactive string) *UpdateConversationParams { 438 params.TimersInactive = &TimersInactive 439 return params 440 } 441 func (params *UpdateConversationParams) SetTimersClosed(TimersClosed string) *UpdateConversationParams { 442 params.TimersClosed = &TimersClosed 443 return params 444 } 445 func (params *UpdateConversationParams) SetUniqueName(UniqueName string) *UpdateConversationParams { 446 params.UniqueName = &UniqueName 447 return params 448 } 449 func (params *UpdateConversationParams) SetBindingsEmailAddress(BindingsEmailAddress string) *UpdateConversationParams { 450 params.BindingsEmailAddress = &BindingsEmailAddress 451 return params 452 } 453 func (params *UpdateConversationParams) SetBindingsEmailName(BindingsEmailName string) *UpdateConversationParams { 454 params.BindingsEmailName = &BindingsEmailName 455 return params 456 } 457 458 // Update an existing conversation in your account's default service 459 func (c *ApiService) UpdateConversation(Sid string, params *UpdateConversationParams) (*ConversationsV1Conversation, error) { 460 path := "/v1/Conversations/{Sid}" 461 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 462 463 data := url.Values{} 464 headers := make(map[string]interface{}) 465 466 if params != nil && params.FriendlyName != nil { 467 data.Set("FriendlyName", *params.FriendlyName) 468 } 469 if params != nil && params.DateCreated != nil { 470 data.Set("DateCreated", fmt.Sprint((*params.DateCreated).Format(time.RFC3339))) 471 } 472 if params != nil && params.DateUpdated != nil { 473 data.Set("DateUpdated", fmt.Sprint((*params.DateUpdated).Format(time.RFC3339))) 474 } 475 if params != nil && params.Attributes != nil { 476 data.Set("Attributes", *params.Attributes) 477 } 478 if params != nil && params.MessagingServiceSid != nil { 479 data.Set("MessagingServiceSid", *params.MessagingServiceSid) 480 } 481 if params != nil && params.State != nil { 482 data.Set("State", *params.State) 483 } 484 if params != nil && params.TimersInactive != nil { 485 data.Set("Timers.Inactive", *params.TimersInactive) 486 } 487 if params != nil && params.TimersClosed != nil { 488 data.Set("Timers.Closed", *params.TimersClosed) 489 } 490 if params != nil && params.UniqueName != nil { 491 data.Set("UniqueName", *params.UniqueName) 492 } 493 if params != nil && params.BindingsEmailAddress != nil { 494 data.Set("Bindings.Email.Address", *params.BindingsEmailAddress) 495 } 496 if params != nil && params.BindingsEmailName != nil { 497 data.Set("Bindings.Email.Name", *params.BindingsEmailName) 498 } 499 500 if params != nil && params.XTwilioWebhookEnabled != nil { 501 headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled 502 } 503 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 504 if err != nil { 505 return nil, err 506 } 507 508 defer resp.Body.Close() 509 510 ps := &ConversationsV1Conversation{} 511 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 512 return nil, err 513 } 514 515 return ps, err 516 }