Skip to content

Commit ba7fa36

Browse files
committed
Support receiving stickers
1 parent 7d3c854 commit ba7fa36

File tree

5 files changed

+101
-6
lines changed

5 files changed

+101
-6
lines changed

Diff for: handlers/dialog360/handler.go

+2
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ func (h *handler) processWhatsAppPayload(ctx context.Context, channel courier.Ch
160160
} else if msg.Type == "image" && msg.Image != nil {
161161
text = msg.Image.Caption
162162
mediaURL, err = h.resolveMediaURL(channel, msg.Image.ID, clog)
163+
} else if msg.Type == "sticker" && msg.Sticker != nil {
164+
mediaURL, err = h.resolveMediaURL(channel, msg.Sticker.ID, clog)
163165
} else if msg.Type == "video" && msg.Video != nil {
164166
text = msg.Video.Caption
165167
mediaURL, err = h.resolveMediaURL(channel, msg.Video.ID, clog)

Diff for: handlers/dialog360/handler_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@ var testCasesD3C = []IncomingTestCase{
114114
ExpectedAttachments: []string{"https://waba-v2.360dialog.io/whatsapp_business/attachments/?mid=id_image"},
115115
ExpectedDate: time.Date(2016, 1, 30, 1, 57, 9, 0, time.UTC),
116116
},
117+
{
118+
Label: "Receive Valid Sticker Message",
119+
URL: d3CReceiveURL,
120+
Data: string(test.ReadFile("../meta/testdata/wac/stickerWAC.json")),
121+
ExpectedRespStatus: 200,
122+
ExpectedBodyContains: "Handled",
123+
NoQueueErrorCheck: true,
124+
NoInvalidChannelCheck: true,
125+
ExpectedMsgText: Sp(""),
126+
ExpectedURN: "whatsapp:5678",
127+
ExpectedExternalID: "external_id",
128+
ExpectedAttachments: []string{"https://waba-v2.360dialog.io/whatsapp_business/attachments/?mid=id_sticker"},
129+
ExpectedDate: time.Date(2016, 1, 30, 1, 57, 9, 0, time.UTC),
130+
},
117131
{
118132
Label: "Receive Valid Video Message",
119133
URL: d3CReceiveURL,
@@ -272,6 +286,10 @@ func buildMockD3MediaService(testChannels []courier.Channel, testCases []Incomin
272286
fileURL = "https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=id_audio"
273287
}
274288

289+
if strings.HasSuffix(r.URL.Path, "id_sticker") {
290+
fileURL = "https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=id_sticker"
291+
}
292+
275293
w.WriteHeader(http.StatusOK)
276294
w.Write([]byte(fmt.Sprintf(`{ "url": "%s" }`, fileURL)))
277295
}))

Diff for: handlers/meta/handlers.go

+2
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ func (h *handler) processWhatsAppPayload(ctx context.Context, channel courier.Ch
309309
} else if msg.Type == "image" && msg.Image != nil {
310310
text = msg.Image.Caption
311311
mediaURL, err = h.resolveMediaURL(msg.Image.ID, token, clog)
312+
} else if msg.Type == "sticker" && msg.Sticker != nil {
313+
mediaURL, err = h.resolveMediaURL(msg.Sticker.ID, token, clog)
312314
} else if msg.Type == "video" && msg.Video != nil {
313315
text = msg.Video.Caption
314316
mediaURL, err = h.resolveMediaURL(msg.Video.ID, token, clog)

Diff for: handlers/meta/whataspp_test.go

+66-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package meta
22

33
import (
44
"context"
5+
"fmt"
6+
"net/http"
7+
"net/http/httptest"
8+
"strings"
59
"testing"
610
"time"
711

@@ -107,6 +111,21 @@ var whatsappIncomingTests = []IncomingTestCase{
107111
ExpectedDate: time.Date(2016, 1, 30, 1, 57, 9, 0, time.UTC),
108112
PrepRequest: addValidSignature,
109113
},
114+
{
115+
Label: "Receive Valid Sticker Message",
116+
URL: whatappReceiveURL,
117+
Data: string(test.ReadFile("./testdata/wac/stickerWAC.json")),
118+
ExpectedRespStatus: 200,
119+
ExpectedBodyContains: "Handled",
120+
NoQueueErrorCheck: true,
121+
NoInvalidChannelCheck: true,
122+
ExpectedMsgText: Sp(""),
123+
ExpectedURN: "whatsapp:5678",
124+
ExpectedExternalID: "external_id",
125+
ExpectedAttachments: []string{"https://foo.bar/attachmentURL_Sticker"},
126+
ExpectedDate: time.Date(2016, 1, 30, 1, 57, 9, 0, time.UTC),
127+
PrepRequest: addValidSignature,
128+
},
110129
{
111130
Label: "Receive Valid Video Message",
112131
URL: whatappReceiveURL,
@@ -277,7 +296,53 @@ var whatsappIncomingTests = []IncomingTestCase{
277296
}
278297

279298
func TestWhatsAppIncoming(t *testing.T) {
280-
graphURL = createMockGraphAPI().URL
299+
300+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
301+
accessToken := r.Header.Get("Authorization")
302+
defer r.Body.Close()
303+
304+
// invalid auth token
305+
if accessToken != "Bearer a123" && accessToken != "Bearer wac_admin_system_user_token" {
306+
fmt.Printf("Access token: %s\n", accessToken)
307+
http.Error(w, "invalid auth token", http.StatusForbidden)
308+
return
309+
}
310+
311+
if strings.HasSuffix(r.URL.Path, "image") {
312+
w.Write([]byte(`{"url": "https://foo.bar/attachmentURL_Image"}`))
313+
return
314+
}
315+
316+
if strings.HasSuffix(r.URL.Path, "audio") {
317+
w.Write([]byte(`{"url": "https://foo.bar/attachmentURL_Audio"}`))
318+
return
319+
}
320+
321+
if strings.HasSuffix(r.URL.Path, "voice") {
322+
w.Write([]byte(`{"url": "https://foo.bar/attachmentURL_Voice"}`))
323+
return
324+
}
325+
326+
if strings.HasSuffix(r.URL.Path, "video") {
327+
w.Write([]byte(`{"url": "https://foo.bar/attachmentURL_Video"}`))
328+
return
329+
}
330+
331+
if strings.HasSuffix(r.URL.Path, "document") {
332+
w.Write([]byte(`{"url": "https://foo.bar/attachmentURL_Document"}`))
333+
return
334+
}
335+
336+
if strings.HasSuffix(r.URL.Path, "sticker") {
337+
w.Write([]byte(`{"url": "https://foo.bar/attachmentURL_Sticker"}`))
338+
return
339+
}
340+
341+
// valid token
342+
w.Write([]byte(`{"url": "https://foo.bar/attachmentURL"}`))
343+
344+
}))
345+
graphURL = server.URL
281346

282347
RunIncomingTestCases(t, whatsappTestChannels, newHandler("WAC", "Cloud API WhatsApp"), whatsappIncomingTests)
283348
}

Diff for: handlers/meta/whatsapp/api.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ type MOMedia struct {
2323
SHA256 string `json:"sha256"`
2424
}
2525

26+
type wacSticker struct {
27+
Animated bool `json:"animated"`
28+
ID string `json:"id"`
29+
Mimetype string `json:"mime_type"`
30+
SHA256 string `json:"sha256"`
31+
}
32+
2633
type Change struct {
2734
Field string `json:"field"`
2835
Value struct {
@@ -51,11 +58,12 @@ type Change struct {
5158
Text struct {
5259
Body string `json:"body"`
5360
} `json:"text"`
54-
Image *MOMedia `json:"image"`
55-
Audio *MOMedia `json:"audio"`
56-
Video *MOMedia `json:"video"`
57-
Document *MOMedia `json:"document"`
58-
Voice *MOMedia `json:"voice"`
61+
Image *MOMedia `json:"image"`
62+
Audio *MOMedia `json:"audio"`
63+
Video *MOMedia `json:"video"`
64+
Document *MOMedia `json:"document"`
65+
Voice *MOMedia `json:"voice"`
66+
Sticker *wacSticker `json:"sticker"`
5967
Location *struct {
6068
Latitude float64 `json:"latitude"`
6169
Longitude float64 `json:"longitude"`

0 commit comments

Comments
 (0)