github.com/twilio/twilio-go@v1.20.1/rest/chat/v2/services_channels_webhooks.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Chat
     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 'CreateChannelWebhook'
    27  type CreateChannelWebhookParams struct {
    28  	//
    29  	Type *string `json:"Type,omitempty"`
    30  	// The URL of the webhook to call using the `configuration.method`.
    31  	ConfigurationUrl *string `json:"Configuration.Url,omitempty"`
    32  	//
    33  	ConfigurationMethod *string `json:"Configuration.Method,omitempty"`
    34  	// The events that cause us to call the Channel Webhook. Used when `type` is `webhook`. This parameter takes only one event. To specify more than one event, repeat this parameter for each event. For the list of possible events, see [Webhook Event Triggers](https://www.twilio.com/docs/chat/webhook-events#webhook-event-trigger).
    35  	ConfigurationFilters *[]string `json:"Configuration.Filters,omitempty"`
    36  	// A string that will cause us to call the webhook when it is present in a message body. This parameter takes only one trigger string. To specify more than one, repeat this parameter for each trigger string up to a total of 5 trigger strings. Used only when `type` = `trigger`.
    37  	ConfigurationTriggers *[]string `json:"Configuration.Triggers,omitempty"`
    38  	// The SID of the Studio [Flow](https://www.twilio.com/docs/studio/rest-api/flow) to call when an event in `configuration.filters` occurs. Used only when `type` is `studio`.
    39  	ConfigurationFlowSid *string `json:"Configuration.FlowSid,omitempty"`
    40  	// The number of times to retry the webhook if the first attempt fails. Can be an integer between 0 and 3, inclusive, and the default is 0.
    41  	ConfigurationRetryCount *int `json:"Configuration.RetryCount,omitempty"`
    42  }
    43  
    44  func (params *CreateChannelWebhookParams) SetType(Type string) *CreateChannelWebhookParams {
    45  	params.Type = &Type
    46  	return params
    47  }
    48  func (params *CreateChannelWebhookParams) SetConfigurationUrl(ConfigurationUrl string) *CreateChannelWebhookParams {
    49  	params.ConfigurationUrl = &ConfigurationUrl
    50  	return params
    51  }
    52  func (params *CreateChannelWebhookParams) SetConfigurationMethod(ConfigurationMethod string) *CreateChannelWebhookParams {
    53  	params.ConfigurationMethod = &ConfigurationMethod
    54  	return params
    55  }
    56  func (params *CreateChannelWebhookParams) SetConfigurationFilters(ConfigurationFilters []string) *CreateChannelWebhookParams {
    57  	params.ConfigurationFilters = &ConfigurationFilters
    58  	return params
    59  }
    60  func (params *CreateChannelWebhookParams) SetConfigurationTriggers(ConfigurationTriggers []string) *CreateChannelWebhookParams {
    61  	params.ConfigurationTriggers = &ConfigurationTriggers
    62  	return params
    63  }
    64  func (params *CreateChannelWebhookParams) SetConfigurationFlowSid(ConfigurationFlowSid string) *CreateChannelWebhookParams {
    65  	params.ConfigurationFlowSid = &ConfigurationFlowSid
    66  	return params
    67  }
    68  func (params *CreateChannelWebhookParams) SetConfigurationRetryCount(ConfigurationRetryCount int) *CreateChannelWebhookParams {
    69  	params.ConfigurationRetryCount = &ConfigurationRetryCount
    70  	return params
    71  }
    72  
    73  //
    74  func (c *ApiService) CreateChannelWebhook(ServiceSid string, ChannelSid string, params *CreateChannelWebhookParams) (*ChatV2ChannelWebhook, error) {
    75  	path := "/v2/Services/{ServiceSid}/Channels/{ChannelSid}/Webhooks"
    76  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    77  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
    78  
    79  	data := url.Values{}
    80  	headers := make(map[string]interface{})
    81  
    82  	if params != nil && params.Type != nil {
    83  		data.Set("Type", *params.Type)
    84  	}
    85  	if params != nil && params.ConfigurationUrl != nil {
    86  		data.Set("Configuration.Url", *params.ConfigurationUrl)
    87  	}
    88  	if params != nil && params.ConfigurationMethod != nil {
    89  		data.Set("Configuration.Method", *params.ConfigurationMethod)
    90  	}
    91  	if params != nil && params.ConfigurationFilters != nil {
    92  		for _, item := range *params.ConfigurationFilters {
    93  			data.Add("Configuration.Filters", item)
    94  		}
    95  	}
    96  	if params != nil && params.ConfigurationTriggers != nil {
    97  		for _, item := range *params.ConfigurationTriggers {
    98  			data.Add("Configuration.Triggers", item)
    99  		}
   100  	}
   101  	if params != nil && params.ConfigurationFlowSid != nil {
   102  		data.Set("Configuration.FlowSid", *params.ConfigurationFlowSid)
   103  	}
   104  	if params != nil && params.ConfigurationRetryCount != nil {
   105  		data.Set("Configuration.RetryCount", fmt.Sprint(*params.ConfigurationRetryCount))
   106  	}
   107  
   108  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  
   113  	defer resp.Body.Close()
   114  
   115  	ps := &ChatV2ChannelWebhook{}
   116  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	return ps, err
   121  }
   122  
   123  //
   124  func (c *ApiService) DeleteChannelWebhook(ServiceSid string, ChannelSid string, Sid string) error {
   125  	path := "/v2/Services/{ServiceSid}/Channels/{ChannelSid}/Webhooks/{Sid}"
   126  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   127  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
   128  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   129  
   130  	data := url.Values{}
   131  	headers := make(map[string]interface{})
   132  
   133  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   134  	if err != nil {
   135  		return err
   136  	}
   137  
   138  	defer resp.Body.Close()
   139  
   140  	return nil
   141  }
   142  
   143  //
   144  func (c *ApiService) FetchChannelWebhook(ServiceSid string, ChannelSid string, Sid string) (*ChatV2ChannelWebhook, error) {
   145  	path := "/v2/Services/{ServiceSid}/Channels/{ChannelSid}/Webhooks/{Sid}"
   146  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   147  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
   148  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   149  
   150  	data := url.Values{}
   151  	headers := make(map[string]interface{})
   152  
   153  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   154  	if err != nil {
   155  		return nil, err
   156  	}
   157  
   158  	defer resp.Body.Close()
   159  
   160  	ps := &ChatV2ChannelWebhook{}
   161  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   162  		return nil, err
   163  	}
   164  
   165  	return ps, err
   166  }
   167  
   168  // Optional parameters for the method 'ListChannelWebhook'
   169  type ListChannelWebhookParams struct {
   170  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   171  	PageSize *int `json:"PageSize,omitempty"`
   172  	// Max number of records to return.
   173  	Limit *int `json:"limit,omitempty"`
   174  }
   175  
   176  func (params *ListChannelWebhookParams) SetPageSize(PageSize int) *ListChannelWebhookParams {
   177  	params.PageSize = &PageSize
   178  	return params
   179  }
   180  func (params *ListChannelWebhookParams) SetLimit(Limit int) *ListChannelWebhookParams {
   181  	params.Limit = &Limit
   182  	return params
   183  }
   184  
   185  // Retrieve a single page of ChannelWebhook records from the API. Request is executed immediately.
   186  func (c *ApiService) PageChannelWebhook(ServiceSid string, ChannelSid string, params *ListChannelWebhookParams, pageToken, pageNumber string) (*ListChannelWebhookResponse, error) {
   187  	path := "/v2/Services/{ServiceSid}/Channels/{ChannelSid}/Webhooks"
   188  
   189  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   190  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
   191  
   192  	data := url.Values{}
   193  	headers := make(map[string]interface{})
   194  
   195  	if params != nil && params.PageSize != nil {
   196  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   197  	}
   198  
   199  	if pageToken != "" {
   200  		data.Set("PageToken", pageToken)
   201  	}
   202  	if pageNumber != "" {
   203  		data.Set("Page", pageNumber)
   204  	}
   205  
   206  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   207  	if err != nil {
   208  		return nil, err
   209  	}
   210  
   211  	defer resp.Body.Close()
   212  
   213  	ps := &ListChannelWebhookResponse{}
   214  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   215  		return nil, err
   216  	}
   217  
   218  	return ps, err
   219  }
   220  
   221  // Lists ChannelWebhook records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   222  func (c *ApiService) ListChannelWebhook(ServiceSid string, ChannelSid string, params *ListChannelWebhookParams) ([]ChatV2ChannelWebhook, error) {
   223  	response, errors := c.StreamChannelWebhook(ServiceSid, ChannelSid, params)
   224  
   225  	records := make([]ChatV2ChannelWebhook, 0)
   226  	for record := range response {
   227  		records = append(records, record)
   228  	}
   229  
   230  	if err := <-errors; err != nil {
   231  		return nil, err
   232  	}
   233  
   234  	return records, nil
   235  }
   236  
   237  // Streams ChannelWebhook records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   238  func (c *ApiService) StreamChannelWebhook(ServiceSid string, ChannelSid string, params *ListChannelWebhookParams) (chan ChatV2ChannelWebhook, chan error) {
   239  	if params == nil {
   240  		params = &ListChannelWebhookParams{}
   241  	}
   242  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   243  
   244  	recordChannel := make(chan ChatV2ChannelWebhook, 1)
   245  	errorChannel := make(chan error, 1)
   246  
   247  	response, err := c.PageChannelWebhook(ServiceSid, ChannelSid, params, "", "")
   248  	if err != nil {
   249  		errorChannel <- err
   250  		close(recordChannel)
   251  		close(errorChannel)
   252  	} else {
   253  		go c.streamChannelWebhook(response, params, recordChannel, errorChannel)
   254  	}
   255  
   256  	return recordChannel, errorChannel
   257  }
   258  
   259  func (c *ApiService) streamChannelWebhook(response *ListChannelWebhookResponse, params *ListChannelWebhookParams, recordChannel chan ChatV2ChannelWebhook, errorChannel chan error) {
   260  	curRecord := 1
   261  
   262  	for response != nil {
   263  		responseRecords := response.Webhooks
   264  		for item := range responseRecords {
   265  			recordChannel <- responseRecords[item]
   266  			curRecord += 1
   267  			if params.Limit != nil && *params.Limit < curRecord {
   268  				close(recordChannel)
   269  				close(errorChannel)
   270  				return
   271  			}
   272  		}
   273  
   274  		record, err := client.GetNext(c.baseURL, response, c.getNextListChannelWebhookResponse)
   275  		if err != nil {
   276  			errorChannel <- err
   277  			break
   278  		} else if record == nil {
   279  			break
   280  		}
   281  
   282  		response = record.(*ListChannelWebhookResponse)
   283  	}
   284  
   285  	close(recordChannel)
   286  	close(errorChannel)
   287  }
   288  
   289  func (c *ApiService) getNextListChannelWebhookResponse(nextPageUrl string) (interface{}, error) {
   290  	if nextPageUrl == "" {
   291  		return nil, nil
   292  	}
   293  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   294  	if err != nil {
   295  		return nil, err
   296  	}
   297  
   298  	defer resp.Body.Close()
   299  
   300  	ps := &ListChannelWebhookResponse{}
   301  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   302  		return nil, err
   303  	}
   304  	return ps, nil
   305  }
   306  
   307  // Optional parameters for the method 'UpdateChannelWebhook'
   308  type UpdateChannelWebhookParams struct {
   309  	// The URL of the webhook to call using the `configuration.method`.
   310  	ConfigurationUrl *string `json:"Configuration.Url,omitempty"`
   311  	//
   312  	ConfigurationMethod *string `json:"Configuration.Method,omitempty"`
   313  	// The events that cause us to call the Channel Webhook. Used when `type` is `webhook`. This parameter takes only one event. To specify more than one event, repeat this parameter for each event. For the list of possible events, see [Webhook Event Triggers](https://www.twilio.com/docs/chat/webhook-events#webhook-event-trigger).
   314  	ConfigurationFilters *[]string `json:"Configuration.Filters,omitempty"`
   315  	// A string that will cause us to call the webhook when it is present in a message body. This parameter takes only one trigger string. To specify more than one, repeat this parameter for each trigger string up to a total of 5 trigger strings. Used only when `type` = `trigger`.
   316  	ConfigurationTriggers *[]string `json:"Configuration.Triggers,omitempty"`
   317  	// The SID of the Studio [Flow](https://www.twilio.com/docs/studio/rest-api/flow) to call when an event in `configuration.filters` occurs. Used only when `type` = `studio`.
   318  	ConfigurationFlowSid *string `json:"Configuration.FlowSid,omitempty"`
   319  	// The number of times to retry the webhook if the first attempt fails. Can be an integer between 0 and 3, inclusive, and the default is 0.
   320  	ConfigurationRetryCount *int `json:"Configuration.RetryCount,omitempty"`
   321  }
   322  
   323  func (params *UpdateChannelWebhookParams) SetConfigurationUrl(ConfigurationUrl string) *UpdateChannelWebhookParams {
   324  	params.ConfigurationUrl = &ConfigurationUrl
   325  	return params
   326  }
   327  func (params *UpdateChannelWebhookParams) SetConfigurationMethod(ConfigurationMethod string) *UpdateChannelWebhookParams {
   328  	params.ConfigurationMethod = &ConfigurationMethod
   329  	return params
   330  }
   331  func (params *UpdateChannelWebhookParams) SetConfigurationFilters(ConfigurationFilters []string) *UpdateChannelWebhookParams {
   332  	params.ConfigurationFilters = &ConfigurationFilters
   333  	return params
   334  }
   335  func (params *UpdateChannelWebhookParams) SetConfigurationTriggers(ConfigurationTriggers []string) *UpdateChannelWebhookParams {
   336  	params.ConfigurationTriggers = &ConfigurationTriggers
   337  	return params
   338  }
   339  func (params *UpdateChannelWebhookParams) SetConfigurationFlowSid(ConfigurationFlowSid string) *UpdateChannelWebhookParams {
   340  	params.ConfigurationFlowSid = &ConfigurationFlowSid
   341  	return params
   342  }
   343  func (params *UpdateChannelWebhookParams) SetConfigurationRetryCount(ConfigurationRetryCount int) *UpdateChannelWebhookParams {
   344  	params.ConfigurationRetryCount = &ConfigurationRetryCount
   345  	return params
   346  }
   347  
   348  //
   349  func (c *ApiService) UpdateChannelWebhook(ServiceSid string, ChannelSid string, Sid string, params *UpdateChannelWebhookParams) (*ChatV2ChannelWebhook, error) {
   350  	path := "/v2/Services/{ServiceSid}/Channels/{ChannelSid}/Webhooks/{Sid}"
   351  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   352  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
   353  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   354  
   355  	data := url.Values{}
   356  	headers := make(map[string]interface{})
   357  
   358  	if params != nil && params.ConfigurationUrl != nil {
   359  		data.Set("Configuration.Url", *params.ConfigurationUrl)
   360  	}
   361  	if params != nil && params.ConfigurationMethod != nil {
   362  		data.Set("Configuration.Method", *params.ConfigurationMethod)
   363  	}
   364  	if params != nil && params.ConfigurationFilters != nil {
   365  		for _, item := range *params.ConfigurationFilters {
   366  			data.Add("Configuration.Filters", item)
   367  		}
   368  	}
   369  	if params != nil && params.ConfigurationTriggers != nil {
   370  		for _, item := range *params.ConfigurationTriggers {
   371  			data.Add("Configuration.Triggers", item)
   372  		}
   373  	}
   374  	if params != nil && params.ConfigurationFlowSid != nil {
   375  		data.Set("Configuration.FlowSid", *params.ConfigurationFlowSid)
   376  	}
   377  	if params != nil && params.ConfigurationRetryCount != nil {
   378  		data.Set("Configuration.RetryCount", fmt.Sprint(*params.ConfigurationRetryCount))
   379  	}
   380  
   381  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   382  	if err != nil {
   383  		return nil, err
   384  	}
   385  
   386  	defer resp.Body.Close()
   387  
   388  	ps := &ChatV2ChannelWebhook{}
   389  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   390  		return nil, err
   391  	}
   392  
   393  	return ps, err
   394  }