Skip to content

Commit

Permalink
Merge branch 'main' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
rasoro committed Oct 23, 2024
2 parents 914f830 + 5724892 commit c44fcd2
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 35 deletions.
7 changes: 7 additions & 0 deletions WENI-CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
1.18.1
----------
* Fix: document name when message have quick replies on whatsapp handler
* Fix: document name when message have quick replies on facebookapp handler for whatsapp cloud api
* Fix: backslashes for headerText, footer and ctaMessage
* Fix: handle empty flow data to not be sent as empty object in request

1.18.0
----------
* Search for contact and update a Teams contact with the serviceURL
Expand Down
41 changes: 20 additions & 21 deletions handlers/facebookapp/facebookapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -1583,7 +1583,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
if msg.Footer() != "" {
interactive.Footer = &struct {
Text string "json:\"text,omitempty\""
}{Text: msg.Footer()}
}{Text: parseBacklashes(msg.Footer())}
}

if msg.HeaderText() != "" {
Expand All @@ -1593,7 +1593,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
Video *wacMTMedia "json:\"video,omitempty\""
Image *wacMTMedia "json:\"image,omitempty\""
Document *wacMTMedia "json:\"document,omitempty\""
}{Type: "text", Text: msg.HeaderText()}
}{Type: "text", Text: parseBacklashes(msg.HeaderText())}
}

btns := make([]wacMTButton, len(qrs))
Expand Down Expand Up @@ -1659,7 +1659,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
if msg.Footer() != "" {
interactive.Footer = &struct {
Text string "json:\"text,omitempty\""
}{Text: msg.Footer()}
}{Text: parseBacklashes(msg.Footer())}
}

if msg.HeaderText() != "" {
Expand All @@ -1669,7 +1669,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
Video *wacMTMedia "json:\"video,omitempty\""
Image *wacMTMedia "json:\"image,omitempty\""
Document *wacMTMedia "json:\"document,omitempty\""
}{Type: "text", Text: msg.HeaderText()}
}{Type: "text", Text: parseBacklashes(msg.HeaderText())}
}
}

Expand Down Expand Up @@ -1733,15 +1733,15 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
}{
Name: "cta_url",
Parameters: map[string]interface{}{
"display_text": ctaMessage.DisplayText,
"display_text": parseBacklashes(ctaMessage.DisplayText),
"url": ctaMessage.URL,
},
},
}
if msg.Footer() != "" {
interactive.Footer = &struct {
Text string "json:\"text,omitempty\""
}{Text: msg.Footer()}
}{Text: parseBacklashes(msg.Footer())}
}

if msg.HeaderText() != "" {
Expand All @@ -1751,7 +1751,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
Video *wacMTMedia "json:\"video,omitempty\""
Image *wacMTMedia "json:\"image,omitempty\""
Document *wacMTMedia "json:\"document,omitempty\""
}{Type: "text", Text: msg.HeaderText()}
}{Type: "text", Text: parseBacklashes(msg.HeaderText())}
}
payload.Interactive = &interactive
}
Expand Down Expand Up @@ -1790,7 +1790,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
if msg.Footer() != "" {
interactive.Footer = &struct {
Text string "json:\"text,omitempty\""
}{Text: msg.Footer()}
}{Text: parseBacklashes(msg.Footer())}
}

if msg.HeaderText() != "" {
Expand All @@ -1800,7 +1800,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
Video *wacMTMedia "json:\"video,omitempty\""
Image *wacMTMedia "json:\"image,omitempty\""
Document *wacMTMedia "json:\"document,omitempty\""
}{Type: "text", Text: msg.HeaderText()}
}{Type: "text", Text: parseBacklashes(msg.HeaderText())}
}
payload.Interactive = &interactive
}
Expand Down Expand Up @@ -1908,7 +1908,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
if msg.Footer() != "" {
interactive.Footer = &struct {
Text string "json:\"text,omitempty\""
}{Text: msg.Footer()}
}{Text: parseBacklashes(msg.Footer())}
}

payload.Interactive = &interactive
Expand Down Expand Up @@ -1998,6 +1998,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)

if len(msg.Attachments()) > 0 {
attType, attURL := handlers.SplitAttachment(msg.Attachments()[i])
fileURL := attURL
mediaID, mediaLogs, err := h.fetchWACMediaID(msg, attType, attURL, accessToken)
for _, log := range mediaLogs {
status.AddLog(log)
Expand All @@ -2011,9 +2012,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
if attType == "application" {
attType = "document"
}
fileURL := attURL
media := wacMTMedia{ID: mediaID, Link: attURL}

if attType == "image" {
interactive.Header = &struct {
Type string "json:\"type\""
Expand Down Expand Up @@ -2081,7 +2080,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
if msg.Footer() != "" {
payload.Interactive.Footer = &struct {
Text string "json:\"text,omitempty\""
}{Text: msg.Footer()}
}{Text: parseBacklashes(msg.Footer())}
}
} else if len(qrs) <= 10 || len(msg.ListMessage().ListItems) > 0 {
interactive := wacInteractive{
Expand Down Expand Up @@ -2120,7 +2119,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
if msg.Footer() != "" {
interactive.Footer = &struct {
Text string "json:\"text,omitempty\""
}{Text: msg.Footer()}
}{Text: parseBacklashes(msg.Footer())}
}
}

Expand Down Expand Up @@ -2178,7 +2177,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
}{
Name: "cta_url",
Parameters: map[string]interface{}{
"display_text": ctaMessage.DisplayText,
"display_text": parseBacklashes(ctaMessage.DisplayText),
"url": ctaMessage.URL,
},
},
Expand All @@ -2187,7 +2186,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
if msg.Footer() != "" {
interactive.Footer = &struct {
Text string "json:\"text,omitempty\""
}{Text: msg.Footer()}
}{Text: parseBacklashes(msg.Footer())}
}

if msg.HeaderText() != "" {
Expand All @@ -2197,7 +2196,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
Video *wacMTMedia "json:\"video,omitempty\""
Image *wacMTMedia "json:\"image,omitempty\""
Document *wacMTMedia "json:\"document,omitempty\""
}{Type: "text", Text: msg.HeaderText()}
}{Type: "text", Text: parseBacklashes(msg.HeaderText())}
}
payload.Interactive = &interactive
}
Expand Down Expand Up @@ -2236,7 +2235,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
if msg.Footer() != "" {
interactive.Footer = &struct {
Text string "json:\"text,omitempty\""
}{Text: msg.Footer()}
}{Text: parseBacklashes(msg.Footer())}
}

if msg.HeaderText() != "" {
Expand All @@ -2246,7 +2245,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
Video *wacMTMedia "json:\"video,omitempty\""
Image *wacMTMedia "json:\"image,omitempty\""
Document *wacMTMedia "json:\"document,omitempty\""
}{Type: "text", Text: msg.HeaderText()}
}{Type: "text", Text: parseBacklashes(msg.HeaderText())}
}
payload.Interactive = &interactive
}
Expand Down Expand Up @@ -2354,7 +2353,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
if msg.Footer() != "" {
interactive.Footer = &struct {
Text string "json:\"text,omitempty\""
}{Text: msg.Footer()}
}{Text: parseBacklashes(msg.Footer())}
}

if len(msg.Attachments()) > 0 {
Expand Down Expand Up @@ -2478,7 +2477,7 @@ func (h *handler) sendCloudAPIWhatsappMsg(ctx context.Context, msg courier.Msg)
interactive.Footer = &struct {
Text string "json:\"text,omitempty\""
}{
Text: msg.Footer(),
Text: parseBacklashes(msg.Footer()),
}
}

Expand Down
50 changes: 50 additions & 0 deletions handlers/facebookapp/facebookapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,10 +877,59 @@ var SendTestCasesWAC = []ChannelSendTestCase{
SendPrep: setSendURL},
}

var CachedSendTestCasesWAC = []ChannelSendTestCase{
{Label: "Interactive Button Message Send with attachment with cached attachment",
Text: "Interactive Button Msg", URN: "whatsapp:250788123123", QuickReplies: []string{"BUTTON1"},
Status: "W", ExternalID: "157b5e14568e8",
Attachments: []string{`application/pdf:https://foo.bar/document.pdf`},
Responses: map[MockedRequest]MockedResponse{
MockedRequest{
Method: "POST",
Path: "/12345_ID/media",
BodyContains: "media bytes",
}: MockedResponse{
Status: 201,
Body: `{"id":"157b5e14568e8"}`,
},
MockedRequest{
Method: "POST",
Path: "/12345_ID/messages",
Body: `{"messaging_product":"whatsapp","recipient_type":"individual","to":"250788123123","type":"interactive","interactive":{"type":"button","header":{"type":"document","document":{"id":"157b5e14568e8","filename":"document.pdf"}},"body":{"text":"Interactive Button Msg"},"action":{"buttons":[{"type":"reply","reply":{"id":"0","title":"BUTTON1"}}]}}}`,
}: MockedResponse{
Status: 201,
Body: `{ "messages": [{"id": "157b5e14568e8"}] }`,
},
},
SendPrep: setSendURL},
}

func mockAttachmentURLs(mediaServer *httptest.Server, testCases []ChannelSendTestCase) []ChannelSendTestCase {
casesWithMockedUrls := make([]ChannelSendTestCase, len(testCases))

for i, testCase := range testCases {
mockedCase := testCase

for j, attachment := range testCase.Attachments {
mockedCase.Attachments[j] = strings.Replace(attachment, "https://foo.bar", mediaServer.URL, 1)
}
casesWithMockedUrls[i] = mockedCase
}
return casesWithMockedUrls
}

func TestSending(t *testing.T) {
uuids.SetGenerator(uuids.NewSeededGenerator(1234))
defer uuids.SetGenerator(uuids.DefaultGenerator)

mediaServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
res.WriteHeader(200)
res.Write([]byte("media bytes"))
}))
defer mediaServer.Close()
CachedSendTestCasesWAC := mockAttachmentURLs(mediaServer, CachedSendTestCasesWAC)
SendTestCasesWAC = append(SendTestCasesWAC, CachedSendTestCasesWAC...)

// shorter max msg length for testing
maxMsgLengthFBA = 100
maxMsgLengthIG = 100
Expand All @@ -891,6 +940,7 @@ func TestSending(t *testing.T) {
RunChannelSendTestCases(t, ChannelFBA, newHandler("FBA", "Facebook", false), SendTestCasesFBA, nil)
RunChannelSendTestCases(t, ChannelIG, newHandler("IG", "Instagram", false), SendTestCasesIG, nil)
RunChannelSendTestCases(t, ChannelWAC, newHandler("WAC", "Cloud API WhatsApp", false), SendTestCasesWAC, nil)

}

func TestSigning(t *testing.T) {
Expand Down
28 changes: 15 additions & 13 deletions handlers/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
if msg.Footer() != "" {
payload.Interactive.Footer = &struct {
Text string "json:\"text\""
}{Text: msg.Footer()}
}{Text: parseBacklashes(msg.Footer())}
}

if msg.HeaderText() != "" {
Expand All @@ -890,7 +890,7 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
Video mediaObject "json:\"video,omitempty\""
Image mediaObject "json:\"image,omitempty\""
Document mediaObject "json:\"document,omitempty\""
}{Type: "text", Text: msg.HeaderText()}
}{Type: "text", Text: parseBacklashes(msg.HeaderText())}
}

btns := make([]mtButton, len(qrs))
Expand Down Expand Up @@ -958,7 +958,7 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
if msg.Footer() != "" {
payload.Interactive.Footer = &struct {
Text string "json:\"text\""
}{Text: msg.Footer()}
}{Text: parseBacklashes(msg.Footer())}
}
}
payload.Interactive.Action.Sections = []mtSection{section}
Expand Down Expand Up @@ -986,14 +986,14 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
}{
Name: "cta_url",
Parameters: map[string]string{
"display_text": ctaMessage.DisplayText,
"display_text": parseBacklashes(ctaMessage.DisplayText),
"url": ctaMessage.URL,
},
}
if msg.Footer() != "" {
payload.Interactive.Footer = &struct {
Text string "json:\"text\""
}{Text: msg.Footer()}
}{Text: parseBacklashes(msg.Footer())}
}

if msg.HeaderText() != "" {
Expand All @@ -1005,7 +1005,7 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
Document mediaObject "json:\"document,omitempty\""
}{
Type: "text",
Text: msg.HeaderText(),
Text: parseBacklashes(msg.HeaderText()),
}
}
payloads = append(payloads, payload)
Expand Down Expand Up @@ -1175,6 +1175,7 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
mimeType = splitedAttType[0]

mediaID, mediaLogs, err := h.fetchMediaID(msg, mimeType, mediaURL)
filename, _ := utils.BasePathForURL(mediaURL)
if len(mediaLogs) > 0 {
logs = append(logs, mediaLogs...)
}
Expand All @@ -1186,6 +1187,7 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
}
mediaPayload := &mediaObject{ID: mediaID, Link: mediaURL}
if strings.HasPrefix(mimeType, "application") {
mediaPayload.Filename = filename
payload.Interactive.Header = &struct {
Type string "json:\"type\""
Text string "json:\"text,omitempty\""
Expand Down Expand Up @@ -1244,7 +1246,7 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
if msg.Footer() != "" {
payload.Interactive.Footer = &struct {
Text string "json:\"text\""
}{Text: msg.Text()}
}{Text: parseBacklashes(msg.Footer())}
}
payloads = append(payloads, payload)

Expand Down Expand Up @@ -1281,7 +1283,7 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
if msg.Footer() != "" {
payload.Interactive.Footer = &struct {
Text string "json:\"text\""
}{Text: msg.Text()}
}{Text: parseBacklashes(msg.Footer())}
}
}

Expand All @@ -1307,7 +1309,7 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
payload.Interactive.Type = "cta_url"
payload.Interactive.Body = struct {
Text string "json:\"text\""
}{msg.Text()}
}{parseBacklashes(msg.Text())}
payload.Interactive.Action = &struct {
Button string "json:\"button,omitempty\""
Sections []mtSection "json:\"sections,omitempty\""
Expand All @@ -1319,15 +1321,15 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
}{
Name: "cta_url",
Parameters: map[string]string{
"display_text": ctaMessage.DisplayText,
"display_text": parseBacklashes(ctaMessage.DisplayText),
"url": ctaMessage.URL,
},
}

if msg.Footer() != "" {
payload.Interactive.Footer = &struct {
Text string "json:\"text\""
}{Text: msg.Footer()}
}{Text: parseBacklashes(msg.Footer())}
}

if msg.HeaderText() != "" {
Expand All @@ -1339,7 +1341,7 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
Document mediaObject "json:\"document,omitempty\""
}{
Type: "text",
Text: msg.HeaderText(),
Text: parseBacklashes(msg.HeaderText()),
}
}
payloads = append(payloads, payload)
Expand Down Expand Up @@ -1429,7 +1431,7 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
payload.Interactive.Footer = &struct {
Text string "json:\"text\""
}{
Text: msg.Footer(),
Text: parseBacklashes(msg.Footer()),
}
}

Expand Down
Loading

0 comments on commit c44fcd2

Please sign in to comment.