Skip to content

Commit

Permalink
Merge pull request nyaruka#437 from Ilhasoft/fix/whatsapp-handler-upd…
Browse files Browse the repository at this point in the history
…ate-urn-on-send

tweak in whatsapp handler on sendWhatsAppMsg returned wppID
  • Loading branch information
rowanseymour authored Apr 27, 2022
2 parents 7ed4e67 + 373fd00 commit d49edf2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
6 changes: 6 additions & 0 deletions handlers/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ type ChannelSendTestCase struct {
ContactURNs map[string]bool

SendPrep SendPrepFunc
NewURN string
}

// Sp is a utility method to get the pointer to the passed in string
Expand Down Expand Up @@ -349,6 +350,11 @@ func RunChannelSendTestCases(t *testing.T, channel courier.Channel, handler cour
}
}

if testCase.NewURN != "" {
old, new := status.UpdatedURN()
require.Equal(urns.URN(testCase.URN), old)
require.Equal(urns.URN(testCase.NewURN), new)
}
})
}

Expand Down
26 changes: 17 additions & 9 deletions handlers/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,10 @@ var waIgnoreStatuses = map[string]bool{
// }

type mtTextPayload struct {
To string `json:"to" validate:"required"`
Type string `json:"type" validate:"required"`
PreviewURL bool `json:"preview_url,omitempty"`
Text struct {
To string `json:"to" validate:"required"`
Type string `json:"type" validate:"required"`
PreviewURL bool `json:"preview_url,omitempty"`
Text struct {
Body string `json:"body" validate:"required"`
} `json:"text"`
}
Expand Down Expand Up @@ -548,6 +548,7 @@ func (h *handler) SendMsg(ctx context.Context, msg courier.Msg) (courier.MsgStat

// we are wired it there were no errors
if err == nil {
// so update contact URN if wppID != ""
if wppID != "" {
newURN, _ := urns.NewWhatsAppURN(wppID)
err = status.SetUpdatedURN(msg.URN(), newURN)
Expand Down Expand Up @@ -805,14 +806,14 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
var payload mtTextPayload
if strings.Contains(part, "https://") || strings.Contains(part, "http://") {
payload = mtTextPayload{
To: msg.URN().Path(),
Type: "text",
To: msg.URN().Path(),
Type: "text",
PreviewURL: true,
}
} else {
payload = mtTextPayload{
To: msg.URN().Path(),
Type: "text",
To: msg.URN().Path(),
Type: "text",
}
}
payload.Text.Body = part
Expand Down Expand Up @@ -1025,7 +1026,14 @@ func sendWhatsAppMsg(rc redis.Conn, msg courier.Msg, sendPath *url.URL, payload
return wppID, externalID, []*courier.ChannelLog{log, checkLog, retryLog}, err
}
externalID, err := getSendWhatsAppMsgId(rr)
return "", externalID, []*courier.ChannelLog{log}, err
if err != nil {
return "", "", []*courier.ChannelLog{log}, err
}
wppID, err := jsonparser.GetString(rr.Body, "contacts", "[0]", "wa_id")
if wppID != "" && wppID != msg.URN().Path() {
return wppID, externalID, []*courier.ChannelLog{log}, err
}
return "", externalID, []*courier.ChannelLog{log}, nil
}

func setWhatsAppAuthHeader(header *http.Header, channel courier.Channel) {
Expand Down
7 changes: 7 additions & 0 deletions handlers/whatsapp/whatsapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,13 @@ var defaultSendTestCases = []ChannelSendTestCase{
},
},
SendPrep: setSendURL},
{Label: "Update URN with wa_id returned",
Text: "Simple Message", URN: "whatsapp:5511987654321", Path: "/v1/messages",
Status: "W", ExternalID: "157b5e14568e8",
ResponseBody: `{ "contacts":[{"input":"5511987654321","wa_id":"551187654321"}], "messages": [{"id": "157b5e14568e8"}] }`, ResponseStatus: 201,
RequestBody: `{"to":"5511987654321","type":"text","text":{"body":"Simple Message"}}`,
SendPrep: setSendURL,
NewURN: "whatsapp:551187654321"},
}

var mediaCacheSendTestCases = []ChannelSendTestCase{
Expand Down

0 comments on commit d49edf2

Please sign in to comment.