Skip to content

Commit

Permalink
add send options
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorwiebe committed Feb 13, 2018
1 parent 51b4bf0 commit a077cdd
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# VTwilio - Go
##### Note: breaking changes may happen in releases before v1.0.0
### Version 0.0.3
### Version 0.1.0
Call the twilio rest api in go.

## Examples
Expand Down Expand Up @@ -132,6 +132,9 @@ func ReleaseNumber() error {
[TwiML Docs](./twiml/README.md)

## Change Log
### v0.1.0
- Add from number option for send method to override the client's default
- Add callback url option to send method
### v0.0.3
- TwiML support
- Incoming phone numbers
Expand Down
14 changes: 11 additions & 3 deletions send.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,26 @@ func (v *VTwilio) SendMessage(message string, to string, opts ...SendOption) (*M
}

func (v *VTwilio) sendMessage(message, to string, config *sendConfiguration) (*Message, error) {
from := v.twilioNumber
if config.From != "" {
from = config.From
}

values := url.Values{}
values.Set("To", to)
values.Set("From", v.twilioNumber)
values.Set("From", from)
values.Set("Body", message)
if config.MediaURL != "" {
values.Set("MediaUrl", config.MediaURL)
}

en := values.Encode()
if config.CallbackURL != "" && config.CallbackMethod != "" {
values.Set("statusCallback", config.CallbackURL)
values.Set("StatusCallbackMethod", config.CallbackMethod.String())
}

en := values.Encode()
urlStr := fmt.Sprintf("%s%s%s.json", v.baseAPI, v.accountSID, messageAPI)
fmt.Println(urlStr)
req, err := http.NewRequest("POST", urlStr, strings.NewReader(en))
if err != nil {
return nil, err
Expand Down
21 changes: 20 additions & 1 deletion send_options.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package vtwilio

type sendConfiguration struct {
MediaURL string
MediaURL string
From string
CallbackURL string
CallbackMethod Method
}

// SendOption is an option for messages being sent
Expand All @@ -13,3 +16,19 @@ func MediaURL(url string) SendOption {
c.MediaURL = url
}
}

// FromNumber is the phone number that the text message will be sent from
// This will override the phone number set on the client for the current text message
func FromNumber(number string) SendOption {
return func(c *sendConfiguration) {
c.From = number
}
}

// Callback adds a callback url with the method that Twilio should call this endpoint
func Callback(url string, method Method) SendOption {
return func(c *sendConfiguration) {
c.CallbackURL = url
c.CallbackMethod = method
}
}
24 changes: 24 additions & 0 deletions send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ func TestSendRequest(t *testing.T) {
expectedPath: "/sid/Messages.json",
expectedBody: "Body=Text+message&From=%2B12345678910&To=%2B09876543210",
},
{
name: "override from",
opts: []SendOption{FromNumber("+10987654321")},
message: "Text message",
to: "+09876543210",
expectedPath: "/sid/Messages.json",
expectedBody: "Body=Text+message&From=%2B10987654321&To=%2B09876543210",
},
{
name: "media url",
opts: []SendOption{MediaURL("http://url.com")},
Expand All @@ -36,6 +44,22 @@ func TestSendRequest(t *testing.T) {
expectedPath: "/sid/Messages.json",
expectedBody: "Body=Text+message&From=%2B12345678910&MediaUrl=http%3A%2F%2Furl.com&To=%2B09876543210",
},
{
name: "callback url",
opts: []SendOption{Callback("http://url.com/callback", POST)},
message: "Text message",
to: "+09876543210",
expectedPath: "/sid/Messages.json",
expectedBody: "Body=Text+message&From=%2B12345678910&StatusCallbackMethod=POST&To=%2B09876543210&statusCallback=http%3A%2F%2Furl.com%2Fcallback",
},
{
name: "meda url and callback url",
opts: []SendOption{MediaURL("http://url.com"), Callback("http://url.com/callback", POST)},
message: "Text message",
to: "+09876543210",
expectedPath: "/sid/Messages.json",
expectedBody: "Body=Text+message&From=%2B12345678910&MediaUrl=http%3A%2F%2Furl.com&StatusCallbackMethod=POST&To=%2B09876543210&statusCallback=http%3A%2F%2Furl.com%2Fcallback",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit a077cdd

Please sign in to comment.