Skip to content

Commit

Permalink
Support receiving stickers
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Apr 23, 2024
1 parent 7d3c854 commit ba7fa36
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 6 deletions.
2 changes: 2 additions & 0 deletions handlers/dialog360/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ func (h *handler) processWhatsAppPayload(ctx context.Context, channel courier.Ch
} else if msg.Type == "image" && msg.Image != nil {
text = msg.Image.Caption
mediaURL, err = h.resolveMediaURL(channel, msg.Image.ID, clog)
} else if msg.Type == "sticker" && msg.Sticker != nil {
mediaURL, err = h.resolveMediaURL(channel, msg.Sticker.ID, clog)
} else if msg.Type == "video" && msg.Video != nil {
text = msg.Video.Caption
mediaURL, err = h.resolveMediaURL(channel, msg.Video.ID, clog)
Expand Down
18 changes: 18 additions & 0 deletions handlers/dialog360/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ var testCasesD3C = []IncomingTestCase{
ExpectedAttachments: []string{"https://waba-v2.360dialog.io/whatsapp_business/attachments/?mid=id_image"},
ExpectedDate: time.Date(2016, 1, 30, 1, 57, 9, 0, time.UTC),
},
{
Label: "Receive Valid Sticker Message",
URL: d3CReceiveURL,
Data: string(test.ReadFile("../meta/testdata/wac/stickerWAC.json")),
ExpectedRespStatus: 200,
ExpectedBodyContains: "Handled",
NoQueueErrorCheck: true,
NoInvalidChannelCheck: true,
ExpectedMsgText: Sp(""),
ExpectedURN: "whatsapp:5678",
ExpectedExternalID: "external_id",
ExpectedAttachments: []string{"https://waba-v2.360dialog.io/whatsapp_business/attachments/?mid=id_sticker"},
ExpectedDate: time.Date(2016, 1, 30, 1, 57, 9, 0, time.UTC),
},
{
Label: "Receive Valid Video Message",
URL: d3CReceiveURL,
Expand Down Expand Up @@ -272,6 +286,10 @@ func buildMockD3MediaService(testChannels []courier.Channel, testCases []Incomin
fileURL = "https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=id_audio"
}

if strings.HasSuffix(r.URL.Path, "id_sticker") {
fileURL = "https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=id_sticker"
}

w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf(`{ "url": "%s" }`, fileURL)))
}))
Expand Down
2 changes: 2 additions & 0 deletions handlers/meta/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ func (h *handler) processWhatsAppPayload(ctx context.Context, channel courier.Ch
} else if msg.Type == "image" && msg.Image != nil {
text = msg.Image.Caption
mediaURL, err = h.resolveMediaURL(msg.Image.ID, token, clog)
} else if msg.Type == "sticker" && msg.Sticker != nil {
mediaURL, err = h.resolveMediaURL(msg.Sticker.ID, token, clog)
} else if msg.Type == "video" && msg.Video != nil {
text = msg.Video.Caption
mediaURL, err = h.resolveMediaURL(msg.Video.ID, token, clog)
Expand Down
67 changes: 66 additions & 1 deletion handlers/meta/whataspp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package meta

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -107,6 +111,21 @@ var whatsappIncomingTests = []IncomingTestCase{
ExpectedDate: time.Date(2016, 1, 30, 1, 57, 9, 0, time.UTC),
PrepRequest: addValidSignature,
},
{
Label: "Receive Valid Sticker Message",
URL: whatappReceiveURL,
Data: string(test.ReadFile("./testdata/wac/stickerWAC.json")),
ExpectedRespStatus: 200,
ExpectedBodyContains: "Handled",
NoQueueErrorCheck: true,
NoInvalidChannelCheck: true,
ExpectedMsgText: Sp(""),
ExpectedURN: "whatsapp:5678",
ExpectedExternalID: "external_id",
ExpectedAttachments: []string{"https://foo.bar/attachmentURL_Sticker"},
ExpectedDate: time.Date(2016, 1, 30, 1, 57, 9, 0, time.UTC),
PrepRequest: addValidSignature,
},
{
Label: "Receive Valid Video Message",
URL: whatappReceiveURL,
Expand Down Expand Up @@ -277,7 +296,53 @@ var whatsappIncomingTests = []IncomingTestCase{
}

func TestWhatsAppIncoming(t *testing.T) {
graphURL = createMockGraphAPI().URL

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
accessToken := r.Header.Get("Authorization")
defer r.Body.Close()

// invalid auth token
if accessToken != "Bearer a123" && accessToken != "Bearer wac_admin_system_user_token" {
fmt.Printf("Access token: %s\n", accessToken)
http.Error(w, "invalid auth token", http.StatusForbidden)
return
}

if strings.HasSuffix(r.URL.Path, "image") {
w.Write([]byte(`{"url": "https://foo.bar/attachmentURL_Image"}`))
return
}

if strings.HasSuffix(r.URL.Path, "audio") {
w.Write([]byte(`{"url": "https://foo.bar/attachmentURL_Audio"}`))
return
}

if strings.HasSuffix(r.URL.Path, "voice") {
w.Write([]byte(`{"url": "https://foo.bar/attachmentURL_Voice"}`))
return
}

if strings.HasSuffix(r.URL.Path, "video") {
w.Write([]byte(`{"url": "https://foo.bar/attachmentURL_Video"}`))
return
}

if strings.HasSuffix(r.URL.Path, "document") {
w.Write([]byte(`{"url": "https://foo.bar/attachmentURL_Document"}`))
return
}

if strings.HasSuffix(r.URL.Path, "sticker") {
w.Write([]byte(`{"url": "https://foo.bar/attachmentURL_Sticker"}`))
return
}

// valid token
w.Write([]byte(`{"url": "https://foo.bar/attachmentURL"}`))

}))
graphURL = server.URL

RunIncomingTestCases(t, whatsappTestChannels, newHandler("WAC", "Cloud API WhatsApp"), whatsappIncomingTests)
}
Expand Down
18 changes: 13 additions & 5 deletions handlers/meta/whatsapp/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ type MOMedia struct {
SHA256 string `json:"sha256"`
}

type wacSticker struct {
Animated bool `json:"animated"`
ID string `json:"id"`
Mimetype string `json:"mime_type"`
SHA256 string `json:"sha256"`
}

type Change struct {
Field string `json:"field"`
Value struct {
Expand Down Expand Up @@ -51,11 +58,12 @@ type Change struct {
Text struct {
Body string `json:"body"`
} `json:"text"`
Image *MOMedia `json:"image"`
Audio *MOMedia `json:"audio"`
Video *MOMedia `json:"video"`
Document *MOMedia `json:"document"`
Voice *MOMedia `json:"voice"`
Image *MOMedia `json:"image"`
Audio *MOMedia `json:"audio"`
Video *MOMedia `json:"video"`
Document *MOMedia `json:"document"`
Voice *MOMedia `json:"voice"`
Sticker *wacSticker `json:"sticker"`
Location *struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Expand Down

0 comments on commit ba7fa36

Please sign in to comment.