github.com/twilio/twilio-go@v1.20.1/rest/notify/v1/services_notifications.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Notify
     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  
    24  // Optional parameters for the method 'CreateNotification'
    25  type CreateNotificationParams struct {
    26  	// The notification text. For FCM and GCM, translates to `data.twi_body`. For APNS, translates to `aps.alert.body`. For SMS, translates to `body`. SMS requires either this `body` value, or `media_urls` attribute defined in the `sms` parameter of the notification.
    27  	Body *string `json:"Body,omitempty"`
    28  	//
    29  	Priority *string `json:"Priority,omitempty"`
    30  	// How long, in seconds, the notification is valid. Can be an integer between 0 and 2,419,200, which is 4 weeks, the default and the maximum supported time to live (TTL). Delivery should be attempted if the device is offline until the TTL elapses. Zero means that the notification delivery is attempted immediately, only once, and is not stored for future delivery. SMS does not support this property.
    31  	Ttl *int `json:"Ttl,omitempty"`
    32  	// The notification title. For FCM and GCM, this translates to the `data.twi_title` value. For APNS, this translates to the `aps.alert.title` value. SMS does not support this property. This field is not visible on iOS phones and tablets but appears on Apple Watch and Android devices.
    33  	Title *string `json:"Title,omitempty"`
    34  	// The name of the sound to be played for the notification. For FCM and GCM, this Translates to `data.twi_sound`.  For APNS, this translates to `aps.sound`.  SMS does not support this property.
    35  	Sound *string `json:"Sound,omitempty"`
    36  	// The actions to display for the notification. For APNS, translates to the `aps.category` value. For GCM, translates to the `data.twi_action` value. For SMS, this parameter is not supported and is omitted from deliveries to those channels.
    37  	Action *string `json:"Action,omitempty"`
    38  	// The custom key-value pairs of the notification's payload. For FCM and GCM, this value translates to `data` in the FCM and GCM payloads. FCM and GCM [reserve certain keys](https://firebase.google.com/docs/cloud-messaging/http-server-ref) that cannot be used in those channels. For APNS, attributes of `data` are inserted into the APNS payload as custom properties outside of the `aps` dictionary. In all channels, we reserve keys that start with `twi_` for future use. Custom keys that start with `twi_` are not allowed and are rejected as 400 Bad request with no delivery attempted. For SMS, this parameter is not supported and is omitted from deliveries to those channels.
    39  	Data *interface{} `json:"Data,omitempty"`
    40  	// The APNS-specific payload that overrides corresponding attributes in the generic payload for APNS Bindings. This property maps to the APNS `Payload` item, therefore the `aps` key must be used to change standard attributes. Adds custom key-value pairs to the root of the dictionary. See the [APNS documentation](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html) for more details. We reserve keys that start with `twi_` for future use. Custom keys that start with `twi_` are not allowed.
    41  	Apn *interface{} `json:"Apn,omitempty"`
    42  	// The GCM-specific payload that overrides corresponding attributes in the generic payload for GCM Bindings.  This property maps to the root JSON dictionary. See the [GCM documentation](https://firebase.google.com/docs/cloud-messaging/http-server-ref) for more details. Target parameters `to`, `registration_ids`, and `notification_key` are not allowed. We reserve keys that start with `twi_` for future use. Custom keys that start with `twi_` are not allowed. GCM also [reserves certain keys](https://firebase.google.com/docs/cloud-messaging/http-server-ref).
    43  	Gcm *interface{} `json:"Gcm,omitempty"`
    44  	// The SMS-specific payload that overrides corresponding attributes in the generic payload for SMS Bindings.  Each attribute in this value maps to the corresponding `form` parameter of the Twilio [Message](https://www.twilio.com/docs/sms/quickstart) resource.  These parameters of the Message resource are supported in snake case format: `body`, `media_urls`, `status_callback`, and `max_price`.  The `status_callback` parameter overrides the corresponding parameter in the messaging service, if configured. The `media_urls` property expects a JSON array.
    45  	Sms *interface{} `json:"Sms,omitempty"`
    46  	// Deprecated.
    47  	FacebookMessenger *interface{} `json:"FacebookMessenger,omitempty"`
    48  	// The FCM-specific payload that overrides corresponding attributes in the generic payload for FCM Bindings. This property maps to the root JSON dictionary. See the [FCM documentation](https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream) for more details. Target parameters `to`, `registration_ids`, `condition`, and `notification_key` are not allowed in this parameter. We reserve keys that start with `twi_` for future use. Custom keys that start with `twi_` are not allowed. FCM also [reserves certain keys](https://firebase.google.com/docs/cloud-messaging/http-server-ref), which cannot be used in that channel.
    49  	Fcm *interface{} `json:"Fcm,omitempty"`
    50  	// The Segment resource is deprecated. Use the `tag` parameter, instead.
    51  	Segment *[]string `json:"Segment,omitempty"`
    52  	// Deprecated.
    53  	Alexa *interface{} `json:"Alexa,omitempty"`
    54  	// The destination address specified as a JSON string.  Multiple `to_binding` parameters can be included but the total size of the request entity should not exceed 1MB. This is typically sufficient for 10,000 phone numbers.
    55  	ToBinding *[]string `json:"ToBinding,omitempty"`
    56  	// URL to send webhooks.
    57  	DeliveryCallbackUrl *string `json:"DeliveryCallbackUrl,omitempty"`
    58  	// The `identity` value that uniquely identifies the new resource's [User](https://www.twilio.com/docs/chat/rest/user-resource) within the [Service](https://www.twilio.com/docs/notify/api/service-resource). Delivery will be attempted only to Bindings with an Identity in this list. No more than 20 items are allowed in this list.
    59  	Identity *[]string `json:"Identity,omitempty"`
    60  	// A tag that selects the Bindings to notify. Repeat this parameter to specify more than one tag, up to a total of 5 tags. The implicit tag `all` is available to notify all Bindings in a Service instance. Similarly, the implicit tags `apn`, `fcm`, `gcm`, `sms` and `facebook-messenger` are available to notify all Bindings in a specific channel.
    61  	Tag *[]string `json:"Tag,omitempty"`
    62  }
    63  
    64  func (params *CreateNotificationParams) SetBody(Body string) *CreateNotificationParams {
    65  	params.Body = &Body
    66  	return params
    67  }
    68  func (params *CreateNotificationParams) SetPriority(Priority string) *CreateNotificationParams {
    69  	params.Priority = &Priority
    70  	return params
    71  }
    72  func (params *CreateNotificationParams) SetTtl(Ttl int) *CreateNotificationParams {
    73  	params.Ttl = &Ttl
    74  	return params
    75  }
    76  func (params *CreateNotificationParams) SetTitle(Title string) *CreateNotificationParams {
    77  	params.Title = &Title
    78  	return params
    79  }
    80  func (params *CreateNotificationParams) SetSound(Sound string) *CreateNotificationParams {
    81  	params.Sound = &Sound
    82  	return params
    83  }
    84  func (params *CreateNotificationParams) SetAction(Action string) *CreateNotificationParams {
    85  	params.Action = &Action
    86  	return params
    87  }
    88  func (params *CreateNotificationParams) SetData(Data interface{}) *CreateNotificationParams {
    89  	params.Data = &Data
    90  	return params
    91  }
    92  func (params *CreateNotificationParams) SetApn(Apn interface{}) *CreateNotificationParams {
    93  	params.Apn = &Apn
    94  	return params
    95  }
    96  func (params *CreateNotificationParams) SetGcm(Gcm interface{}) *CreateNotificationParams {
    97  	params.Gcm = &Gcm
    98  	return params
    99  }
   100  func (params *CreateNotificationParams) SetSms(Sms interface{}) *CreateNotificationParams {
   101  	params.Sms = &Sms
   102  	return params
   103  }
   104  func (params *CreateNotificationParams) SetFacebookMessenger(FacebookMessenger interface{}) *CreateNotificationParams {
   105  	params.FacebookMessenger = &FacebookMessenger
   106  	return params
   107  }
   108  func (params *CreateNotificationParams) SetFcm(Fcm interface{}) *CreateNotificationParams {
   109  	params.Fcm = &Fcm
   110  	return params
   111  }
   112  func (params *CreateNotificationParams) SetSegment(Segment []string) *CreateNotificationParams {
   113  	params.Segment = &Segment
   114  	return params
   115  }
   116  func (params *CreateNotificationParams) SetAlexa(Alexa interface{}) *CreateNotificationParams {
   117  	params.Alexa = &Alexa
   118  	return params
   119  }
   120  func (params *CreateNotificationParams) SetToBinding(ToBinding []string) *CreateNotificationParams {
   121  	params.ToBinding = &ToBinding
   122  	return params
   123  }
   124  func (params *CreateNotificationParams) SetDeliveryCallbackUrl(DeliveryCallbackUrl string) *CreateNotificationParams {
   125  	params.DeliveryCallbackUrl = &DeliveryCallbackUrl
   126  	return params
   127  }
   128  func (params *CreateNotificationParams) SetIdentity(Identity []string) *CreateNotificationParams {
   129  	params.Identity = &Identity
   130  	return params
   131  }
   132  func (params *CreateNotificationParams) SetTag(Tag []string) *CreateNotificationParams {
   133  	params.Tag = &Tag
   134  	return params
   135  }
   136  
   137  //
   138  func (c *ApiService) CreateNotification(ServiceSid string, params *CreateNotificationParams) (*NotifyV1Notification, error) {
   139  	path := "/v1/Services/{ServiceSid}/Notifications"
   140  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   141  
   142  	data := url.Values{}
   143  	headers := make(map[string]interface{})
   144  
   145  	if params != nil && params.Body != nil {
   146  		data.Set("Body", *params.Body)
   147  	}
   148  	if params != nil && params.Priority != nil {
   149  		data.Set("Priority", *params.Priority)
   150  	}
   151  	if params != nil && params.Ttl != nil {
   152  		data.Set("Ttl", fmt.Sprint(*params.Ttl))
   153  	}
   154  	if params != nil && params.Title != nil {
   155  		data.Set("Title", *params.Title)
   156  	}
   157  	if params != nil && params.Sound != nil {
   158  		data.Set("Sound", *params.Sound)
   159  	}
   160  	if params != nil && params.Action != nil {
   161  		data.Set("Action", *params.Action)
   162  	}
   163  	if params != nil && params.Data != nil {
   164  		v, err := json.Marshal(params.Data)
   165  
   166  		if err != nil {
   167  			return nil, err
   168  		}
   169  
   170  		data.Set("Data", string(v))
   171  	}
   172  	if params != nil && params.Apn != nil {
   173  		v, err := json.Marshal(params.Apn)
   174  
   175  		if err != nil {
   176  			return nil, err
   177  		}
   178  
   179  		data.Set("Apn", string(v))
   180  	}
   181  	if params != nil && params.Gcm != nil {
   182  		v, err := json.Marshal(params.Gcm)
   183  
   184  		if err != nil {
   185  			return nil, err
   186  		}
   187  
   188  		data.Set("Gcm", string(v))
   189  	}
   190  	if params != nil && params.Sms != nil {
   191  		v, err := json.Marshal(params.Sms)
   192  
   193  		if err != nil {
   194  			return nil, err
   195  		}
   196  
   197  		data.Set("Sms", string(v))
   198  	}
   199  	if params != nil && params.FacebookMessenger != nil {
   200  		v, err := json.Marshal(params.FacebookMessenger)
   201  
   202  		if err != nil {
   203  			return nil, err
   204  		}
   205  
   206  		data.Set("FacebookMessenger", string(v))
   207  	}
   208  	if params != nil && params.Fcm != nil {
   209  		v, err := json.Marshal(params.Fcm)
   210  
   211  		if err != nil {
   212  			return nil, err
   213  		}
   214  
   215  		data.Set("Fcm", string(v))
   216  	}
   217  	if params != nil && params.Segment != nil {
   218  		for _, item := range *params.Segment {
   219  			data.Add("Segment", item)
   220  		}
   221  	}
   222  	if params != nil && params.Alexa != nil {
   223  		v, err := json.Marshal(params.Alexa)
   224  
   225  		if err != nil {
   226  			return nil, err
   227  		}
   228  
   229  		data.Set("Alexa", string(v))
   230  	}
   231  	if params != nil && params.ToBinding != nil {
   232  		for _, item := range *params.ToBinding {
   233  			data.Add("ToBinding", item)
   234  		}
   235  	}
   236  	if params != nil && params.DeliveryCallbackUrl != nil {
   237  		data.Set("DeliveryCallbackUrl", *params.DeliveryCallbackUrl)
   238  	}
   239  	if params != nil && params.Identity != nil {
   240  		for _, item := range *params.Identity {
   241  			data.Add("Identity", item)
   242  		}
   243  	}
   244  	if params != nil && params.Tag != nil {
   245  		for _, item := range *params.Tag {
   246  			data.Add("Tag", item)
   247  		}
   248  	}
   249  
   250  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   251  	if err != nil {
   252  		return nil, err
   253  	}
   254  
   255  	defer resp.Body.Close()
   256  
   257  	ps := &NotifyV1Notification{}
   258  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   259  		return nil, err
   260  	}
   261  
   262  	return ps, err
   263  }