diff --git a/handlers/facebookapp/facebookapp.go b/handlers/facebookapp/facebookapp.go index 19dad01a..4c9b0711 100644 --- a/handlers/facebookapp/facebookapp.go +++ b/handlers/facebookapp/facebookapp.go @@ -1993,6 +1993,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) @@ -2006,9 +2007,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\"" diff --git a/handlers/facebookapp/facebookapp_test.go b/handlers/facebookapp/facebookapp_test.go index 2fa59994..f303a37d 100644 --- a/handlers/facebookapp/facebookapp_test.go +++ b/handlers/facebookapp/facebookapp_test.go @@ -870,10 +870,58 @@ 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) + // shorter max msg length for testing maxMsgLengthFBA = 100 maxMsgLengthIG = 100 @@ -884,6 +932,8 @@ 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) + RunChannelSendTestCases(t, ChannelWAC, newHandler("WAC", "Cloud API WhatsApp", false), CachedSendTestCasesWAC, nil) + } func TestSigning(t *testing.T) {