github.com/twilio/twilio-go@v1.20.1/rest/conversations/v1/services_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 'CreateServiceConversationScopedWebhook'
    27  type CreateServiceConversationScopedWebhookParams 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 *CreateServiceConversationScopedWebhookParams) SetTarget(Target string) *CreateServiceConversationScopedWebhookParams {
    45  	params.Target = &Target
    46  	return params
    47  }
    48  func (params *CreateServiceConversationScopedWebhookParams) SetConfigurationUrl(ConfigurationUrl string) *CreateServiceConversationScopedWebhookParams {
    49  	params.ConfigurationUrl = &ConfigurationUrl
    50  	return params
    51  }
    52  func (params *CreateServiceConversationScopedWebhookParams) SetConfigurationMethod(ConfigurationMethod string) *CreateServiceConversationScopedWebhookParams {
    53  	params.ConfigurationMethod = &ConfigurationMethod
    54  	return params
    55  }
    56  func (params *CreateServiceConversationScopedWebhookParams) SetConfigurationFilters(ConfigurationFilters []string) *CreateServiceConversationScopedWebhookParams {
    57  	params.ConfigurationFilters = &ConfigurationFilters
    58  	return params
    59  }
    60  func (params *CreateServiceConversationScopedWebhookParams) SetConfigurationTriggers(ConfigurationTriggers []string) *CreateServiceConversationScopedWebhookParams {
    61  	params.ConfigurationTriggers = &ConfigurationTriggers
    62  	return params
    63  }
    64  func (params *CreateServiceConversationScopedWebhookParams) SetConfigurationFlowSid(ConfigurationFlowSid string) *CreateServiceConversationScopedWebhookParams {
    65  	params.ConfigurationFlowSid = &ConfigurationFlowSid
    66  	return params
    67  }
    68  func (params *CreateServiceConversationScopedWebhookParams) SetConfigurationReplayAfter(ConfigurationReplayAfter int) *CreateServiceConversationScopedWebhookParams {
    69  	params.ConfigurationReplayAfter = &ConfigurationReplayAfter
    70  	return params
    71  }
    72  
    73  // Create a new webhook scoped to the conversation in a specific service
    74  func (c *ApiService) CreateServiceConversationScopedWebhook(ChatServiceSid string, ConversationSid string, params *CreateServiceConversationScopedWebhookParams) (*ConversationsV1ServiceConversationScopedWebhook, error) {
    75  	path := "/v1/Services/{ChatServiceSid}/Conversations/{ConversationSid}/Webhooks"
    76  	path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1)
    77  	path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1)
    78  
    79  	data := url.Values{}
    80  	headers := make(map[string]interface{})
    81  
    82  	if params != nil && params.Target != nil {
    83  		data.Set("Target", *params.Target)
    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.ConfigurationReplayAfter != nil {
   105  		data.Set("Configuration.ReplayAfter", fmt.Sprint(*params.ConfigurationReplayAfter))
   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 := &ConversationsV1ServiceConversationScopedWebhook{}
   116  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	return ps, err
   121  }
   122  
   123  // Remove an existing webhook scoped to the conversation
   124  func (c *ApiService) DeleteServiceConversationScopedWebhook(ChatServiceSid string, ConversationSid string, Sid string) error {
   125  	path := "/v1/Services/{ChatServiceSid}/Conversations/{ConversationSid}/Webhooks/{Sid}"
   126  	path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1)
   127  	path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -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  // Fetch the configuration of a conversation-scoped webhook
   144  func (c *ApiService) FetchServiceConversationScopedWebhook(ChatServiceSid string, ConversationSid string, Sid string) (*ConversationsV1ServiceConversationScopedWebhook, error) {
   145  	path := "/v1/Services/{ChatServiceSid}/Conversations/{ConversationSid}/Webhooks/{Sid}"
   146  	path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1)
   147  	path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -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 := &ConversationsV1ServiceConversationScopedWebhook{}
   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 'ListServiceConversationScopedWebhook'
   169  type ListServiceConversationScopedWebhookParams 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 *ListServiceConversationScopedWebhookParams) SetPageSize(PageSize int) *ListServiceConversationScopedWebhookParams {
   177  	params.PageSize = &PageSize
   178  	return params
   179  }
   180  func (params *ListServiceConversationScopedWebhookParams) SetLimit(Limit int) *ListServiceConversationScopedWebhookParams {
   181  	params.Limit = &Limit
   182  	return params
   183  }
   184  
   185  // Retrieve a single page of ServiceConversationScopedWebhook records from the API. Request is executed immediately.
   186  func (c *ApiService) PageServiceConversationScopedWebhook(ChatServiceSid string, ConversationSid string, params *ListServiceConversationScopedWebhookParams, pageToken, pageNumber string) (*ListServiceConversationScopedWebhookResponse, error) {
   187  	path := "/v1/Services/{ChatServiceSid}/Conversations/{ConversationSid}/Webhooks"
   188  
   189  	path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1)
   190  	path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -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 := &ListServiceConversationScopedWebhookResponse{}
   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 ServiceConversationScopedWebhook 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) ListServiceConversationScopedWebhook(ChatServiceSid string, ConversationSid string, params *ListServiceConversationScopedWebhookParams) ([]ConversationsV1ServiceConversationScopedWebhook, error) {
   223  	response, errors := c.StreamServiceConversationScopedWebhook(ChatServiceSid, ConversationSid, params)
   224  
   225  	records := make([]ConversationsV1ServiceConversationScopedWebhook, 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 ServiceConversationScopedWebhook 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) StreamServiceConversationScopedWebhook(ChatServiceSid string, ConversationSid string, params *ListServiceConversationScopedWebhookParams) (chan ConversationsV1ServiceConversationScopedWebhook, chan error) {
   239  	if params == nil {
   240  		params = &ListServiceConversationScopedWebhookParams{}
   241  	}
   242  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   243  
   244  	recordChannel := make(chan ConversationsV1ServiceConversationScopedWebhook, 1)
   245  	errorChannel := make(chan error, 1)
   246  
   247  	response, err := c.PageServiceConversationScopedWebhook(ChatServiceSid, ConversationSid, params, "", "")
   248  	if err != nil {
   249  		errorChannel <- err
   250  		close(recordChannel)
   251  		close(errorChannel)
   252  	} else {
   253  		go c.streamServiceConversationScopedWebhook(response, params, recordChannel, errorChannel)
   254  	}
   255  
   256  	return recordChannel, errorChannel
   257  }
   258  
   259  func (c *ApiService) streamServiceConversationScopedWebhook(response *ListServiceConversationScopedWebhookResponse, params *ListServiceConversationScopedWebhookParams, recordChannel chan ConversationsV1ServiceConversationScopedWebhook, 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.getNextListServiceConversationScopedWebhookResponse)
   275  		if err != nil {
   276  			errorChannel <- err
   277  			break
   278  		} else if record == nil {
   279  			break
   280  		}
   281  
   282  		response = record.(*ListServiceConversationScopedWebhookResponse)
   283  	}
   284  
   285  	close(recordChannel)
   286  	close(errorChannel)
   287  }
   288  
   289  func (c *ApiService) getNextListServiceConversationScopedWebhookResponse(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 := &ListServiceConversationScopedWebhookResponse{}
   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 'UpdateServiceConversationScopedWebhook'
   308  type UpdateServiceConversationScopedWebhookParams struct {
   309  	// The absolute url the webhook request should be sent to.
   310  	ConfigurationUrl *string `json:"Configuration.Url,omitempty"`
   311  	//
   312  	ConfigurationMethod *string `json:"Configuration.Method,omitempty"`
   313  	// The list of events, firing webhook event for this Conversation.
   314  	ConfigurationFilters *[]string `json:"Configuration.Filters,omitempty"`
   315  	// The list of keywords, firing webhook event for this Conversation.
   316  	ConfigurationTriggers *[]string `json:"Configuration.Triggers,omitempty"`
   317  	// The studio flow SID, where the webhook should be sent to.
   318  	ConfigurationFlowSid *string `json:"Configuration.FlowSid,omitempty"`
   319  }
   320  
   321  func (params *UpdateServiceConversationScopedWebhookParams) SetConfigurationUrl(ConfigurationUrl string) *UpdateServiceConversationScopedWebhookParams {
   322  	params.ConfigurationUrl = &ConfigurationUrl
   323  	return params
   324  }
   325  func (params *UpdateServiceConversationScopedWebhookParams) SetConfigurationMethod(ConfigurationMethod string) *UpdateServiceConversationScopedWebhookParams {
   326  	params.ConfigurationMethod = &ConfigurationMethod
   327  	return params
   328  }
   329  func (params *UpdateServiceConversationScopedWebhookParams) SetConfigurationFilters(ConfigurationFilters []string) *UpdateServiceConversationScopedWebhookParams {
   330  	params.ConfigurationFilters = &ConfigurationFilters
   331  	return params
   332  }
   333  func (params *UpdateServiceConversationScopedWebhookParams) SetConfigurationTriggers(ConfigurationTriggers []string) *UpdateServiceConversationScopedWebhookParams {
   334  	params.ConfigurationTriggers = &ConfigurationTriggers
   335  	return params
   336  }
   337  func (params *UpdateServiceConversationScopedWebhookParams) SetConfigurationFlowSid(ConfigurationFlowSid string) *UpdateServiceConversationScopedWebhookParams {
   338  	params.ConfigurationFlowSid = &ConfigurationFlowSid
   339  	return params
   340  }
   341  
   342  // Update an existing conversation-scoped webhook
   343  func (c *ApiService) UpdateServiceConversationScopedWebhook(ChatServiceSid string, ConversationSid string, Sid string, params *UpdateServiceConversationScopedWebhookParams) (*ConversationsV1ServiceConversationScopedWebhook, error) {
   344  	path := "/v1/Services/{ChatServiceSid}/Conversations/{ConversationSid}/Webhooks/{Sid}"
   345  	path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1)
   346  	path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1)
   347  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   348  
   349  	data := url.Values{}
   350  	headers := make(map[string]interface{})
   351  
   352  	if params != nil && params.ConfigurationUrl != nil {
   353  		data.Set("Configuration.Url", *params.ConfigurationUrl)
   354  	}
   355  	if params != nil && params.ConfigurationMethod != nil {
   356  		data.Set("Configuration.Method", *params.ConfigurationMethod)
   357  	}
   358  	if params != nil && params.ConfigurationFilters != nil {
   359  		for _, item := range *params.ConfigurationFilters {
   360  			data.Add("Configuration.Filters", item)
   361  		}
   362  	}
   363  	if params != nil && params.ConfigurationTriggers != nil {
   364  		for _, item := range *params.ConfigurationTriggers {
   365  			data.Add("Configuration.Triggers", item)
   366  		}
   367  	}
   368  	if params != nil && params.ConfigurationFlowSid != nil {
   369  		data.Set("Configuration.FlowSid", *params.ConfigurationFlowSid)
   370  	}
   371  
   372  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   373  	if err != nil {
   374  		return nil, err
   375  	}
   376  
   377  	defer resp.Body.Close()
   378  
   379  	ps := &ConversationsV1ServiceConversationScopedWebhook{}
   380  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   381  		return nil, err
   382  	}
   383  
   384  	return ps, err
   385  }