github.com/twilio/twilio-go@v1.20.1/rest/chat/v2/services_channels_messages.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  	"time"
    23  
    24  	"github.com/twilio/twilio-go/client"
    25  )
    26  
    27  // Optional parameters for the method 'CreateMessage'
    28  type CreateMessageParams struct {
    29  	// The X-Twilio-Webhook-Enabled HTTP request header
    30  	XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"`
    31  	// The [Identity](https://www.twilio.com/docs/chat/identity) of the new message's author. The default value is `system`.
    32  	From *string `json:"From,omitempty"`
    33  	// A valid JSON string that contains application-specific data.
    34  	Attributes *string `json:"Attributes,omitempty"`
    35  	// The date, specified in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format, to assign to the resource as the date it was created. The default value is the current time set by the Chat service. This parameter should only be used when a Chat's history is being recreated from a backup/separate source.
    36  	DateCreated *time.Time `json:"DateCreated,omitempty"`
    37  	// The date, specified in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format, to assign to the resource as the date it was last updated.
    38  	DateUpdated *time.Time `json:"DateUpdated,omitempty"`
    39  	// The [Identity](https://www.twilio.com/docs/chat/identity) of the User who last updated the Message, if applicable.
    40  	LastUpdatedBy *string `json:"LastUpdatedBy,omitempty"`
    41  	// The message to send to the channel. Can be an empty string or `null`, which sets the value as an empty string. You can send structured data in the body by serializing it as a string.
    42  	Body *string `json:"Body,omitempty"`
    43  	// The SID of the [Media](https://www.twilio.com/docs/chat/rest/media) to attach to the new Message.
    44  	MediaSid *string `json:"MediaSid,omitempty"`
    45  }
    46  
    47  func (params *CreateMessageParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *CreateMessageParams {
    48  	params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled
    49  	return params
    50  }
    51  func (params *CreateMessageParams) SetFrom(From string) *CreateMessageParams {
    52  	params.From = &From
    53  	return params
    54  }
    55  func (params *CreateMessageParams) SetAttributes(Attributes string) *CreateMessageParams {
    56  	params.Attributes = &Attributes
    57  	return params
    58  }
    59  func (params *CreateMessageParams) SetDateCreated(DateCreated time.Time) *CreateMessageParams {
    60  	params.DateCreated = &DateCreated
    61  	return params
    62  }
    63  func (params *CreateMessageParams) SetDateUpdated(DateUpdated time.Time) *CreateMessageParams {
    64  	params.DateUpdated = &DateUpdated
    65  	return params
    66  }
    67  func (params *CreateMessageParams) SetLastUpdatedBy(LastUpdatedBy string) *CreateMessageParams {
    68  	params.LastUpdatedBy = &LastUpdatedBy
    69  	return params
    70  }
    71  func (params *CreateMessageParams) SetBody(Body string) *CreateMessageParams {
    72  	params.Body = &Body
    73  	return params
    74  }
    75  func (params *CreateMessageParams) SetMediaSid(MediaSid string) *CreateMessageParams {
    76  	params.MediaSid = &MediaSid
    77  	return params
    78  }
    79  
    80  //
    81  func (c *ApiService) CreateMessage(ServiceSid string, ChannelSid string, params *CreateMessageParams) (*ChatV2Message, error) {
    82  	path := "/v2/Services/{ServiceSid}/Channels/{ChannelSid}/Messages"
    83  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    84  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
    85  
    86  	data := url.Values{}
    87  	headers := make(map[string]interface{})
    88  
    89  	if params != nil && params.From != nil {
    90  		data.Set("From", *params.From)
    91  	}
    92  	if params != nil && params.Attributes != nil {
    93  		data.Set("Attributes", *params.Attributes)
    94  	}
    95  	if params != nil && params.DateCreated != nil {
    96  		data.Set("DateCreated", fmt.Sprint((*params.DateCreated).Format(time.RFC3339)))
    97  	}
    98  	if params != nil && params.DateUpdated != nil {
    99  		data.Set("DateUpdated", fmt.Sprint((*params.DateUpdated).Format(time.RFC3339)))
   100  	}
   101  	if params != nil && params.LastUpdatedBy != nil {
   102  		data.Set("LastUpdatedBy", *params.LastUpdatedBy)
   103  	}
   104  	if params != nil && params.Body != nil {
   105  		data.Set("Body", *params.Body)
   106  	}
   107  	if params != nil && params.MediaSid != nil {
   108  		data.Set("MediaSid", *params.MediaSid)
   109  	}
   110  
   111  	if params != nil && params.XTwilioWebhookEnabled != nil {
   112  		headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled
   113  	}
   114  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  
   119  	defer resp.Body.Close()
   120  
   121  	ps := &ChatV2Message{}
   122  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   123  		return nil, err
   124  	}
   125  
   126  	return ps, err
   127  }
   128  
   129  // Optional parameters for the method 'DeleteMessage'
   130  type DeleteMessageParams struct {
   131  	// The X-Twilio-Webhook-Enabled HTTP request header
   132  	XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"`
   133  }
   134  
   135  func (params *DeleteMessageParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *DeleteMessageParams {
   136  	params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled
   137  	return params
   138  }
   139  
   140  //
   141  func (c *ApiService) DeleteMessage(ServiceSid string, ChannelSid string, Sid string, params *DeleteMessageParams) error {
   142  	path := "/v2/Services/{ServiceSid}/Channels/{ChannelSid}/Messages/{Sid}"
   143  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   144  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
   145  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   146  
   147  	data := url.Values{}
   148  	headers := make(map[string]interface{})
   149  
   150  	if params != nil && params.XTwilioWebhookEnabled != nil {
   151  		headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled
   152  	}
   153  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   154  	if err != nil {
   155  		return err
   156  	}
   157  
   158  	defer resp.Body.Close()
   159  
   160  	return nil
   161  }
   162  
   163  //
   164  func (c *ApiService) FetchMessage(ServiceSid string, ChannelSid string, Sid string) (*ChatV2Message, error) {
   165  	path := "/v2/Services/{ServiceSid}/Channels/{ChannelSid}/Messages/{Sid}"
   166  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   167  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
   168  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   169  
   170  	data := url.Values{}
   171  	headers := make(map[string]interface{})
   172  
   173  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   174  	if err != nil {
   175  		return nil, err
   176  	}
   177  
   178  	defer resp.Body.Close()
   179  
   180  	ps := &ChatV2Message{}
   181  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   182  		return nil, err
   183  	}
   184  
   185  	return ps, err
   186  }
   187  
   188  // Optional parameters for the method 'ListMessage'
   189  type ListMessageParams struct {
   190  	// The sort order of the returned messages. Can be: `asc` (ascending) or `desc` (descending) with `asc` as the default.
   191  	Order *string `json:"Order,omitempty"`
   192  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   193  	PageSize *int `json:"PageSize,omitempty"`
   194  	// Max number of records to return.
   195  	Limit *int `json:"limit,omitempty"`
   196  }
   197  
   198  func (params *ListMessageParams) SetOrder(Order string) *ListMessageParams {
   199  	params.Order = &Order
   200  	return params
   201  }
   202  func (params *ListMessageParams) SetPageSize(PageSize int) *ListMessageParams {
   203  	params.PageSize = &PageSize
   204  	return params
   205  }
   206  func (params *ListMessageParams) SetLimit(Limit int) *ListMessageParams {
   207  	params.Limit = &Limit
   208  	return params
   209  }
   210  
   211  // Retrieve a single page of Message records from the API. Request is executed immediately.
   212  func (c *ApiService) PageMessage(ServiceSid string, ChannelSid string, params *ListMessageParams, pageToken, pageNumber string) (*ListMessageResponse, error) {
   213  	path := "/v2/Services/{ServiceSid}/Channels/{ChannelSid}/Messages"
   214  
   215  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   216  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
   217  
   218  	data := url.Values{}
   219  	headers := make(map[string]interface{})
   220  
   221  	if params != nil && params.Order != nil {
   222  		data.Set("Order", *params.Order)
   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 := &ListMessageResponse{}
   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 Message 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) ListMessage(ServiceSid string, ChannelSid string, params *ListMessageParams) ([]ChatV2Message, error) {
   252  	response, errors := c.StreamMessage(ServiceSid, ChannelSid, params)
   253  
   254  	records := make([]ChatV2Message, 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 Message 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) StreamMessage(ServiceSid string, ChannelSid string, params *ListMessageParams) (chan ChatV2Message, chan error) {
   268  	if params == nil {
   269  		params = &ListMessageParams{}
   270  	}
   271  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   272  
   273  	recordChannel := make(chan ChatV2Message, 1)
   274  	errorChannel := make(chan error, 1)
   275  
   276  	response, err := c.PageMessage(ServiceSid, ChannelSid, params, "", "")
   277  	if err != nil {
   278  		errorChannel <- err
   279  		close(recordChannel)
   280  		close(errorChannel)
   281  	} else {
   282  		go c.streamMessage(response, params, recordChannel, errorChannel)
   283  	}
   284  
   285  	return recordChannel, errorChannel
   286  }
   287  
   288  func (c *ApiService) streamMessage(response *ListMessageResponse, params *ListMessageParams, recordChannel chan ChatV2Message, errorChannel chan error) {
   289  	curRecord := 1
   290  
   291  	for response != nil {
   292  		responseRecords := response.Messages
   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.getNextListMessageResponse)
   304  		if err != nil {
   305  			errorChannel <- err
   306  			break
   307  		} else if record == nil {
   308  			break
   309  		}
   310  
   311  		response = record.(*ListMessageResponse)
   312  	}
   313  
   314  	close(recordChannel)
   315  	close(errorChannel)
   316  }
   317  
   318  func (c *ApiService) getNextListMessageResponse(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 := &ListMessageResponse{}
   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 'UpdateMessage'
   337  type UpdateMessageParams struct {
   338  	// The X-Twilio-Webhook-Enabled HTTP request header
   339  	XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"`
   340  	// The message to send to the channel. Can be an empty string or `null`, which sets the value as an empty string. You can send structured data in the body by serializing it as a string.
   341  	Body *string `json:"Body,omitempty"`
   342  	// A valid JSON string that contains application-specific data.
   343  	Attributes *string `json:"Attributes,omitempty"`
   344  	// The date, specified in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format, to assign to the resource as the date it was created. The default value is the current time set by the Chat service. This parameter should only be used when a Chat's history is being recreated from a backup/separate source.
   345  	DateCreated *time.Time `json:"DateCreated,omitempty"`
   346  	// The date, specified in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format, to assign to the resource as the date it was last updated.
   347  	DateUpdated *time.Time `json:"DateUpdated,omitempty"`
   348  	// The [Identity](https://www.twilio.com/docs/chat/identity) of the User who last updated the Message, if applicable.
   349  	LastUpdatedBy *string `json:"LastUpdatedBy,omitempty"`
   350  	// The [Identity](https://www.twilio.com/docs/chat/identity) of the message's author.
   351  	From *string `json:"From,omitempty"`
   352  }
   353  
   354  func (params *UpdateMessageParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *UpdateMessageParams {
   355  	params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled
   356  	return params
   357  }
   358  func (params *UpdateMessageParams) SetBody(Body string) *UpdateMessageParams {
   359  	params.Body = &Body
   360  	return params
   361  }
   362  func (params *UpdateMessageParams) SetAttributes(Attributes string) *UpdateMessageParams {
   363  	params.Attributes = &Attributes
   364  	return params
   365  }
   366  func (params *UpdateMessageParams) SetDateCreated(DateCreated time.Time) *UpdateMessageParams {
   367  	params.DateCreated = &DateCreated
   368  	return params
   369  }
   370  func (params *UpdateMessageParams) SetDateUpdated(DateUpdated time.Time) *UpdateMessageParams {
   371  	params.DateUpdated = &DateUpdated
   372  	return params
   373  }
   374  func (params *UpdateMessageParams) SetLastUpdatedBy(LastUpdatedBy string) *UpdateMessageParams {
   375  	params.LastUpdatedBy = &LastUpdatedBy
   376  	return params
   377  }
   378  func (params *UpdateMessageParams) SetFrom(From string) *UpdateMessageParams {
   379  	params.From = &From
   380  	return params
   381  }
   382  
   383  //
   384  func (c *ApiService) UpdateMessage(ServiceSid string, ChannelSid string, Sid string, params *UpdateMessageParams) (*ChatV2Message, error) {
   385  	path := "/v2/Services/{ServiceSid}/Channels/{ChannelSid}/Messages/{Sid}"
   386  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   387  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
   388  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   389  
   390  	data := url.Values{}
   391  	headers := make(map[string]interface{})
   392  
   393  	if params != nil && params.Body != nil {
   394  		data.Set("Body", *params.Body)
   395  	}
   396  	if params != nil && params.Attributes != nil {
   397  		data.Set("Attributes", *params.Attributes)
   398  	}
   399  	if params != nil && params.DateCreated != nil {
   400  		data.Set("DateCreated", fmt.Sprint((*params.DateCreated).Format(time.RFC3339)))
   401  	}
   402  	if params != nil && params.DateUpdated != nil {
   403  		data.Set("DateUpdated", fmt.Sprint((*params.DateUpdated).Format(time.RFC3339)))
   404  	}
   405  	if params != nil && params.LastUpdatedBy != nil {
   406  		data.Set("LastUpdatedBy", *params.LastUpdatedBy)
   407  	}
   408  	if params != nil && params.From != nil {
   409  		data.Set("From", *params.From)
   410  	}
   411  
   412  	if params != nil && params.XTwilioWebhookEnabled != nil {
   413  		headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled
   414  	}
   415  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   416  	if err != nil {
   417  		return nil, err
   418  	}
   419  
   420  	defer resp.Body.Close()
   421  
   422  	ps := &ChatV2Message{}
   423  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   424  		return nil, err
   425  	}
   426  
   427  	return ps, err
   428  }