Skip to content
Open
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
23 changes: 23 additions & 0 deletions handlers/facebookapp/facebookapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -1041,12 +1041,19 @@ type wacMTResponse struct {
Messages []*struct {
ID string `json:"id"`
} `json:"messages"`
Contacts []*struct {
Input string `json:"input,omitempty"`
WaID string `json:"wa_id,omitempty"`
} `json:"contacts,omitempty"`
}

func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg) (courier.MsgStatus, error) {
// can't do anything without an access token
accessToken := h.Server().Config().WhatsappAdminSystemUserToken

start := time.Now()
hasNewURN := false

base, _ := url.Parse(graphURL)
path, _ := url.Parse(fmt.Sprintf("/%s/messages", msg.Channel().Address()))
wacPhoneURL := base.ResolveReference(path)
Expand Down Expand Up @@ -1264,6 +1271,22 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
// this was wired successfully
status.SetStatus(courier.MsgWired)

// if payload.contacts[0].wa_id != payload.contacts[0].input | to fix cases with 9 extra
if len(respPayload.Contacts) > 0 && respPayload.Contacts[0].WaID != msg.URN().Path() {
if !hasNewURN {
toUpdateURN, err := urns.NewWhatsAppURN(respPayload.Contacts[0].WaID)
if err != nil {
return status, nil
}
err = status.SetUpdatedURN(msg.URN(), toUpdateURN)
if err != nil {
log := courier.NewChannelLogFromError("unable to update contact URN for a new based on wa_id", msg.Channel(), msg.ID(), time.Since(start), err)
status.AddLog(log)
}
hasNewURN = true
}
}

}
return status, nil
}
Expand Down
7 changes: 7 additions & 0 deletions handlers/facebookapp/facebookapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,13 @@ var SendTestCasesWAC = []ChannelSendTestCase{
},
},
SendPrep: setSendURL},
{Label: "Update URN with wa_id returned",
Text: "Simple Message", URN: "whatsapp:5511987654321", Path: "/12345_ID/messages",
Status: "W", ExternalID: "157b5e14568e8",
ResponseBody: `{ "contacts":[{"input":"5511987654321", "wa_id":"551187654321"}], "messages": [{"id": "157b5e14568e8"}] }`, ResponseStatus: 201,
RequestBody: `{"messaging_product":"whatsapp","preview_url":false,"recipient_type":"individual","to":"5511987654321","type":"text","text":{"body":"Simple Message"}}`,
SendPrep: setSendURL,
NewURN: "whatsapp:551187654321"},
}

func TestSending(t *testing.T) {
Expand Down