github.com/twilio/twilio-go@v1.20.1/rest/chat/v1/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  
    23  	"github.com/twilio/twilio-go/client"
    24  )
    25  
    26  // Optional parameters for the method 'CreateMessage'
    27  type CreateMessageParams struct {
    28  	// The message to send to the channel. Can also 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.
    29  	Body *string `json:"Body,omitempty"`
    30  	// The [identity](https://www.twilio.com/docs/api/chat/guides/identity) of the new message's author. The default value is `system`.
    31  	From *string `json:"From,omitempty"`
    32  	// A valid JSON string that contains application-specific data.
    33  	Attributes *string `json:"Attributes,omitempty"`
    34  }
    35  
    36  func (params *CreateMessageParams) SetBody(Body string) *CreateMessageParams {
    37  	params.Body = &Body
    38  	return params
    39  }
    40  func (params *CreateMessageParams) SetFrom(From string) *CreateMessageParams {
    41  	params.From = &From
    42  	return params
    43  }
    44  func (params *CreateMessageParams) SetAttributes(Attributes string) *CreateMessageParams {
    45  	params.Attributes = &Attributes
    46  	return params
    47  }
    48  
    49  //
    50  func (c *ApiService) CreateMessage(ServiceSid string, ChannelSid string, params *CreateMessageParams) (*ChatV1Message, error) {
    51  	path := "/v1/Services/{ServiceSid}/Channels/{ChannelSid}/Messages"
    52  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    53  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
    54  
    55  	data := url.Values{}
    56  	headers := make(map[string]interface{})
    57  
    58  	if params != nil && params.Body != nil {
    59  		data.Set("Body", *params.Body)
    60  	}
    61  	if params != nil && params.From != nil {
    62  		data.Set("From", *params.From)
    63  	}
    64  	if params != nil && params.Attributes != nil {
    65  		data.Set("Attributes", *params.Attributes)
    66  	}
    67  
    68  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	defer resp.Body.Close()
    74  
    75  	ps := &ChatV1Message{}
    76  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	return ps, err
    81  }
    82  
    83  //
    84  func (c *ApiService) DeleteMessage(ServiceSid string, ChannelSid string, Sid string) error {
    85  	path := "/v1/Services/{ServiceSid}/Channels/{ChannelSid}/Messages/{Sid}"
    86  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    87  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
    88  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    89  
    90  	data := url.Values{}
    91  	headers := make(map[string]interface{})
    92  
    93  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	defer resp.Body.Close()
    99  
   100  	return nil
   101  }
   102  
   103  //
   104  func (c *ApiService) FetchMessage(ServiceSid string, ChannelSid string, Sid string) (*ChatV1Message, error) {
   105  	path := "/v1/Services/{ServiceSid}/Channels/{ChannelSid}/Messages/{Sid}"
   106  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   107  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
   108  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   109  
   110  	data := url.Values{}
   111  	headers := make(map[string]interface{})
   112  
   113  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   114  	if err != nil {
   115  		return nil, err
   116  	}
   117  
   118  	defer resp.Body.Close()
   119  
   120  	ps := &ChatV1Message{}
   121  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   122  		return nil, err
   123  	}
   124  
   125  	return ps, err
   126  }
   127  
   128  // Optional parameters for the method 'ListMessage'
   129  type ListMessageParams struct {
   130  	// The sort order of the returned messages. Can be: `asc` (ascending) or `desc` (descending) with `asc` as the default.
   131  	Order *string `json:"Order,omitempty"`
   132  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   133  	PageSize *int `json:"PageSize,omitempty"`
   134  	// Max number of records to return.
   135  	Limit *int `json:"limit,omitempty"`
   136  }
   137  
   138  func (params *ListMessageParams) SetOrder(Order string) *ListMessageParams {
   139  	params.Order = &Order
   140  	return params
   141  }
   142  func (params *ListMessageParams) SetPageSize(PageSize int) *ListMessageParams {
   143  	params.PageSize = &PageSize
   144  	return params
   145  }
   146  func (params *ListMessageParams) SetLimit(Limit int) *ListMessageParams {
   147  	params.Limit = &Limit
   148  	return params
   149  }
   150  
   151  // Retrieve a single page of Message records from the API. Request is executed immediately.
   152  func (c *ApiService) PageMessage(ServiceSid string, ChannelSid string, params *ListMessageParams, pageToken, pageNumber string) (*ListMessageResponse, error) {
   153  	path := "/v1/Services/{ServiceSid}/Channels/{ChannelSid}/Messages"
   154  
   155  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   156  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
   157  
   158  	data := url.Values{}
   159  	headers := make(map[string]interface{})
   160  
   161  	if params != nil && params.Order != nil {
   162  		data.Set("Order", *params.Order)
   163  	}
   164  	if params != nil && params.PageSize != nil {
   165  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   166  	}
   167  
   168  	if pageToken != "" {
   169  		data.Set("PageToken", pageToken)
   170  	}
   171  	if pageNumber != "" {
   172  		data.Set("Page", pageNumber)
   173  	}
   174  
   175  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   176  	if err != nil {
   177  		return nil, err
   178  	}
   179  
   180  	defer resp.Body.Close()
   181  
   182  	ps := &ListMessageResponse{}
   183  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   184  		return nil, err
   185  	}
   186  
   187  	return ps, err
   188  }
   189  
   190  // Lists Message records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   191  func (c *ApiService) ListMessage(ServiceSid string, ChannelSid string, params *ListMessageParams) ([]ChatV1Message, error) {
   192  	response, errors := c.StreamMessage(ServiceSid, ChannelSid, params)
   193  
   194  	records := make([]ChatV1Message, 0)
   195  	for record := range response {
   196  		records = append(records, record)
   197  	}
   198  
   199  	if err := <-errors; err != nil {
   200  		return nil, err
   201  	}
   202  
   203  	return records, nil
   204  }
   205  
   206  // Streams Message records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   207  func (c *ApiService) StreamMessage(ServiceSid string, ChannelSid string, params *ListMessageParams) (chan ChatV1Message, chan error) {
   208  	if params == nil {
   209  		params = &ListMessageParams{}
   210  	}
   211  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   212  
   213  	recordChannel := make(chan ChatV1Message, 1)
   214  	errorChannel := make(chan error, 1)
   215  
   216  	response, err := c.PageMessage(ServiceSid, ChannelSid, params, "", "")
   217  	if err != nil {
   218  		errorChannel <- err
   219  		close(recordChannel)
   220  		close(errorChannel)
   221  	} else {
   222  		go c.streamMessage(response, params, recordChannel, errorChannel)
   223  	}
   224  
   225  	return recordChannel, errorChannel
   226  }
   227  
   228  func (c *ApiService) streamMessage(response *ListMessageResponse, params *ListMessageParams, recordChannel chan ChatV1Message, errorChannel chan error) {
   229  	curRecord := 1
   230  
   231  	for response != nil {
   232  		responseRecords := response.Messages
   233  		for item := range responseRecords {
   234  			recordChannel <- responseRecords[item]
   235  			curRecord += 1
   236  			if params.Limit != nil && *params.Limit < curRecord {
   237  				close(recordChannel)
   238  				close(errorChannel)
   239  				return
   240  			}
   241  		}
   242  
   243  		record, err := client.GetNext(c.baseURL, response, c.getNextListMessageResponse)
   244  		if err != nil {
   245  			errorChannel <- err
   246  			break
   247  		} else if record == nil {
   248  			break
   249  		}
   250  
   251  		response = record.(*ListMessageResponse)
   252  	}
   253  
   254  	close(recordChannel)
   255  	close(errorChannel)
   256  }
   257  
   258  func (c *ApiService) getNextListMessageResponse(nextPageUrl string) (interface{}, error) {
   259  	if nextPageUrl == "" {
   260  		return nil, nil
   261  	}
   262  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   263  	if err != nil {
   264  		return nil, err
   265  	}
   266  
   267  	defer resp.Body.Close()
   268  
   269  	ps := &ListMessageResponse{}
   270  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   271  		return nil, err
   272  	}
   273  	return ps, nil
   274  }
   275  
   276  // Optional parameters for the method 'UpdateMessage'
   277  type UpdateMessageParams struct {
   278  	// The message to send to the channel. Can also 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.
   279  	Body *string `json:"Body,omitempty"`
   280  	// A valid JSON string that contains application-specific data.
   281  	Attributes *string `json:"Attributes,omitempty"`
   282  }
   283  
   284  func (params *UpdateMessageParams) SetBody(Body string) *UpdateMessageParams {
   285  	params.Body = &Body
   286  	return params
   287  }
   288  func (params *UpdateMessageParams) SetAttributes(Attributes string) *UpdateMessageParams {
   289  	params.Attributes = &Attributes
   290  	return params
   291  }
   292  
   293  //
   294  func (c *ApiService) UpdateMessage(ServiceSid string, ChannelSid string, Sid string, params *UpdateMessageParams) (*ChatV1Message, error) {
   295  	path := "/v1/Services/{ServiceSid}/Channels/{ChannelSid}/Messages/{Sid}"
   296  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   297  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
   298  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   299  
   300  	data := url.Values{}
   301  	headers := make(map[string]interface{})
   302  
   303  	if params != nil && params.Body != nil {
   304  		data.Set("Body", *params.Body)
   305  	}
   306  	if params != nil && params.Attributes != nil {
   307  		data.Set("Attributes", *params.Attributes)
   308  	}
   309  
   310  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   311  	if err != nil {
   312  		return nil, err
   313  	}
   314  
   315  	defer resp.Body.Close()
   316  
   317  	ps := &ChatV1Message{}
   318  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   319  		return nil, err
   320  	}
   321  
   322  	return ps, err
   323  }