github.com/twilio/twilio-go@v1.20.1/rest/conversations/v1/conversations_webhooks.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 23 "github.com/twilio/twilio-go/client" 24 ) 25 26 // Optional parameters for the method 'CreateConversationScopedWebhook' 27 type CreateConversationScopedWebhookParams struct { 28 // 29 Target *string `json:"Target,omitempty"` 30 // The absolute url the webhook request should be sent to. 31 ConfigurationUrl *string `json:"Configuration.Url,omitempty"` 32 // 33 ConfigurationMethod *string `json:"Configuration.Method,omitempty"` 34 // The list of events, firing webhook event for this Conversation. 35 ConfigurationFilters *[]string `json:"Configuration.Filters,omitempty"` 36 // The list of keywords, firing webhook event for this Conversation. 37 ConfigurationTriggers *[]string `json:"Configuration.Triggers,omitempty"` 38 // The studio flow SID, where the webhook should be sent to. 39 ConfigurationFlowSid *string `json:"Configuration.FlowSid,omitempty"` 40 // The message index for which and it's successors the webhook will be replayed. Not set by default 41 ConfigurationReplayAfter *int `json:"Configuration.ReplayAfter,omitempty"` 42 } 43 44 func (params *CreateConversationScopedWebhookParams) SetTarget(Target string) *CreateConversationScopedWebhookParams { 45 params.Target = &Target 46 return params 47 } 48 func (params *CreateConversationScopedWebhookParams) SetConfigurationUrl(ConfigurationUrl string) *CreateConversationScopedWebhookParams { 49 params.ConfigurationUrl = &ConfigurationUrl 50 return params 51 } 52 func (params *CreateConversationScopedWebhookParams) SetConfigurationMethod(ConfigurationMethod string) *CreateConversationScopedWebhookParams { 53 params.ConfigurationMethod = &ConfigurationMethod 54 return params 55 } 56 func (params *CreateConversationScopedWebhookParams) SetConfigurationFilters(ConfigurationFilters []string) *CreateConversationScopedWebhookParams { 57 params.ConfigurationFilters = &ConfigurationFilters 58 return params 59 } 60 func (params *CreateConversationScopedWebhookParams) SetConfigurationTriggers(ConfigurationTriggers []string) *CreateConversationScopedWebhookParams { 61 params.ConfigurationTriggers = &ConfigurationTriggers 62 return params 63 } 64 func (params *CreateConversationScopedWebhookParams) SetConfigurationFlowSid(ConfigurationFlowSid string) *CreateConversationScopedWebhookParams { 65 params.ConfigurationFlowSid = &ConfigurationFlowSid 66 return params 67 } 68 func (params *CreateConversationScopedWebhookParams) SetConfigurationReplayAfter(ConfigurationReplayAfter int) *CreateConversationScopedWebhookParams { 69 params.ConfigurationReplayAfter = &ConfigurationReplayAfter 70 return params 71 } 72 73 // Create a new webhook scoped to the conversation 74 func (c *ApiService) CreateConversationScopedWebhook(ConversationSid string, params *CreateConversationScopedWebhookParams) (*ConversationsV1ConversationScopedWebhook, error) { 75 path := "/v1/Conversations/{ConversationSid}/Webhooks" 76 path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1) 77 78 data := url.Values{} 79 headers := make(map[string]interface{}) 80 81 if params != nil && params.Target != nil { 82 data.Set("Target", *params.Target) 83 } 84 if params != nil && params.ConfigurationUrl != nil { 85 data.Set("Configuration.Url", *params.ConfigurationUrl) 86 } 87 if params != nil && params.ConfigurationMethod != nil { 88 data.Set("Configuration.Method", *params.ConfigurationMethod) 89 } 90 if params != nil && params.ConfigurationFilters != nil { 91 for _, item := range *params.ConfigurationFilters { 92 data.Add("Configuration.Filters", item) 93 } 94 } 95 if params != nil && params.ConfigurationTriggers != nil { 96 for _, item := range *params.ConfigurationTriggers { 97 data.Add("Configuration.Triggers", item) 98 } 99 } 100 if params != nil && params.ConfigurationFlowSid != nil { 101 data.Set("Configuration.FlowSid", *params.ConfigurationFlowSid) 102 } 103 if params != nil && params.ConfigurationReplayAfter != nil { 104 data.Set("Configuration.ReplayAfter", fmt.Sprint(*params.ConfigurationReplayAfter)) 105 } 106 107 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 108 if err != nil { 109 return nil, err 110 } 111 112 defer resp.Body.Close() 113 114 ps := &ConversationsV1ConversationScopedWebhook{} 115 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 116 return nil, err 117 } 118 119 return ps, err 120 } 121 122 // Remove an existing webhook scoped to the conversation 123 func (c *ApiService) DeleteConversationScopedWebhook(ConversationSid string, Sid string) error { 124 path := "/v1/Conversations/{ConversationSid}/Webhooks/{Sid}" 125 path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1) 126 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 127 128 data := url.Values{} 129 headers := make(map[string]interface{}) 130 131 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 132 if err != nil { 133 return err 134 } 135 136 defer resp.Body.Close() 137 138 return nil 139 } 140 141 // Fetch the configuration of a conversation-scoped webhook 142 func (c *ApiService) FetchConversationScopedWebhook(ConversationSid string, Sid string) (*ConversationsV1ConversationScopedWebhook, error) { 143 path := "/v1/Conversations/{ConversationSid}/Webhooks/{Sid}" 144 path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1) 145 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 146 147 data := url.Values{} 148 headers := make(map[string]interface{}) 149 150 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 151 if err != nil { 152 return nil, err 153 } 154 155 defer resp.Body.Close() 156 157 ps := &ConversationsV1ConversationScopedWebhook{} 158 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 159 return nil, err 160 } 161 162 return ps, err 163 } 164 165 // Optional parameters for the method 'ListConversationScopedWebhook' 166 type ListConversationScopedWebhookParams struct { 167 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 168 PageSize *int `json:"PageSize,omitempty"` 169 // Max number of records to return. 170 Limit *int `json:"limit,omitempty"` 171 } 172 173 func (params *ListConversationScopedWebhookParams) SetPageSize(PageSize int) *ListConversationScopedWebhookParams { 174 params.PageSize = &PageSize 175 return params 176 } 177 func (params *ListConversationScopedWebhookParams) SetLimit(Limit int) *ListConversationScopedWebhookParams { 178 params.Limit = &Limit 179 return params 180 } 181 182 // Retrieve a single page of ConversationScopedWebhook records from the API. Request is executed immediately. 183 func (c *ApiService) PageConversationScopedWebhook(ConversationSid string, params *ListConversationScopedWebhookParams, pageToken, pageNumber string) (*ListConversationScopedWebhookResponse, error) { 184 path := "/v1/Conversations/{ConversationSid}/Webhooks" 185 186 path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1) 187 188 data := url.Values{} 189 headers := make(map[string]interface{}) 190 191 if params != nil && params.PageSize != nil { 192 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 193 } 194 195 if pageToken != "" { 196 data.Set("PageToken", pageToken) 197 } 198 if pageNumber != "" { 199 data.Set("Page", pageNumber) 200 } 201 202 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 203 if err != nil { 204 return nil, err 205 } 206 207 defer resp.Body.Close() 208 209 ps := &ListConversationScopedWebhookResponse{} 210 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 211 return nil, err 212 } 213 214 return ps, err 215 } 216 217 // Lists ConversationScopedWebhook records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 218 func (c *ApiService) ListConversationScopedWebhook(ConversationSid string, params *ListConversationScopedWebhookParams) ([]ConversationsV1ConversationScopedWebhook, error) { 219 response, errors := c.StreamConversationScopedWebhook(ConversationSid, params) 220 221 records := make([]ConversationsV1ConversationScopedWebhook, 0) 222 for record := range response { 223 records = append(records, record) 224 } 225 226 if err := <-errors; err != nil { 227 return nil, err 228 } 229 230 return records, nil 231 } 232 233 // Streams ConversationScopedWebhook records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 234 func (c *ApiService) StreamConversationScopedWebhook(ConversationSid string, params *ListConversationScopedWebhookParams) (chan ConversationsV1ConversationScopedWebhook, chan error) { 235 if params == nil { 236 params = &ListConversationScopedWebhookParams{} 237 } 238 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 239 240 recordChannel := make(chan ConversationsV1ConversationScopedWebhook, 1) 241 errorChannel := make(chan error, 1) 242 243 response, err := c.PageConversationScopedWebhook(ConversationSid, params, "", "") 244 if err != nil { 245 errorChannel <- err 246 close(recordChannel) 247 close(errorChannel) 248 } else { 249 go c.streamConversationScopedWebhook(response, params, recordChannel, errorChannel) 250 } 251 252 return recordChannel, errorChannel 253 } 254 255 func (c *ApiService) streamConversationScopedWebhook(response *ListConversationScopedWebhookResponse, params *ListConversationScopedWebhookParams, recordChannel chan ConversationsV1ConversationScopedWebhook, errorChannel chan error) { 256 curRecord := 1 257 258 for response != nil { 259 responseRecords := response.Webhooks 260 for item := range responseRecords { 261 recordChannel <- responseRecords[item] 262 curRecord += 1 263 if params.Limit != nil && *params.Limit < curRecord { 264 close(recordChannel) 265 close(errorChannel) 266 return 267 } 268 } 269 270 record, err := client.GetNext(c.baseURL, response, c.getNextListConversationScopedWebhookResponse) 271 if err != nil { 272 errorChannel <- err 273 break 274 } else if record == nil { 275 break 276 } 277 278 response = record.(*ListConversationScopedWebhookResponse) 279 } 280 281 close(recordChannel) 282 close(errorChannel) 283 } 284 285 func (c *ApiService) getNextListConversationScopedWebhookResponse(nextPageUrl string) (interface{}, error) { 286 if nextPageUrl == "" { 287 return nil, nil 288 } 289 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 290 if err != nil { 291 return nil, err 292 } 293 294 defer resp.Body.Close() 295 296 ps := &ListConversationScopedWebhookResponse{} 297 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 298 return nil, err 299 } 300 return ps, nil 301 } 302 303 // Optional parameters for the method 'UpdateConversationScopedWebhook' 304 type UpdateConversationScopedWebhookParams struct { 305 // The absolute url the webhook request should be sent to. 306 ConfigurationUrl *string `json:"Configuration.Url,omitempty"` 307 // 308 ConfigurationMethod *string `json:"Configuration.Method,omitempty"` 309 // The list of events, firing webhook event for this Conversation. 310 ConfigurationFilters *[]string `json:"Configuration.Filters,omitempty"` 311 // The list of keywords, firing webhook event for this Conversation. 312 ConfigurationTriggers *[]string `json:"Configuration.Triggers,omitempty"` 313 // The studio flow SID, where the webhook should be sent to. 314 ConfigurationFlowSid *string `json:"Configuration.FlowSid,omitempty"` 315 } 316 317 func (params *UpdateConversationScopedWebhookParams) SetConfigurationUrl(ConfigurationUrl string) *UpdateConversationScopedWebhookParams { 318 params.ConfigurationUrl = &ConfigurationUrl 319 return params 320 } 321 func (params *UpdateConversationScopedWebhookParams) SetConfigurationMethod(ConfigurationMethod string) *UpdateConversationScopedWebhookParams { 322 params.ConfigurationMethod = &ConfigurationMethod 323 return params 324 } 325 func (params *UpdateConversationScopedWebhookParams) SetConfigurationFilters(ConfigurationFilters []string) *UpdateConversationScopedWebhookParams { 326 params.ConfigurationFilters = &ConfigurationFilters 327 return params 328 } 329 func (params *UpdateConversationScopedWebhookParams) SetConfigurationTriggers(ConfigurationTriggers []string) *UpdateConversationScopedWebhookParams { 330 params.ConfigurationTriggers = &ConfigurationTriggers 331 return params 332 } 333 func (params *UpdateConversationScopedWebhookParams) SetConfigurationFlowSid(ConfigurationFlowSid string) *UpdateConversationScopedWebhookParams { 334 params.ConfigurationFlowSid = &ConfigurationFlowSid 335 return params 336 } 337 338 // Update an existing conversation-scoped webhook 339 func (c *ApiService) UpdateConversationScopedWebhook(ConversationSid string, Sid string, params *UpdateConversationScopedWebhookParams) (*ConversationsV1ConversationScopedWebhook, error) { 340 path := "/v1/Conversations/{ConversationSid}/Webhooks/{Sid}" 341 path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1) 342 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 343 344 data := url.Values{} 345 headers := make(map[string]interface{}) 346 347 if params != nil && params.ConfigurationUrl != nil { 348 data.Set("Configuration.Url", *params.ConfigurationUrl) 349 } 350 if params != nil && params.ConfigurationMethod != nil { 351 data.Set("Configuration.Method", *params.ConfigurationMethod) 352 } 353 if params != nil && params.ConfigurationFilters != nil { 354 for _, item := range *params.ConfigurationFilters { 355 data.Add("Configuration.Filters", item) 356 } 357 } 358 if params != nil && params.ConfigurationTriggers != nil { 359 for _, item := range *params.ConfigurationTriggers { 360 data.Add("Configuration.Triggers", item) 361 } 362 } 363 if params != nil && params.ConfigurationFlowSid != nil { 364 data.Set("Configuration.FlowSid", *params.ConfigurationFlowSid) 365 } 366 367 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 368 if err != nil { 369 return nil, err 370 } 371 372 defer resp.Body.Close() 373 374 ps := &ConversationsV1ConversationScopedWebhook{} 375 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 376 return nil, err 377 } 378 379 return ps, err 380 }