Skip to content

Commit

Permalink
Merge pull request #49 from nyaruka/stop-twilio-contacts
Browse files Browse the repository at this point in the history
twilio returns 400s when sending to twilio, do the right thing in tha…
  • Loading branch information
nicpottier authored Oct 6, 2017
2 parents a7c4553 + 3b5fef8 commit de0945c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
24 changes: 13 additions & 11 deletions handlers/twilio/twilio.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,19 +232,21 @@ func (h *handler) SendMsg(msg courier.Msg) (courier.MsgStatus, error) {
log := courier.NewChannelLogFromRR("Message Sent", msg.Channel(), msg.ID(), rr).WithError("Message Send Error", err)
status.AddLog(log)

// fail if we received an error
if err != nil {
return status, nil
// parse the response if we had one
if rr.Body != nil {
errorCode, _ := jsonparser.GetInt([]byte(rr.Body), "error_code")
if errorCode != 0 {
if errorCode == errorStopped {
status.SetStatus(courier.MsgFailed)
h.Backend().StopMsgContact(msg)
}
log.WithError("Message Send Error", errors.Errorf("received error code from twilio '%d'", errorCode))
return status, nil
}
}

// was this request successful?
errorCode, _ := jsonparser.GetInt([]byte(rr.Body), "error_code")
if errorCode != 0 {
if errorCode == errorStopped {
status.SetStatus(courier.MsgFailed)
h.Backend().StopMsgContact(msg)
}
log.WithError("Message Send Error", errors.Errorf("received error code from twilio '%d'", errorCode))
// fail if we received an error
if err != nil {
return status, nil
}

Expand Down
2 changes: 1 addition & 1 deletion handlers/twilio/twilio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ var defaultSendTestCases = []ChannelSendTestCase{
{Label: "Stopped Contact Code",
Text: "Stopped Contact", URN: "tel:+250788383383",
Status: "F",
ResponseBody: `{ "error_code": 21610 }`, ResponseStatus: 200,
ResponseBody: `{ "error_code": 21610 }`, ResponseStatus: 400,
PostParams: map[string]string{"Body": "Stopped Contact", "To": "+250788383383"},
SendPrep: setSendURL,
Stopped: true},
Expand Down

0 comments on commit de0945c

Please sign in to comment.