Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support retrying failed messages for rate limit on Twilio WhatsApp #733

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we got rid of handlers creating status updates? This won't be used no? The sender creates the status update.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case the status is reported to use asynchronously so the handler still need set the status to error so we can retry in this case

We changed for the Send method only

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok nevermind

}
}

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
Loading