github.com/twilio/twilio-go@v1.20.1/rest/conversations/v1/conversations_messages.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 'CreateConversationMessage'
    28  type CreateConversationMessageParams struct {
    29  	// The X-Twilio-Webhook-Enabled HTTP request header
    30  	XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"`
    31  	// The channel specific identifier of the message's author. Defaults to `system`.
    32  	Author *string `json:"Author,omitempty"`
    33  	// The content of the message, can be up to 1,600 characters long.
    34  	Body *string `json:"Body,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. `null` if the message has not been edited.
    38  	DateUpdated *time.Time `json:"DateUpdated,omitempty"`
    39  	// A 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.
    40  	Attributes *string `json:"Attributes,omitempty"`
    41  	// The Media SID to be attached to the new Message.
    42  	MediaSid *string `json:"MediaSid,omitempty"`
    43  	// The unique ID of the multi-channel [Rich Content](https://www.twilio.com/docs/content) template, required for template-generated messages.  **Note** that if this field is set, `Body` and `MediaSid` parameters are ignored.
    44  	ContentSid *string `json:"ContentSid,omitempty"`
    45  	// A structurally valid JSON string that contains values to resolve Rich Content template variables.
    46  	ContentVariables *string `json:"ContentVariables,omitempty"`
    47  	// The subject of the message, can be up to 256 characters long.
    48  	Subject *string `json:"Subject,omitempty"`
    49  }
    50  
    51  func (params *CreateConversationMessageParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *CreateConversationMessageParams {
    52  	params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled
    53  	return params
    54  }
    55  func (params *CreateConversationMessageParams) SetAuthor(Author string) *CreateConversationMessageParams {
    56  	params.Author = &Author
    57  	return params
    58  }
    59  func (params *CreateConversationMessageParams) SetBody(Body string) *CreateConversationMessageParams {
    60  	params.Body = &Body
    61  	return params
    62  }
    63  func (params *CreateConversationMessageParams) SetDateCreated(DateCreated time.Time) *CreateConversationMessageParams {
    64  	params.DateCreated = &DateCreated
    65  	return params
    66  }
    67  func (params *CreateConversationMessageParams) SetDateUpdated(DateUpdated time.Time) *CreateConversationMessageParams {
    68  	params.DateUpdated = &DateUpdated
    69  	return params
    70  }
    71  func (params *CreateConversationMessageParams) SetAttributes(Attributes string) *CreateConversationMessageParams {
    72  	params.Attributes = &Attributes
    73  	return params
    74  }
    75  func (params *CreateConversationMessageParams) SetMediaSid(MediaSid string) *CreateConversationMessageParams {
    76  	params.MediaSid = &MediaSid
    77  	return params
    78  }
    79  func (params *CreateConversationMessageParams) SetContentSid(ContentSid string) *CreateConversationMessageParams {
    80  	params.ContentSid = &ContentSid
    81  	return params
    82  }
    83  func (params *CreateConversationMessageParams) SetContentVariables(ContentVariables string) *CreateConversationMessageParams {
    84  	params.ContentVariables = &ContentVariables
    85  	return params
    86  }
    87  func (params *CreateConversationMessageParams) SetSubject(Subject string) *CreateConversationMessageParams {
    88  	params.Subject = &Subject
    89  	return params
    90  }
    91  
    92  // Add a new message to the conversation
    93  func (c *ApiService) CreateConversationMessage(ConversationSid string, params *CreateConversationMessageParams) (*ConversationsV1ConversationMessage, error) {
    94  	path := "/v1/Conversations/{ConversationSid}/Messages"
    95  	path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1)
    96  
    97  	data := url.Values{}
    98  	headers := make(map[string]interface{})
    99  
   100  	if params != nil && params.Author != nil {
   101  		data.Set("Author", *params.Author)
   102  	}
   103  	if params != nil && params.Body != nil {
   104  		data.Set("Body", *params.Body)
   105  	}
   106  	if params != nil && params.DateCreated != nil {
   107  		data.Set("DateCreated", fmt.Sprint((*params.DateCreated).Format(time.RFC3339)))
   108  	}
   109  	if params != nil && params.DateUpdated != nil {
   110  		data.Set("DateUpdated", fmt.Sprint((*params.DateUpdated).Format(time.RFC3339)))
   111  	}
   112  	if params != nil && params.Attributes != nil {
   113  		data.Set("Attributes", *params.Attributes)
   114  	}
   115  	if params != nil && params.MediaSid != nil {
   116  		data.Set("MediaSid", *params.MediaSid)
   117  	}
   118  	if params != nil && params.ContentSid != nil {
   119  		data.Set("ContentSid", *params.ContentSid)
   120  	}
   121  	if params != nil && params.ContentVariables != nil {
   122  		data.Set("ContentVariables", *params.ContentVariables)
   123  	}
   124  	if params != nil && params.Subject != nil {
   125  		data.Set("Subject", *params.Subject)
   126  	}
   127  
   128  	if params != nil && params.XTwilioWebhookEnabled != nil {
   129  		headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled
   130  	}
   131  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  
   136  	defer resp.Body.Close()
   137  
   138  	ps := &ConversationsV1ConversationMessage{}
   139  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   140  		return nil, err
   141  	}
   142  
   143  	return ps, err
   144  }
   145  
   146  // Optional parameters for the method 'DeleteConversationMessage'
   147  type DeleteConversationMessageParams struct {
   148  	// The X-Twilio-Webhook-Enabled HTTP request header
   149  	XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"`
   150  }
   151  
   152  func (params *DeleteConversationMessageParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *DeleteConversationMessageParams {
   153  	params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled
   154  	return params
   155  }
   156  
   157  // Remove a message from the conversation
   158  func (c *ApiService) DeleteConversationMessage(ConversationSid string, Sid string, params *DeleteConversationMessageParams) error {
   159  	path := "/v1/Conversations/{ConversationSid}/Messages/{Sid}"
   160  	path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1)
   161  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   162  
   163  	data := url.Values{}
   164  	headers := make(map[string]interface{})
   165  
   166  	if params != nil && params.XTwilioWebhookEnabled != nil {
   167  		headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled
   168  	}
   169  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   170  	if err != nil {
   171  		return err
   172  	}
   173  
   174  	defer resp.Body.Close()
   175  
   176  	return nil
   177  }
   178  
   179  // Fetch a message from the conversation
   180  func (c *ApiService) FetchConversationMessage(ConversationSid string, Sid string) (*ConversationsV1ConversationMessage, error) {
   181  	path := "/v1/Conversations/{ConversationSid}/Messages/{Sid}"
   182  	path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1)
   183  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   184  
   185  	data := url.Values{}
   186  	headers := make(map[string]interface{})
   187  
   188  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   189  	if err != nil {
   190  		return nil, err
   191  	}
   192  
   193  	defer resp.Body.Close()
   194  
   195  	ps := &ConversationsV1ConversationMessage{}
   196  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   197  		return nil, err
   198  	}
   199  
   200  	return ps, err
   201  }
   202  
   203  // Optional parameters for the method 'ListConversationMessage'
   204  type ListConversationMessageParams struct {
   205  	// The sort order of the returned messages. Can be: `asc` (ascending) or `desc` (descending), with `asc` as the default.
   206  	Order *string `json:"Order,omitempty"`
   207  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   208  	PageSize *int `json:"PageSize,omitempty"`
   209  	// Max number of records to return.
   210  	Limit *int `json:"limit,omitempty"`
   211  }
   212  
   213  func (params *ListConversationMessageParams) SetOrder(Order string) *ListConversationMessageParams {
   214  	params.Order = &Order
   215  	return params
   216  }
   217  func (params *ListConversationMessageParams) SetPageSize(PageSize int) *ListConversationMessageParams {
   218  	params.PageSize = &PageSize
   219  	return params
   220  }
   221  func (params *ListConversationMessageParams) SetLimit(Limit int) *ListConversationMessageParams {
   222  	params.Limit = &Limit
   223  	return params
   224  }
   225  
   226  // Retrieve a single page of ConversationMessage records from the API. Request is executed immediately.
   227  func (c *ApiService) PageConversationMessage(ConversationSid string, params *ListConversationMessageParams, pageToken, pageNumber string) (*ListConversationMessageResponse, error) {
   228  	path := "/v1/Conversations/{ConversationSid}/Messages"
   229  
   230  	path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1)
   231  
   232  	data := url.Values{}
   233  	headers := make(map[string]interface{})
   234  
   235  	if params != nil && params.Order != nil {
   236  		data.Set("Order", *params.Order)
   237  	}
   238  	if params != nil && params.PageSize != nil {
   239  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   240  	}
   241  
   242  	if pageToken != "" {
   243  		data.Set("PageToken", pageToken)
   244  	}
   245  	if pageNumber != "" {
   246  		data.Set("Page", pageNumber)
   247  	}
   248  
   249  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   250  	if err != nil {
   251  		return nil, err
   252  	}
   253  
   254  	defer resp.Body.Close()
   255  
   256  	ps := &ListConversationMessageResponse{}
   257  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   258  		return nil, err
   259  	}
   260  
   261  	return ps, err
   262  }
   263  
   264  // Lists ConversationMessage records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   265  func (c *ApiService) ListConversationMessage(ConversationSid string, params *ListConversationMessageParams) ([]ConversationsV1ConversationMessage, error) {
   266  	response, errors := c.StreamConversationMessage(ConversationSid, params)
   267  
   268  	records := make([]ConversationsV1ConversationMessage, 0)
   269  	for record := range response {
   270  		records = append(records, record)
   271  	}
   272  
   273  	if err := <-errors; err != nil {
   274  		return nil, err
   275  	}
   276  
   277  	return records, nil
   278  }
   279  
   280  // Streams ConversationMessage records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   281  func (c *ApiService) StreamConversationMessage(ConversationSid string, params *ListConversationMessageParams) (chan ConversationsV1ConversationMessage, chan error) {
   282  	if params == nil {
   283  		params = &ListConversationMessageParams{}
   284  	}
   285  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   286  
   287  	recordChannel := make(chan ConversationsV1ConversationMessage, 1)
   288  	errorChannel := make(chan error, 1)
   289  
   290  	response, err := c.PageConversationMessage(ConversationSid, params, "", "")
   291  	if err != nil {
   292  		errorChannel <- err
   293  		close(recordChannel)
   294  		close(errorChannel)
   295  	} else {
   296  		go c.streamConversationMessage(response, params, recordChannel, errorChannel)
   297  	}
   298  
   299  	return recordChannel, errorChannel
   300  }
   301  
   302  func (c *ApiService) streamConversationMessage(response *ListConversationMessageResponse, params *ListConversationMessageParams, recordChannel chan ConversationsV1ConversationMessage, errorChannel chan error) {
   303  	curRecord := 1
   304  
   305  	for response != nil {
   306  		responseRecords := response.Messages
   307  		for item := range responseRecords {
   308  			recordChannel <- responseRecords[item]
   309  			curRecord += 1
   310  			if params.Limit != nil && *params.Limit < curRecord {
   311  				close(recordChannel)
   312  				close(errorChannel)
   313  				return
   314  			}
   315  		}
   316  
   317  		record, err := client.GetNext(c.baseURL, response, c.getNextListConversationMessageResponse)
   318  		if err != nil {
   319  			errorChannel <- err
   320  			break
   321  		} else if record == nil {
   322  			break
   323  		}
   324  
   325  		response = record.(*ListConversationMessageResponse)
   326  	}
   327  
   328  	close(recordChannel)
   329  	close(errorChannel)
   330  }
   331  
   332  func (c *ApiService) getNextListConversationMessageResponse(nextPageUrl string) (interface{}, error) {
   333  	if nextPageUrl == "" {
   334  		return nil, nil
   335  	}
   336  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   337  	if err != nil {
   338  		return nil, err
   339  	}
   340  
   341  	defer resp.Body.Close()
   342  
   343  	ps := &ListConversationMessageResponse{}
   344  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   345  		return nil, err
   346  	}
   347  	return ps, nil
   348  }
   349  
   350  // Optional parameters for the method 'UpdateConversationMessage'
   351  type UpdateConversationMessageParams struct {
   352  	// The X-Twilio-Webhook-Enabled HTTP request header
   353  	XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"`
   354  	// The channel specific identifier of the message's author. Defaults to `system`.
   355  	Author *string `json:"Author,omitempty"`
   356  	// The content of the message, can be up to 1,600 characters long.
   357  	Body *string `json:"Body,omitempty"`
   358  	// The date that this resource was created.
   359  	DateCreated *time.Time `json:"DateCreated,omitempty"`
   360  	// The date that this resource was last updated. `null` if the message has not been edited.
   361  	DateUpdated *time.Time `json:"DateUpdated,omitempty"`
   362  	// A 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.
   363  	Attributes *string `json:"Attributes,omitempty"`
   364  	// The subject of the message, can be up to 256 characters long.
   365  	Subject *string `json:"Subject,omitempty"`
   366  }
   367  
   368  func (params *UpdateConversationMessageParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *UpdateConversationMessageParams {
   369  	params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled
   370  	return params
   371  }
   372  func (params *UpdateConversationMessageParams) SetAuthor(Author string) *UpdateConversationMessageParams {
   373  	params.Author = &Author
   374  	return params
   375  }
   376  func (params *UpdateConversationMessageParams) SetBody(Body string) *UpdateConversationMessageParams {
   377  	params.Body = &Body
   378  	return params
   379  }
   380  func (params *UpdateConversationMessageParams) SetDateCreated(DateCreated time.Time) *UpdateConversationMessageParams {
   381  	params.DateCreated = &DateCreated
   382  	return params
   383  }
   384  func (params *UpdateConversationMessageParams) SetDateUpdated(DateUpdated time.Time) *UpdateConversationMessageParams {
   385  	params.DateUpdated = &DateUpdated
   386  	return params
   387  }
   388  func (params *UpdateConversationMessageParams) SetAttributes(Attributes string) *UpdateConversationMessageParams {
   389  	params.Attributes = &Attributes
   390  	return params
   391  }
   392  func (params *UpdateConversationMessageParams) SetSubject(Subject string) *UpdateConversationMessageParams {
   393  	params.Subject = &Subject
   394  	return params
   395  }
   396  
   397  // Update an existing message in the conversation
   398  func (c *ApiService) UpdateConversationMessage(ConversationSid string, Sid string, params *UpdateConversationMessageParams) (*ConversationsV1ConversationMessage, error) {
   399  	path := "/v1/Conversations/{ConversationSid}/Messages/{Sid}"
   400  	path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1)
   401  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   402  
   403  	data := url.Values{}
   404  	headers := make(map[string]interface{})
   405  
   406  	if params != nil && params.Author != nil {
   407  		data.Set("Author", *params.Author)
   408  	}
   409  	if params != nil && params.Body != nil {
   410  		data.Set("Body", *params.Body)
   411  	}
   412  	if params != nil && params.DateCreated != nil {
   413  		data.Set("DateCreated", fmt.Sprint((*params.DateCreated).Format(time.RFC3339)))
   414  	}
   415  	if params != nil && params.DateUpdated != nil {
   416  		data.Set("DateUpdated", fmt.Sprint((*params.DateUpdated).Format(time.RFC3339)))
   417  	}
   418  	if params != nil && params.Attributes != nil {
   419  		data.Set("Attributes", *params.Attributes)
   420  	}
   421  	if params != nil && params.Subject != nil {
   422  		data.Set("Subject", *params.Subject)
   423  	}
   424  
   425  	if params != nil && params.XTwilioWebhookEnabled != nil {
   426  		headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled
   427  	}
   428  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   429  	if err != nil {
   430  		return nil, err
   431  	}
   432  
   433  	defer resp.Body.Close()
   434  
   435  	ps := &ConversationsV1ConversationMessage{}
   436  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   437  		return nil, err
   438  	}
   439  
   440  	return ps, err
   441  }