diff --git a/cmd/mailroom/main.go b/cmd/mailroom/main.go index 6fa0ff4e6..1acc31d76 100644 --- a/cmd/mailroom/main.go +++ b/cmd/mailroom/main.go @@ -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" diff --git a/ivr/twilio/twilio.go b/ivr/twiml/twiml.go similarity index 93% rename from ivr/twilio/twilio.go rename to ivr/twiml/twiml.go index 70a386d23..f3c8dec2f 100644 --- a/ivr/twilio/twilio.go +++ b/ivr/twiml/twiml.go @@ -1,4 +1,4 @@ -package twilio +package twiml import ( "bytes" @@ -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` @@ -48,7 +53,9 @@ const ( accountSIDConfig = "account_sid" authTokenConfig = "auth_token" - baseURLConfig = "send_url" + + sendURLConfig = "send_url" + baseURLConfig = "base_url" errorBody = ` @@ -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 @@ -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 } @@ -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 } diff --git a/ivr/twilio/twilio_test.go b/ivr/twiml/twiml_test.go similarity index 99% rename from ivr/twilio/twilio_test.go rename to ivr/twiml/twiml_test.go index fb064d6bc..fc0f06e40 100644 --- a/ivr/twilio/twilio_test.go +++ b/ivr/twiml/twiml_test.go @@ -1,4 +1,4 @@ -package twilio +package twiml import ( "encoding/xml" diff --git a/web/ivr/ivr_test.go b/web/ivr/ivr_test.go index 69086783b..12d49a60f 100644 --- a/web/ivr/ivr_test.go +++ b/web/ivr/ivr_test.go @@ -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) { @@ -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)