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

Fix handling stops via status callbacks on Twilio #427

Merged
merged 1 commit into from
Mar 21, 2022
Merged
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
44 changes: 25 additions & 19 deletions handlers/twiml/twiml.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,7 @@ func (h *handler) receiveMessage(ctx context.Context, channel courier.Channel, w
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}

// create our URN
var urn urns.URN
if channel.IsScheme(urns.WhatsAppScheme) {
// Twilio Whatsapp from is in the form: whatsapp:+12211414154 or +12211414154
var fromTel string
parts := strings.Split(form.From, ":")
if len(parts) > 1 {
fromTel = parts[1]
} else {
fromTel = parts[0]
}

// trim off left +, official whatsapp IDs dont have that
urn, err = urns.NewWhatsAppURN(strings.TrimLeft(fromTel, "+"))
} else {
urn, err = urns.NewTelURNForCountry(form.From, form.FromCountry)
}

urn, err := h.parseURN(channel, form.From, form.FromCountry)
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}
Expand Down Expand Up @@ -199,8 +182,13 @@ func (h *handler) receiveStatus(ctx context.Context, channel courier.Channel, w

errorCode, _ := strconv.ParseInt(form.ErrorCode, 10, 64)
if errorCode == errorStopped {
urn, err := h.parseURN(channel, form.To, "")
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}

// create a stop channel event
channelEvent := h.Backend().NewChannelEvent(channel, courier.StopContact, urns.URN(form.To))
Copy link
Member Author

Choose a reason for hiding this comment

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

this ends up being just a phone number

channelEvent := h.Backend().NewChannelEvent(channel, courier.StopContact, urn)
err = h.Backend().WriteChannelEvent(ctx, channelEvent)
if err != nil {
return nil, err
Expand Down Expand Up @@ -326,6 +314,24 @@ func (h *handler) SendMsg(ctx context.Context, msg courier.Msg) (courier.MsgStat
return status, nil
}

func (h *handler) parseURN(channel courier.Channel, text, country string) (urns.URN, error) {
if channel.IsScheme(urns.WhatsAppScheme) {
// Twilio Whatsapp from is in the form: whatsapp:+12211414154 or +12211414154
var fromTel string
parts := strings.Split(text, ":")
if len(parts) > 1 {
fromTel = parts[1]
} else {
fromTel = parts[0]
}

// trim off left +, official whatsapp IDs dont have that
return urns.NewWhatsAppURN(strings.TrimLeft(fromTel, "+"))
}

return urns.NewTelURNForCountry(text, country)
}

func (h *handler) baseURL(c courier.Channel) string {
// Twilio channels use the Twili base URL
if c.ChannelType() == "T" || c.ChannelType() == "TMS" || c.ChannelType() == "TWA" {
Expand Down