github.com/twilio/twilio-go@v1.20.1/rest/conversations/v1/conversations_participants.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 'CreateConversationParticipant'
    28  type CreateConversationParticipantParams struct {
    29  	// The X-Twilio-Webhook-Enabled HTTP request header
    30  	XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"`
    31  	// A unique string identifier for the conversation participant as [Conversation User](https://www.twilio.com/docs/conversations/api/user-resource). This parameter is non-null if (and only if) the participant is using the Conversations SDK to communicate. Limited to 256 characters.
    32  	Identity *string `json:"Identity,omitempty"`
    33  	// The address of the participant's device, e.g. a phone or WhatsApp number. Together with the Proxy address, this determines a participant uniquely. This field (with proxy_address) is only null when the participant is interacting from an SDK endpoint (see the 'identity' field).
    34  	MessagingBindingAddress *string `json:"MessagingBinding.Address,omitempty"`
    35  	// The address of the Twilio phone number (or WhatsApp number) that the participant is in contact with. This field, together with participant address, is only null when the participant is interacting from an SDK endpoint (see the 'identity' field).
    36  	MessagingBindingProxyAddress *string `json:"MessagingBinding.ProxyAddress,omitempty"`
    37  	// The date that this resource was created.
    38  	DateCreated *time.Time `json:"DateCreated,omitempty"`
    39  	// The date that this resource was last updated.
    40  	DateUpdated *time.Time `json:"DateUpdated,omitempty"`
    41  	// An optional 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.
    42  	Attributes *string `json:"Attributes,omitempty"`
    43  	// The address of the Twilio phone number that is used in Group MMS. Communication mask for the Conversation participant with Identity.
    44  	MessagingBindingProjectedAddress *string `json:"MessagingBinding.ProjectedAddress,omitempty"`
    45  	// The SID of a conversation-level [Role](https://www.twilio.com/docs/conversations/api/role-resource) to assign to the participant.
    46  	RoleSid *string `json:"RoleSid,omitempty"`
    47  }
    48  
    49  func (params *CreateConversationParticipantParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *CreateConversationParticipantParams {
    50  	params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled
    51  	return params
    52  }
    53  func (params *CreateConversationParticipantParams) SetIdentity(Identity string) *CreateConversationParticipantParams {
    54  	params.Identity = &Identity
    55  	return params
    56  }
    57  func (params *CreateConversationParticipantParams) SetMessagingBindingAddress(MessagingBindingAddress string) *CreateConversationParticipantParams {
    58  	params.MessagingBindingAddress = &MessagingBindingAddress
    59  	return params
    60  }
    61  func (params *CreateConversationParticipantParams) SetMessagingBindingProxyAddress(MessagingBindingProxyAddress string) *CreateConversationParticipantParams {
    62  	params.MessagingBindingProxyAddress = &MessagingBindingProxyAddress
    63  	return params
    64  }
    65  func (params *CreateConversationParticipantParams) SetDateCreated(DateCreated time.Time) *CreateConversationParticipantParams {
    66  	params.DateCreated = &DateCreated
    67  	return params
    68  }
    69  func (params *CreateConversationParticipantParams) SetDateUpdated(DateUpdated time.Time) *CreateConversationParticipantParams {
    70  	params.DateUpdated = &DateUpdated
    71  	return params
    72  }
    73  func (params *CreateConversationParticipantParams) SetAttributes(Attributes string) *CreateConversationParticipantParams {
    74  	params.Attributes = &Attributes
    75  	return params
    76  }
    77  func (params *CreateConversationParticipantParams) SetMessagingBindingProjectedAddress(MessagingBindingProjectedAddress string) *CreateConversationParticipantParams {
    78  	params.MessagingBindingProjectedAddress = &MessagingBindingProjectedAddress
    79  	return params
    80  }
    81  func (params *CreateConversationParticipantParams) SetRoleSid(RoleSid string) *CreateConversationParticipantParams {
    82  	params.RoleSid = &RoleSid
    83  	return params
    84  }
    85  
    86  // Add a new participant to the conversation
    87  func (c *ApiService) CreateConversationParticipant(ConversationSid string, params *CreateConversationParticipantParams) (*ConversationsV1ConversationParticipant, error) {
    88  	path := "/v1/Conversations/{ConversationSid}/Participants"
    89  	path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1)
    90  
    91  	data := url.Values{}
    92  	headers := make(map[string]interface{})
    93  
    94  	if params != nil && params.Identity != nil {
    95  		data.Set("Identity", *params.Identity)
    96  	}
    97  	if params != nil && params.MessagingBindingAddress != nil {
    98  		data.Set("MessagingBinding.Address", *params.MessagingBindingAddress)
    99  	}
   100  	if params != nil && params.MessagingBindingProxyAddress != nil {
   101  		data.Set("MessagingBinding.ProxyAddress", *params.MessagingBindingProxyAddress)
   102  	}
   103  	if params != nil && params.DateCreated != nil {
   104  		data.Set("DateCreated", fmt.Sprint((*params.DateCreated).Format(time.RFC3339)))
   105  	}
   106  	if params != nil && params.DateUpdated != nil {
   107  		data.Set("DateUpdated", fmt.Sprint((*params.DateUpdated).Format(time.RFC3339)))
   108  	}
   109  	if params != nil && params.Attributes != nil {
   110  		data.Set("Attributes", *params.Attributes)
   111  	}
   112  	if params != nil && params.MessagingBindingProjectedAddress != nil {
   113  		data.Set("MessagingBinding.ProjectedAddress", *params.MessagingBindingProjectedAddress)
   114  	}
   115  	if params != nil && params.RoleSid != nil {
   116  		data.Set("RoleSid", *params.RoleSid)
   117  	}
   118  
   119  	if params != nil && params.XTwilioWebhookEnabled != nil {
   120  		headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled
   121  	}
   122  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  
   127  	defer resp.Body.Close()
   128  
   129  	ps := &ConversationsV1ConversationParticipant{}
   130  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   131  		return nil, err
   132  	}
   133  
   134  	return ps, err
   135  }
   136  
   137  // Optional parameters for the method 'DeleteConversationParticipant'
   138  type DeleteConversationParticipantParams struct {
   139  	// The X-Twilio-Webhook-Enabled HTTP request header
   140  	XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"`
   141  }
   142  
   143  func (params *DeleteConversationParticipantParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *DeleteConversationParticipantParams {
   144  	params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled
   145  	return params
   146  }
   147  
   148  // Remove a participant from the conversation
   149  func (c *ApiService) DeleteConversationParticipant(ConversationSid string, Sid string, params *DeleteConversationParticipantParams) error {
   150  	path := "/v1/Conversations/{ConversationSid}/Participants/{Sid}"
   151  	path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1)
   152  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   153  
   154  	data := url.Values{}
   155  	headers := make(map[string]interface{})
   156  
   157  	if params != nil && params.XTwilioWebhookEnabled != nil {
   158  		headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled
   159  	}
   160  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   161  	if err != nil {
   162  		return err
   163  	}
   164  
   165  	defer resp.Body.Close()
   166  
   167  	return nil
   168  }
   169  
   170  // Fetch a participant of the conversation
   171  func (c *ApiService) FetchConversationParticipant(ConversationSid string, Sid string) (*ConversationsV1ConversationParticipant, error) {
   172  	path := "/v1/Conversations/{ConversationSid}/Participants/{Sid}"
   173  	path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1)
   174  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   175  
   176  	data := url.Values{}
   177  	headers := make(map[string]interface{})
   178  
   179  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   180  	if err != nil {
   181  		return nil, err
   182  	}
   183  
   184  	defer resp.Body.Close()
   185  
   186  	ps := &ConversationsV1ConversationParticipant{}
   187  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   188  		return nil, err
   189  	}
   190  
   191  	return ps, err
   192  }
   193  
   194  // Optional parameters for the method 'ListConversationParticipant'
   195  type ListConversationParticipantParams struct {
   196  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   197  	PageSize *int `json:"PageSize,omitempty"`
   198  	// Max number of records to return.
   199  	Limit *int `json:"limit,omitempty"`
   200  }
   201  
   202  func (params *ListConversationParticipantParams) SetPageSize(PageSize int) *ListConversationParticipantParams {
   203  	params.PageSize = &PageSize
   204  	return params
   205  }
   206  func (params *ListConversationParticipantParams) SetLimit(Limit int) *ListConversationParticipantParams {
   207  	params.Limit = &Limit
   208  	return params
   209  }
   210  
   211  // Retrieve a single page of ConversationParticipant records from the API. Request is executed immediately.
   212  func (c *ApiService) PageConversationParticipant(ConversationSid string, params *ListConversationParticipantParams, pageToken, pageNumber string) (*ListConversationParticipantResponse, error) {
   213  	path := "/v1/Conversations/{ConversationSid}/Participants"
   214  
   215  	path = strings.Replace(path, "{"+"ConversationSid"+"}", ConversationSid, -1)
   216  
   217  	data := url.Values{}
   218  	headers := make(map[string]interface{})
   219  
   220  	if params != nil && params.PageSize != nil {
   221  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   222  	}
   223  
   224  	if pageToken != "" {
   225  		data.Set("PageToken", pageToken)
   226  	}
   227  	if pageNumber != "" {
   228  		data.Set("Page", pageNumber)
   229  	}
   230  
   231  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   232  	if err != nil {
   233  		return nil, err
   234  	}
   235  
   236  	defer resp.Body.Close()
   237  
   238  	ps := &ListConversationParticipantResponse{}
   239  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   240  		return nil, err
   241  	}
   242  
   243  	return ps, err
   244  }
   245  
   246  // Lists ConversationParticipant records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   247  func (c *ApiService) ListConversationParticipant(ConversationSid string, params *ListConversationParticipantParams) ([]ConversationsV1ConversationParticipant, error) {
   248  	response, errors := c.StreamConversationParticipant(ConversationSid, params)
   249  
   250  	records := make([]ConversationsV1ConversationParticipant, 0)
   251  	for record := range response {
   252  		records = append(records, record)
   253  	}
   254  
   255  	if err := <-errors; err != nil {
   256  		return nil, err
   257  	}
   258  
   259  	return records, nil
   260  }
   261  
   262  // Streams ConversationParticipant records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   263  func (c *ApiService) StreamConversationParticipant(ConversationSid string, params *ListConversationParticipantParams) (chan ConversationsV1ConversationParticipant, chan error) {
   264  	if params == nil {
   265  		params = &ListConversationParticipantParams{}
   266  	}
   267  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   268  
   269  	recordChannel := make(chan ConversationsV1ConversationParticipant, 1)
   270  	errorChannel := make(chan error, 1)
   271  
   272  	response, err := c.PageConversationParticipant(ConversationSid, params, "", "")
   273  	if err != nil {
   274  		errorChannel <- err
   275  		close(recordChannel)
   276  		close(errorChannel)
   277  	} else {
   278  		go c.streamConversationParticipant(response, params, recordChannel, errorChannel)
   279  	}
   280  
   281  	return recordChannel, errorChannel
   282  }
   283  
   284  func (c *ApiService) streamConversationParticipant(response *ListConversationParticipantResponse, params *ListConversationParticipantParams, recordChannel chan ConversationsV1ConversationParticipant, errorChannel chan error) {
   285  	curRecord := 1
   286  
   287  	for response != nil {
   288  		responseRecords := response.Participants
   289  		for item := range responseRecords {
   290  			recordChannel <- responseRecords[item]
   291  			curRecord += 1
   292  			if params.Limit != nil && *params.Limit < curRecord {
   293  				close(recordChannel)
   294  				close(errorChannel)
   295  				return
   296  			}
   297  		}
   298  
   299  		record, err := client.GetNext(c.baseURL, response, c.getNextListConversationParticipantResponse)
   300  		if err != nil {
   301  			errorChannel <- err
   302  			break
   303  		} else if record == nil {
   304  			break
   305  		}
   306  
   307  		response = record.(*ListConversationParticipantResponse)
   308  	}
   309  
   310  	close(recordChannel)
   311  	close(errorChannel)
   312  }
   313  
   314  func (c *ApiService) getNextListConversationParticipantResponse(nextPageUrl string) (interface{}, error) {
   315  	if nextPageUrl == "" {
   316  		return nil, nil
   317  	}
   318  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   319  	if err != nil {
   320  		return nil, err
   321  	}
   322  
   323  	defer resp.Body.Close()
   324  
   325  	ps := &ListConversationParticipantResponse{}
   326  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   327  		return nil, err
   328  	}
   329  	return ps, nil
   330  }
   331  
   332  // Optional parameters for the method 'UpdateConversationParticipant'
   333  type UpdateConversationParticipantParams struct {
   334  	// The X-Twilio-Webhook-Enabled HTTP request header
   335  	XTwilioWebhookEnabled *string `json:"X-Twilio-Webhook-Enabled,omitempty"`
   336  	// The date that this resource was created.
   337  	DateCreated *time.Time `json:"DateCreated,omitempty"`
   338  	// The date that this resource was last updated.
   339  	DateUpdated *time.Time `json:"DateUpdated,omitempty"`
   340  	// An optional 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.
   341  	Attributes *string `json:"Attributes,omitempty"`
   342  	// The SID of a conversation-level [Role](https://www.twilio.com/docs/conversations/api/role-resource) to assign to the participant.
   343  	RoleSid *string `json:"RoleSid,omitempty"`
   344  	// The address of the Twilio phone number that the participant is in contact with. 'null' value will remove it.
   345  	MessagingBindingProxyAddress *string `json:"MessagingBinding.ProxyAddress,omitempty"`
   346  	// The address of the Twilio phone number that is used in Group MMS. 'null' value will remove it.
   347  	MessagingBindingProjectedAddress *string `json:"MessagingBinding.ProjectedAddress,omitempty"`
   348  	// A unique string identifier for the conversation participant as [Conversation User](https://www.twilio.com/docs/conversations/api/user-resource). This parameter is non-null if (and only if) the participant is using the Conversations SDK to communicate. Limited to 256 characters.
   349  	Identity *string `json:"Identity,omitempty"`
   350  	// Index of last “read” message in the [Conversation](https://www.twilio.com/docs/conversations/api/conversation-resource) for the Participant.
   351  	LastReadMessageIndex *int `json:"LastReadMessageIndex,omitempty"`
   352  	// Timestamp of last “read” message in the [Conversation](https://www.twilio.com/docs/conversations/api/conversation-resource) for the Participant.
   353  	LastReadTimestamp *string `json:"LastReadTimestamp,omitempty"`
   354  }
   355  
   356  func (params *UpdateConversationParticipantParams) SetXTwilioWebhookEnabled(XTwilioWebhookEnabled string) *UpdateConversationParticipantParams {
   357  	params.XTwilioWebhookEnabled = &XTwilioWebhookEnabled
   358  	return params
   359  }
   360  func (params *UpdateConversationParticipantParams) SetDateCreated(DateCreated time.Time) *UpdateConversationParticipantParams {
   361  	params.DateCreated = &DateCreated
   362  	return params
   363  }
   364  func (params *UpdateConversationParticipantParams) SetDateUpdated(DateUpdated time.Time) *UpdateConversationParticipantParams {
   365  	params.DateUpdated = &DateUpdated
   366  	return params
   367  }
   368  func (params *UpdateConversationParticipantParams) SetAttributes(Attributes string) *UpdateConversationParticipantParams {
   369  	params.Attributes = &Attributes
   370  	return params
   371  }
   372  func (params *UpdateConversationParticipantParams) SetRoleSid(RoleSid string) *UpdateConversationParticipantParams {
   373  	params.RoleSid = &RoleSid
   374  	return params
   375  }
   376  func (params *UpdateConversationParticipantParams) SetMessagingBindingProxyAddress(MessagingBindingProxyAddress string) *UpdateConversationParticipantParams {
   377  	params.MessagingBindingProxyAddress = &MessagingBindingProxyAddress
   378  	return params
   379  }
   380  func (params *UpdateConversationParticipantParams) SetMessagingBindingProjectedAddress(MessagingBindingProjectedAddress string) *UpdateConversationParticipantParams {
   381  	params.MessagingBindingProjectedAddress = &MessagingBindingProjectedAddress
   382  	return params
   383  }
   384  func (params *UpdateConversationParticipantParams) SetIdentity(Identity string) *UpdateConversationParticipantParams {
   385  	params.Identity = &Identity
   386  	return params
   387  }
   388  func (params *UpdateConversationParticipantParams) SetLastReadMessageIndex(LastReadMessageIndex int) *UpdateConversationParticipantParams {
   389  	params.LastReadMessageIndex = &LastReadMessageIndex
   390  	return params
   391  }
   392  func (params *UpdateConversationParticipantParams) SetLastReadTimestamp(LastReadTimestamp string) *UpdateConversationParticipantParams {
   393  	params.LastReadTimestamp = &LastReadTimestamp
   394  	return params
   395  }
   396  
   397  // Update an existing participant in the conversation
   398  func (c *ApiService) UpdateConversationParticipant(ConversationSid string, Sid string, params *UpdateConversationParticipantParams) (*ConversationsV1ConversationParticipant, error) {
   399  	path := "/v1/Conversations/{ConversationSid}/Participants/{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.DateCreated != nil {
   407  		data.Set("DateCreated", fmt.Sprint((*params.DateCreated).Format(time.RFC3339)))
   408  	}
   409  	if params != nil && params.DateUpdated != nil {
   410  		data.Set("DateUpdated", fmt.Sprint((*params.DateUpdated).Format(time.RFC3339)))
   411  	}
   412  	if params != nil && params.Attributes != nil {
   413  		data.Set("Attributes", *params.Attributes)
   414  	}
   415  	if params != nil && params.RoleSid != nil {
   416  		data.Set("RoleSid", *params.RoleSid)
   417  	}
   418  	if params != nil && params.MessagingBindingProxyAddress != nil {
   419  		data.Set("MessagingBinding.ProxyAddress", *params.MessagingBindingProxyAddress)
   420  	}
   421  	if params != nil && params.MessagingBindingProjectedAddress != nil {
   422  		data.Set("MessagingBinding.ProjectedAddress", *params.MessagingBindingProjectedAddress)
   423  	}
   424  	if params != nil && params.Identity != nil {
   425  		data.Set("Identity", *params.Identity)
   426  	}
   427  	if params != nil && params.LastReadMessageIndex != nil {
   428  		data.Set("LastReadMessageIndex", fmt.Sprint(*params.LastReadMessageIndex))
   429  	}
   430  	if params != nil && params.LastReadTimestamp != nil {
   431  		data.Set("LastReadTimestamp", *params.LastReadTimestamp)
   432  	}
   433  
   434  	if params != nil && params.XTwilioWebhookEnabled != nil {
   435  		headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled
   436  	}
   437  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   438  	if err != nil {
   439  		return nil, err
   440  	}
   441  
   442  	defer resp.Body.Close()
   443  
   444  	ps := &ConversationsV1ConversationParticipant{}
   445  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   446  		return nil, err
   447  	}
   448  
   449  	return ps, err
   450  }