Skip to content

Commit

Permalink
Make IVR machine detection an option in channel config
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Aug 18, 2021
1 parent c302d68 commit 8d60f4b
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions core/ivr/ivr.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func GetClient(channel *models.Channel) (Client, error) {

// Client defines the interface IVR clients must satisfy
type Client interface {
RequestCall(number urns.URN, handleURL string, statusURL string) (CallID, *httpx.Trace, error)
RequestCall(number urns.URN, handleURL string, statusURL string, machineDetection bool) (CallID, *httpx.Trace, error)

HangupCall(externalID string) (*httpx.Trace, error)

Expand Down Expand Up @@ -250,7 +250,7 @@ func RequestCallStartForConnection(ctx context.Context, config *config.Config, d
}

// try to request our call start
callID, trace, err := c.RequestCall(telURN, resumeURL, statusURL)
callID, trace, err := c.RequestCall(telURN, resumeURL, statusURL, channel.MachineDetection())

/// insert an channel log if we have an HTTP trace
if trace != nil {
Expand Down
11 changes: 7 additions & 4 deletions core/ivr/twiml/twiml.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,18 @@ type CallResponse struct {
}

// RequestCall causes this client to request a new outgoing call for this provider
func (c *client) RequestCall(number urns.URN, callbackURL string, statusURL string) (ivr.CallID, *httpx.Trace, error) {
func (c *client) RequestCall(number urns.URN, callbackURL string, statusURL string, machineDetection bool) (ivr.CallID, *httpx.Trace, error) {
form := url.Values{}
form.Set("To", number.Path())
form.Set("From", c.channel.Address())
form.Set("Url", callbackURL)
form.Set("StatusCallback", statusURL)
form.Set("MachineDetection", "Enable")
form.Set("AsyncAmd", "true")
form.Set("AsyncAmdStatusCallback", statusURL)

if machineDetection {
form.Set("MachineDetection", "Enable")
form.Set("AsyncAmd", "true")
form.Set("AsyncAmdStatusCallback", statusURL)
}

sendURL := c.baseURL + strings.Replace(callPath, "{AccountSID}", c.accountSID, -1)

Expand Down
6 changes: 4 additions & 2 deletions core/ivr/vonage/vonage.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,17 @@ type CallResponse struct {
}

// RequestCall causes this client to request a new outgoing call for this provider
func (c *client) RequestCall(number urns.URN, resumeURL string, statusURL string) (ivr.CallID, *httpx.Trace, error) {
func (c *client) RequestCall(number urns.URN, resumeURL string, statusURL string, machineDetection bool) (ivr.CallID, *httpx.Trace, error) {
callR := &CallRequest{
AnswerURL: []string{resumeURL + "&sig=" + url.QueryEscape(c.calculateSignature(resumeURL))},
AnswerMethod: http.MethodPost,

EventURL: []string{statusURL + "?sig=" + url.QueryEscape(c.calculateSignature(statusURL))},
EventMethod: http.MethodPost,
}

MachineDetection: "hangup", // if an answering machine answers, just hangup
if machineDetection {
callR.MachineDetection = "hangup" // if an answering machine answers, just hangup
}

callR.To = append(callR.To, Phone{Type: "phone", Number: strings.TrimLeft(number.Path(), "+")})
Expand Down
7 changes: 6 additions & 1 deletion core/models/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type Channel struct {
Roles []assets.ChannelRole `json:"roles"`
MatchPrefixes []string `json:"match_prefixes"`
AllowInternational bool `json:"allow_international"`
MachineDetection bool `json:"machine_detection"`
Config map[string]interface{} `json:"config"`
}
}
Expand Down Expand Up @@ -91,6 +92,9 @@ func (c *Channel) MatchPrefixes() []string { return c.c.MatchPrefixes }
// AllowInternational returns whether this channel allows sending internationally (only applies to TEL schemes)
func (c *Channel) AllowInternational() bool { return c.c.AllowInternational }

// MachineDetection returns whether this channel should do answering machine detection (only applies to IVR)
func (c *Channel) MachineDetection() bool { return c.c.MachineDetection }

// Parent returns a reference to the parent channel of this channel (if any)
func (c *Channel) Parent() *assets.ChannelReference { return c.c.Parent }

Expand Down Expand Up @@ -169,7 +173,8 @@ SELECT ROW_TO_JSON(r) FROM (SELECT
FROM unnest(regexp_split_to_array(c.role,'')) as r)
) as roles,
JSON_EXTRACT_PATH(c.config::json, 'matching_prefixes') as match_prefixes,
JSON_EXTRACT_PATH(c.config::json, 'allow_international') as allow_international
JSON_EXTRACT_PATH(c.config::json, 'allow_international') as allow_international,
JSON_EXTRACT_PATH(c.config::json, 'machine_detection') as machine_detection
FROM
channels_channel c
WHERE
Expand Down
2 changes: 1 addition & 1 deletion core/tasks/ivr/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type MockClient struct {
callError error
}

func (c *MockClient) RequestCall(number urns.URN, handleURL string, statusURL string) (ivr.CallID, *httpx.Trace, error) {
func (c *MockClient) RequestCall(number urns.URN, handleURL string, statusURL string, machineDetection bool) (ivr.CallID, *httpx.Trace, error) {
return c.callID, nil, c.callError
}

Expand Down
2 changes: 1 addition & 1 deletion web/ivr/ivr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestTwilioIVR(t *testing.T) {
defer server.Stop()

// add auth tokens
db.MustExec(`UPDATE channels_channel SET config = '{"auth_token": "token", "account_sid": "sid", "callback_domain": "localhost:8090"}' WHERE id = $1`, testdata.TwilioChannel.ID)
db.MustExec(`UPDATE channels_channel SET config = '{"auth_token": "token", "account_sid": "sid", "callback_domain": "localhost:8090", "machine_detection": true}' WHERE id = $1`, testdata.TwilioChannel.ID)

// create a flow start for cathy bob, and george
parentSummary := json.RawMessage(`{
Expand Down

0 comments on commit 8d60f4b

Please sign in to comment.