Skip to content

Commit

Permalink
Merge pull request rapidpro#481 from nyaruka/fix_amd_retry
Browse files Browse the repository at this point in the history
Fix retries after answering machine detection
  • Loading branch information
rowanseymour authored Aug 16, 2021
2 parents 6d91f5e + 27fdbae commit 1e42f83
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
13 changes: 9 additions & 4 deletions core/ivr/ivr.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,18 @@ func ResumeIVRFlow(
if session.ConnectionID() == nil {
return WriteErrorResponse(ctx, rt.DB, client, conn, w, errors.Errorf("active session: %d has no connection", session.ID()))
}

if *session.ConnectionID() != conn.ID() {
return WriteErrorResponse(ctx, rt.DB, client, conn, w, errors.Errorf("active session: %d does not match connection: %d", session.ID(), *session.ConnectionID()))
}

// check connection is still marked as in progress
if conn.Status() != models.ConnectionStatusInProgress {
return WriteErrorResponse(ctx, rt.DB, client, conn, w, errors.Errorf("connection in invalid state: %s", conn.Status()))
// check if connection has been marked as errored - it maybe have been updated by status callback
if conn.Status() == models.ConnectionStatusErrored || conn.Status() == models.ConnectionStatusFailed {
err = models.ExitSessions(ctx, rt.DB, []models.SessionID{session.ID()}, models.ExitInterrupted, time.Now())
if err != nil {
logrus.WithError(err).Error("error interrupting session")
}

return client.WriteErrorResponse(w, fmt.Errorf("ending call due to previous status callback"))
}

// preprocess this request
Expand Down Expand Up @@ -616,6 +620,7 @@ func HandleIVRStatus(ctx context.Context, rt *runtime.Runtime, oa *models.OrgAss
if conn.Status() == models.ConnectionStatusErrored {
return client.WriteEmptyResponse(w, fmt.Sprintf("status updated: %s next_attempt: %s", conn.Status(), conn.NextAttempt()))
}

} else if status == models.ConnectionStatusFailed {
conn.MarkFailed(ctx, rt.DB, time.Now())
} else {
Expand Down
11 changes: 11 additions & 0 deletions web/ivr/ivr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,17 @@ func TestTwilioIVR(t *testing.T) {
expectedStatus: 200,
contains: []string{"<Response><!--status updated: E next_attempt:"},
},
{
label: "subsequent resume which should see we are now errored and hangup",
url: fmt.Sprintf("/ivr/c/%s/handle?action=resume&connection=3", testdata.TwilioChannel.UUID),
form: url.Values{
"CallStatus": []string{"completed"},
"wait_type": []string{"gather"},
"Digits": []string{"56"},
},
expectedStatus: 200,
expectedResponse: `<Response><!--ending call due to previous status callback--><Say>An error has occurred, please try again later.</Say><Hangup></Hangup></Response>`,
},
}

for i, tc := range tcs {
Expand Down

0 comments on commit 1e42f83

Please sign in to comment.