sfreiberg / gotwilio

Twilio library for Go (golang).
BSD 2-Clause "Simplified" License
342 stars 136 forks source link

Feature request: address and content retention options for sms #84

Closed jnu closed 3 years ago

jnu commented 4 years ago

Hi, thanks for maintaining a go client for twilio, it's super helpful! Would it be possible to support the AddressRetention and ContentRetention parameters in SendSMS? Documentation here: https://www.twilio.com/docs/sms/api/message-resource#create-a-message-resource

The code should be easy to add to sendMessage, seems like it's mostly a question of how you want to allow optional parameters. One method is using an ...Option spread, for example: https://github.com/nats-io/go-nats/blob/master/nats.go#L515

gotwilio.SendSMS(from, to, content, "https://..., "...", gotwilio.ContentRetentionDiscard, gotwilio.AddressRetentionObfuscate)

then these get applied to form values in SendSMS:

formValues.Set("ContentRetention", "discard")
formValues.Set("AddressRetention", "obfuscate")

the implementation might look like:

type Option struct {
    Key   string
    Value string
} 

var ContentRetentionDiscard = &Option{"ContentRetention", "discard"}
var AddressRetentionObfuscate = &Option{"AddressRetention", "obfuscate"}

func (twilio *Twilio) SendSMS(from, to, body, statusCallback, applicationSid string, opts ...Option) (smsResponse *SmsResponse, exception *Exception, err error) {
    formValues := initFormValues(to, body, nil, statusCallback, applicationSid)
    formValues.Set("From", from)

    for _, opt := range opts {
        if opt != nil {
            formValues.Set(opt.Key, opt.Value)
        }
    }

    smsResponse, exception, err = twilio.sendMessage(formValues)
    return
}

happy to submit a PR for this, or discuss other approaches!