Skip to content

Commit

Permalink
Merge pull request #134 from nyaruka/more-twiml-ivr
Browse files Browse the repository at this point in the history
handle signalwire and twiml IVR calls
  • Loading branch information
nicpottier authored Jun 5, 2019
2 parents 486a0f9 + ce624d1 commit 8d33e68
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cmd/mailroom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
_ "github.com/nyaruka/mailroom/web/surveyor"

_ "github.com/nyaruka/mailroom/ivr/nexmo"
_ "github.com/nyaruka/mailroom/ivr/twilio"
_ "github.com/nyaruka/mailroom/ivr/twiml"
)

var version = "Dev"
Expand Down
37 changes: 24 additions & 13 deletions ivr/twilio/twilio.go → ivr/twiml/twiml.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package twilio
package twiml

import (
"bytes"
Expand Down Expand Up @@ -30,11 +30,16 @@ import (
"github.com/sirupsen/logrus"
)

// BaseURL is our default base URL for TWIML channels (public for testing overriding)
var BaseURL = `https://api.twilio.com`

// IgnoreSignatures controls whether we ignore signatures (public for testing overriding)
var IgnoreSignatures = false

const (
twilioChannelType = models.ChannelType("T")
twilioChannelType = models.ChannelType("T")
twimlChannelType = models.ChannelType("TW")
signalWireChannelType = models.ChannelType("SW")

callPath = `/2010-04-01/Accounts/{AccountSID}/Calls.json`
hangupPath = `/2010-04-01/Accounts/{AccountSID}/Calls/{SID}.json`
Expand All @@ -48,7 +53,9 @@ const (

accountSIDConfig = "account_sid"
authTokenConfig = "auth_token"
baseURLConfig = "send_url"

sendURLConfig = "send_url"
baseURLConfig = "base_url"

errorBody = `<?xml version="1.0" encoding="UTF-8"?>
<Response>
Expand All @@ -61,14 +68,17 @@ const (
var indentMarshal = true

type client struct {
channel *models.Channel
baseURL string
accountSID string
authToken string
channel *models.Channel
baseURL string
accountSID string
authToken string
validateSigs bool
}

func init() {
ivr.RegisterClientType(twimlChannelType, NewClientFromChannel)
ivr.RegisterClientType(twilioChannelType, NewClientFromChannel)
ivr.RegisterClientType(signalWireChannelType, NewClientFromChannel)
}

// NewClientFromChannel creates a new Twilio IVR client for the passed in account and and auth token
Expand All @@ -78,13 +88,14 @@ func NewClientFromChannel(channel *models.Channel) (ivr.Client, error) {
if accountSID == "" || authToken == "" {
return nil, errors.Errorf("missing auth_token or account_sid on channel config: %v for channel: %s", channel.Config(), channel.UUID())
}
baseURL := channel.ConfigValue(baseURLConfig, BaseURL)
baseURL := channel.ConfigValue(baseURLConfig, channel.ConfigValue(sendURLConfig, BaseURL))

return &client{
channel: channel,
baseURL: baseURL,
accountSID: accountSID,
authToken: authToken,
channel: channel,
baseURL: baseURL,
accountSID: accountSID,
authToken: authToken,
validateSigs: channel.Type() != signalWireChannelType,
}, nil
}

Expand Down Expand Up @@ -246,7 +257,7 @@ func (c *client) StatusForRequest(r *http.Request) (models.ConnectionStatus, int
// ValidateRequestSignature validates the signature on the passed in request, returning an error if it is invaled
func (c *client) ValidateRequestSignature(r *http.Request) error {
// shortcut for testing
if IgnoreSignatures {
if IgnoreSignatures || !c.validateSigs {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion ivr/twilio/twilio_test.go → ivr/twiml/twiml_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package twilio
package twiml

import (
"encoding/xml"
Expand Down
6 changes: 3 additions & 3 deletions web/ivr/ivr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
_ "github.com/nyaruka/mailroom/hooks"
"github.com/nyaruka/mailroom/ivr"
"github.com/nyaruka/mailroom/ivr/nexmo"
"github.com/nyaruka/mailroom/ivr/twilio"
"github.com/nyaruka/mailroom/ivr/twiml"
)

func TestTwilioIVR(t *testing.T) {
Expand Down Expand Up @@ -55,8 +55,8 @@ func TestTwilioIVR(t *testing.T) {
}))
defer ts.Close()

twilio.BaseURL = ts.URL
twilio.IgnoreSignatures = true
twiml.BaseURL = ts.URL
twiml.IgnoreSignatures = true

wg := &sync.WaitGroup{}
server := web.NewServer(ctx, config.Mailroom, db, rp, nil, wg)
Expand Down

0 comments on commit 8d33e68

Please sign in to comment.