github.com/twilio/twilio-go@v1.20.1/rest/conversations/v1/services_conversations_participants.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 'CreateServiceConversationParticipant' 28 type CreateServiceConversationParticipantParams struct { 29 // The X-Twilio-Webhook-Enabled HTTP request header 30 XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"` 31 // A unique string identifier for the conversation participant as [Conversation User](https://www.twilio.com/docs/conversations/api/user-resource). This parameter is non-null if (and only if) the participant is using the [Conversation SDK](https://www.twilio.com/docs/conversations/sdk-overview) to communicate. Limited to 256 characters. 32 Identity *string `json:"Identity,omitempty"` 33 // The address of the participant's device, e.g. a phone or WhatsApp number. Together with the Proxy address, this determines a participant uniquely. This field (with `proxy_address`) is only null when the participant is interacting from an SDK endpoint (see the `identity` field). 34 MessagingBindingAddress *string `json:"MessagingBinding.Address,omitempty"` 35 // The address of the Twilio phone number (or WhatsApp number) that the participant is in contact with. This field, together with participant address, is only null when the participant is interacting from an SDK endpoint (see the `identity` field). 36 MessagingBindingProxyAddress *string `json:"MessagingBinding.ProxyAddress,omitempty"` 37 // The date on which this resource was created. 38 DateCreated *time.Time `json:"DateCreated,omitempty"` 39 // The date on which this resource was last updated. 40 DateUpdated *time.Time `json:"DateUpdated,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 // The address of the Twilio phone number that is used in Group MMS. 44 MessagingBindingProjectedAddress *string `json:"MessagingBinding.ProjectedAddress,omitempty"` 45 // The SID of a conversation-level [Role](https://www.twilio.com/docs/conversations/api/role-resource) to assign to the participant. 46 RoleSid *string `json:"RoleSid,omitempty"` 47 } 48 49 func (params *CreateServiceConversationParticipantParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *CreateServiceConversationParticipantParams { 50 params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled 51 return params 52 } 53 func (params *CreateServiceConversationParticipantParams) SetIdentity(Identity string) *CreateServiceConversationParticipantParams { 54 params.Identity = &Identity 55 return params 56 } 57 func (params *CreateServiceConversationParticipantParams) SetMessagingBindingAddress(MessagingBindingAddress string) *CreateServiceConversationParticipantParams { 58 params.MessagingBindingAddress = &MessagingBindingAddress 59 return params 60 } 61 func (params *CreateServiceConversationParticipantParams) SetMessagingBindingProxyAddress(MessagingBindingProxyAddress string) *CreateServiceConversationParticipantParams { 62 params.MessagingBindingProxyAddress = &MessagingBindingProxyAddress 63 return params 64 } 65 func (params *CreateServiceConversationParticipantParams) SetDateCreated(DateCreated time.Time) *CreateServiceConversationParticipantParams { 66 params.DateCreated = &DateCreated 67 return params 68 } 69 func (params *CreateServiceConversationParticipantParams) SetDateUpdated(DateUpdated time.Time) *CreateServiceConversationParticipantParams { 70 params.DateUpdated = &DateUpdated 71 return params 72 } 73 func (params *CreateServiceConversationParticipantParams) SetAttributes(Attributes string) *CreateServiceConversationParticipantParams { 74 params.Attributes = &Attributes 75 return params 76 } 77 func (params *CreateServiceConversationParticipantParams) SetMessagingBindingProjectedAddress(MessagingBindingProjectedAddress string) *CreateServiceConversationParticipantParams { 78 params.MessagingBindingProjectedAddress = &MessagingBindingProjectedAddress 79 return params 80 } 81 func (params *CreateServiceConversationParticipantParams) SetRoleSid(RoleSid string) *CreateServiceConversationParticipantParams { 82 params.RoleSid = &RoleSid 83 return params 84 } 85 86 // Add a new participant to the conversation in a specific service 87 func (c *ApiService) CreateServiceConversationParticipant(ChatServiceSid string, ConversationSid string, params *CreateServiceConversationParticipantParams) (*ConversationsV1ServiceConversationParticipant, error) { 88 path := "/v1/Services/{ChatServiceSid}/Conversations/{ConversationSid}/Participants" 89 path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1) 90 path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1) 91 92 data := url.Values{} 93 headers := make(map[string]interface{}) 94 95 if params != nil && params.Identity != nil { 96 data.Set("Identity", *params.Identity) 97 } 98 if params != nil && params.MessagingBindingAddress != nil { 99 data.Set("MessagingBinding.Address", *params.MessagingBindingAddress) 100 } 101 if params != nil && params.MessagingBindingProxyAddress != nil { 102 data.Set("MessagingBinding.ProxyAddress", *params.MessagingBindingProxyAddress) 103 } 104 if params != nil && params.DateCreated != nil { 105 data.Set("DateCreated", fmt.Sprint((*params.DateCreated).Format(time.RFC3339))) 106 } 107 if params != nil && params.DateUpdated != nil { 108 data.Set("DateUpdated", fmt.Sprint((*params.DateUpdated).Format(time.RFC3339))) 109 } 110 if params != nil && params.Attributes != nil { 111 data.Set("Attributes", *params.Attributes) 112 } 113 if params != nil && params.MessagingBindingProjectedAddress != nil { 114 data.Set("MessagingBinding.ProjectedAddress", *params.MessagingBindingProjectedAddress) 115 } 116 if params != nil && params.RoleSid != nil { 117 data.Set("RoleSid", *params.RoleSid) 118 } 119 120 if params != nil && params.XTwilioWebhookEnabled != nil { 121 headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled 122 } 123 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 124 if err != nil { 125 return nil, err 126 } 127 128 defer resp.Body.Close() 129 130 ps := &ConversationsV1ServiceConversationParticipant{} 131 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 132 return nil, err 133 } 134 135 return ps, err 136 } 137 138 // Optional parameters for the method 'DeleteServiceConversationParticipant' 139 type DeleteServiceConversationParticipantParams struct { 140 // The X-Twilio-Webhook-Enabled HTTP request header 141 XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"` 142 } 143 144 func (params *DeleteServiceConversationParticipantParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *DeleteServiceConversationParticipantParams { 145 params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled 146 return params 147 } 148 149 // Remove a participant from the conversation 150 func (c *ApiService) DeleteServiceConversationParticipant(ChatServiceSid string, ConversationSid string, Sid string, params *DeleteServiceConversationParticipantParams) error { 151 path := "/v1/Services/{ChatServiceSid}/Conversations/{ConversationSid}/Participants/{Sid}" 152 path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1) 153 path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1) 154 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 155 156 data := url.Values{} 157 headers := make(map[string]interface{}) 158 159 if params != nil && params.XTwilioWebhookEnabled != nil { 160 headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled 161 } 162 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 163 if err != nil { 164 return err 165 } 166 167 defer resp.Body.Close() 168 169 return nil 170 } 171 172 // Fetch a participant of the conversation 173 func (c *ApiService) FetchServiceConversationParticipant(ChatServiceSid string, ConversationSid string, Sid string) (*ConversationsV1ServiceConversationParticipant, error) { 174 path := "/v1/Services/{ChatServiceSid}/Conversations/{ConversationSid}/Participants/{Sid}" 175 path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1) 176 path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1) 177 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 178 179 data := url.Values{} 180 headers := make(map[string]interface{}) 181 182 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 183 if err != nil { 184 return nil, err 185 } 186 187 defer resp.Body.Close() 188 189 ps := &ConversationsV1ServiceConversationParticipant{} 190 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 191 return nil, err 192 } 193 194 return ps, err 195 } 196 197 // Optional parameters for the method 'ListServiceConversationParticipant' 198 type ListServiceConversationParticipantParams struct { 199 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 200 PageSize *int `json:"PageSize,omitempty"` 201 // Max number of records to return. 202 Limit *int `json:"limit,omitempty"` 203 } 204 205 func (params *ListServiceConversationParticipantParams) SetPageSize(PageSize int) *ListServiceConversationParticipantParams { 206 params.PageSize = &PageSize 207 return params 208 } 209 func (params *ListServiceConversationParticipantParams) SetLimit(Limit int) *ListServiceConversationParticipantParams { 210 params.Limit = &Limit 211 return params 212 } 213 214 // Retrieve a single page of ServiceConversationParticipant records from the API. Request is executed immediately. 215 func (c *ApiService) PageServiceConversationParticipant(ChatServiceSid string, ConversationSid string, params *ListServiceConversationParticipantParams, pageToken, pageNumber string) (*ListServiceConversationParticipantResponse, error) { 216 path := "/v1/Services/{ChatServiceSid}/Conversations/{ConversationSid}/Participants" 217 218 path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1) 219 path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1) 220 221 data := url.Values{} 222 headers := make(map[string]interface{}) 223 224 if params != nil && params.PageSize != nil { 225 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 226 } 227 228 if pageToken != "" { 229 data.Set("PageToken", pageToken) 230 } 231 if pageNumber != "" { 232 data.Set("Page", pageNumber) 233 } 234 235 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 236 if err != nil { 237 return nil, err 238 } 239 240 defer resp.Body.Close() 241 242 ps := &ListServiceConversationParticipantResponse{} 243 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 244 return nil, err 245 } 246 247 return ps, err 248 } 249 250 // Lists ServiceConversationParticipant records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 251 func (c *ApiService) ListServiceConversationParticipant(ChatServiceSid string, ConversationSid string, params *ListServiceConversationParticipantParams) ([]ConversationsV1ServiceConversationParticipant, error) { 252 response, errors := c.StreamServiceConversationParticipant(ChatServiceSid, ConversationSid, params) 253 254 records := make([]ConversationsV1ServiceConversationParticipant, 0) 255 for record := range response { 256 records = append(records, record) 257 } 258 259 if err := <-errors; err != nil { 260 return nil, err 261 } 262 263 return records, nil 264 } 265 266 // Streams ServiceConversationParticipant records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 267 func (c *ApiService) StreamServiceConversationParticipant(ChatServiceSid string, ConversationSid string, params *ListServiceConversationParticipantParams) (chan ConversationsV1ServiceConversationParticipant, chan error) { 268 if params == nil { 269 params = &ListServiceConversationParticipantParams{} 270 } 271 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 272 273 recordChannel := make(chan ConversationsV1ServiceConversationParticipant, 1) 274 errorChannel := make(chan error, 1) 275 276 response, err := c.PageServiceConversationParticipant(ChatServiceSid, ConversationSid, params, "", "") 277 if err != nil { 278 errorChannel <- err 279 close(recordChannel) 280 close(errorChannel) 281 } else { 282 go c.streamServiceConversationParticipant(response, params, recordChannel, errorChannel) 283 } 284 285 return recordChannel, errorChannel 286 } 287 288 func (c *ApiService) streamServiceConversationParticipant(response *ListServiceConversationParticipantResponse, params *ListServiceConversationParticipantParams, recordChannel chan ConversationsV1ServiceConversationParticipant, errorChannel chan error) { 289 curRecord := 1 290 291 for response != nil { 292 responseRecords := response.Participants 293 for item := range responseRecords { 294 recordChannel <- responseRecords[item] 295 curRecord += 1 296 if params.Limit != nil && *params.Limit < curRecord { 297 close(recordChannel) 298 close(errorChannel) 299 return 300 } 301 } 302 303 record, err := client.GetNext(c.baseURL, response, c.getNextListServiceConversationParticipantResponse) 304 if err != nil { 305 errorChannel <- err 306 break 307 } else if record == nil { 308 break 309 } 310 311 response = record.(*ListServiceConversationParticipantResponse) 312 } 313 314 close(recordChannel) 315 close(errorChannel) 316 } 317 318 func (c *ApiService) getNextListServiceConversationParticipantResponse(nextPageUrl string) (interface{}, error) { 319 if nextPageUrl == "" { 320 return nil, nil 321 } 322 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 323 if err != nil { 324 return nil, err 325 } 326 327 defer resp.Body.Close() 328 329 ps := &ListServiceConversationParticipantResponse{} 330 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 331 return nil, err 332 } 333 return ps, nil 334 } 335 336 // Optional parameters for the method 'UpdateServiceConversationParticipant' 337 type UpdateServiceConversationParticipantParams struct { 338 // The X-Twilio-Webhook-Enabled HTTP request header 339 XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"` 340 // The date on which this resource was created. 341 DateCreated *time.Time `json:"DateCreated,omitempty"` 342 // The date on which this resource was last updated. 343 DateUpdated *time.Time `json:"DateUpdated,omitempty"` 344 // A unique string identifier for the conversation participant as [Conversation User](https://www.twilio.com/docs/conversations/api/user-resource). This parameter is non-null if (and only if) the participant is using the [Conversation SDK](https://www.twilio.com/docs/conversations/sdk-overview) to communicate. Limited to 256 characters. 345 Identity *string `json:"Identity,omitempty"` 346 // 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. 347 Attributes *string `json:"Attributes,omitempty"` 348 // The SID of a conversation-level [Role](https://www.twilio.com/docs/conversations/api/role-resource) to assign to the participant. 349 RoleSid *string `json:"RoleSid,omitempty"` 350 // The address of the Twilio phone number that the participant is in contact with. 'null' value will remove it. 351 MessagingBindingProxyAddress *string `json:"MessagingBinding.ProxyAddress,omitempty"` 352 // The address of the Twilio phone number that is used in Group MMS. 'null' value will remove it. 353 MessagingBindingProjectedAddress *string `json:"MessagingBinding.ProjectedAddress,omitempty"` 354 // Index of last “read” message in the [Conversation](https://www.twilio.com/docs/conversations/api/conversation-resource) for the Participant. 355 LastReadMessageIndex *int `json:"LastReadMessageIndex,omitempty"` 356 // Timestamp of last “read” message in the [Conversation](https://www.twilio.com/docs/conversations/api/conversation-resource) for the Participant. 357 LastReadTimestamp *string `json:"LastReadTimestamp,omitempty"` 358 } 359 360 func (params *UpdateServiceConversationParticipantParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *UpdateServiceConversationParticipantParams { 361 params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled 362 return params 363 } 364 func (params *UpdateServiceConversationParticipantParams) SetDateCreated(DateCreated time.Time) *UpdateServiceConversationParticipantParams { 365 params.DateCreated = &DateCreated 366 return params 367 } 368 func (params *UpdateServiceConversationParticipantParams) SetDateUpdated(DateUpdated time.Time) *UpdateServiceConversationParticipantParams { 369 params.DateUpdated = &DateUpdated 370 return params 371 } 372 func (params *UpdateServiceConversationParticipantParams) SetIdentity(Identity string) *UpdateServiceConversationParticipantParams { 373 params.Identity = &Identity 374 return params 375 } 376 func (params *UpdateServiceConversationParticipantParams) SetAttributes(Attributes string) *UpdateServiceConversationParticipantParams { 377 params.Attributes = &Attributes 378 return params 379 } 380 func (params *UpdateServiceConversationParticipantParams) SetRoleSid(RoleSid string) *UpdateServiceConversationParticipantParams { 381 params.RoleSid = &RoleSid 382 return params 383 } 384 func (params *UpdateServiceConversationParticipantParams) SetMessagingBindingProxyAddress(MessagingBindingProxyAddress string) *UpdateServiceConversationParticipantParams { 385 params.MessagingBindingProxyAddress = &MessagingBindingProxyAddress 386 return params 387 } 388 func (params *UpdateServiceConversationParticipantParams) SetMessagingBindingProjectedAddress(MessagingBindingProjectedAddress string) *UpdateServiceConversationParticipantParams { 389 params.MessagingBindingProjectedAddress = &MessagingBindingProjectedAddress 390 return params 391 } 392 func (params *UpdateServiceConversationParticipantParams) SetLastReadMessageIndex(LastReadMessageIndex int) *UpdateServiceConversationParticipantParams { 393 params.LastReadMessageIndex = &LastReadMessageIndex 394 return params 395 } 396 func (params *UpdateServiceConversationParticipantParams) SetLastReadTimestamp(LastReadTimestamp string) *UpdateServiceConversationParticipantParams { 397 params.LastReadTimestamp = &LastReadTimestamp 398 return params 399 } 400 401 // Update an existing participant in the conversation 402 func (c *ApiService) UpdateServiceConversationParticipant(ChatServiceSid string, ConversationSid string, Sid string, params *UpdateServiceConversationParticipantParams) (*ConversationsV1ServiceConversationParticipant, error) { 403 path := "/v1/Services/{ChatServiceSid}/Conversations/{ConversationSid}/Participants/{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.DateCreated != nil { 412 data.Set("DateCreated", fmt.Sprint((*params.DateCreated).Format(time.RFC3339))) 413 } 414 if params != nil && params.DateUpdated != nil { 415 data.Set("DateUpdated", fmt.Sprint((*params.DateUpdated).Format(time.RFC3339))) 416 } 417 if params != nil && params.Identity != nil { 418 data.Set("Identity", *params.Identity) 419 } 420 if params != nil && params.Attributes != nil { 421 data.Set("Attributes", *params.Attributes) 422 } 423 if params != nil && params.RoleSid != nil { 424 data.Set("RoleSid", *params.RoleSid) 425 } 426 if params != nil && params.MessagingBindingProxyAddress != nil { 427 data.Set("MessagingBinding.ProxyAddress", *params.MessagingBindingProxyAddress) 428 } 429 if params != nil && params.MessagingBindingProjectedAddress != nil { 430 data.Set("MessagingBinding.ProjectedAddress", *params.MessagingBindingProjectedAddress) 431 } 432 if params != nil && params.LastReadMessageIndex != nil { 433 data.Set("LastReadMessageIndex", fmt.Sprint(*params.LastReadMessageIndex)) 434 } 435 if params != nil && params.LastReadTimestamp != nil { 436 data.Set("LastReadTimestamp", *params.LastReadTimestamp) 437 } 438 439 if params != nil && params.XTwilioWebhookEnabled != nil { 440 headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled 441 } 442 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 443 if err != nil { 444 return nil, err 445 } 446 447 defer resp.Body.Close() 448 449 ps := &ConversationsV1ServiceConversationParticipant{} 450 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 451 return nil, err 452 } 453 454 return ps, err 455 }