Skip to content

Commit

Permalink
fix document name on send message with quick replies if media is cach…
Browse files Browse the repository at this point in the history
…ed on whatsapp cloud
  • Loading branch information
rasoro committed Oct 22, 2024
1 parent 45744cd commit 04cc918
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
3 changes: 1 addition & 2 deletions handlers/facebookapp/facebookapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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\""
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 @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit 04cc918

Please sign in to comment.