Skip to content

Commit

Permalink
Fix handling stops via status callbacks on Twilio
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Mar 21, 2022
1 parent 17f7da4 commit 6868a5c
Showing 1 changed file with 25 additions and 19 deletions.
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))
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

0 comments on commit 6868a5c

Please sign in to comment.