Skip to content

Commit

Permalink
Merge pull request #733 from nyaruka/twilio-whatsapp-retry
Browse files Browse the repository at this point in the history
Support retrying failed messages for rate limit on Twilio WhatsApp
  • Loading branch information
rowanseymour authored Apr 22, 2024
2 parents e066c10 + 5c4b60c commit 179f89d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 4 additions & 0 deletions handlers/twiml/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ var mediaSupport = map[handlers.MediaType]handlers.MediaTypeSupport{

// error code twilio returns when a contact has sent "stop"
const errorStopped = 21610
const errorThrottled = 63018

type handler struct {
handlers.BaseHandler
Expand Down Expand Up @@ -207,6 +208,9 @@ func (h *handler) receiveStatus(ctx context.Context, channel courier.Channel, w
}
}
clog.Error(twilioError(errorCode))
if errorCode == errorThrottled {
status = h.Backend().NewStatusUpdateByExternalID(channel, form.MessageSID, courier.MsgStatusErrored, clog)
}
}

return handlers.WriteMsgStatusAndResponse(ctx, h, channel, status, w, r)
Expand Down
19 changes: 16 additions & 3 deletions handlers/twiml/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ var (

statusStop = "ErrorCode=21610&MessageSid=SMe287d7109a5a925f182f0e07fe5b223b&MessageStatus=failed&To=%2B12028831111"

statusInvalid = "MessageSid=SMe287d7109a5a925f182f0e07fe5b223b&MessageStatus=huh"
statusValid = "MessageSid=SMe287d7109a5a925f182f0e07fe5b223b&MessageStatus=delivered"
statusRead = "MessageSid=SMe287d7109a5a925f182f0e07fe5b223b&MessageStatus=read"
statusInvalid = "MessageSid=SMe287d7109a5a925f182f0e07fe5b223b&MessageStatus=huh"
statusValid = "MessageSid=SMe287d7109a5a925f182f0e07fe5b223b&MessageStatus=delivered"
statusRead = "MessageSid=SMe287d7109a5a925f182f0e07fe5b223b&MessageStatus=read"
statusRateLimit = "MessageSid=SMe287d7109a5a925f182f0e07fe5b223b&MessageStatus=failed&ErrorCode=63018"

tmsStatusExtra = "SmsStatus=sent&MessageStatus=sent&To=2021&MessagingServiceSid=MGdb23ec0f89ee2632e46e91d8128f5e2b&MessageSid=SM0b6e2697aae04182a9f5b5c7a8994c7f&AccountSid=acctid&From=%2B14133881111&ApiVersion=2010-04-01"
tmsReceiveExtra = "ToCountry=US&ToState=&SmsMessageSid=SMbbf29aeb9d380ce2a1c0ae4635ff9dab&NumMedia=0&ToCity=&FromZip=27609&SmsSid=SMbbf29aeb9d380ce2a1c0ae4635ff9dab&FromState=NC&SmsStatus=received&FromCity=RALEIGH&Body=John+Cruz&FromCountry=US&To=384387&ToZip=&NumSegments=1&MessageSid=SMbbf29aeb9d380ce2a1c0ae4635ff9dab&AccountSid=acctid&From=%2B14133881111&ApiVersion=2010-04-01"
Expand Down Expand Up @@ -481,6 +482,18 @@ var twaTestCases = []IncomingTestCase{
},
PrepRequest: addValidSignature,
},
{
Label: "Status ID Rate limit",
URL: twaStatusIDURL,
Data: statusRateLimit,
ExpectedRespStatus: 200,
ExpectedBodyContains: `"status":"E"`,
ExpectedStatuses: []ExpectedStatus{
{ExternalID: "SMe287d7109a5a925f182f0e07fe5b223b", Status: courier.MsgStatusErrored},
},
PrepRequest: addValidSignature,
ExpectedErrors: []*courier.ChannelError{courier.ErrorExternal("63018", "Rate limit exceeded for Channel")},
},
{
Label: "Status ID Invalid",
URL: twaStatusInvalidIDURL,
Expand Down

0 comments on commit 179f89d

Please sign in to comment.