Skip to content

Commit

Permalink
Merge pull request #159 from Ilhasoft/fix/template-media-wpp
Browse files Browse the repository at this point in the history
Add support for sending templates with media in WA
  • Loading branch information
Robi9 authored Jul 12, 2024
2 parents 8a515e9 + 51487d6 commit d64a705
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 23 deletions.
68 changes: 45 additions & 23 deletions handlers/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,25 +484,12 @@ type LocalizableParam struct {
Default string `json:"default"`
}

type mmtImage struct {
Link string `json:"link,omitempty"`
}

type mmtDocument struct {
Link string `json:"link,omitempty"`
Filename string `json:"filename,omitempty"`
}

type mmtVideo struct {
Link string `json:"link,omitempty"`
}

type Param struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
Image *mmtImage `json:"image,omitempty"`
Document *mmtDocument `json:"document,omitempty"`
Video *mmtVideo `json:"video,omitempty"`
Image *mediaObject `json:"image,omitempty"`
Document *mediaObject `json:"document,omitempty"`
Video *mediaObject `json:"video,omitempty"`
}

type Component struct {
Expand Down Expand Up @@ -650,7 +637,14 @@ func buildPayloads(msg courier.Msg, h *handler, clog *courier.ChannelLog) ([]int

textAsCaption := false

if len(msg.Attachments()) > 0 {
// do we have a template?
var templating *MsgTemplating
templating, err = h.getTemplate(msg)
if err != nil {
return nil, errors.Wrapf(err, "unable to decode template: %s for channel: %s", string(msg.Metadata()), msg.Channel().UUID())
}

if len(msg.Attachments()) > 0 && templating == nil {
for attachmentCount, attachment := range msg.Attachments() {

mimeType, mediaURL := handlers.SplitAttachment(attachment)
Expand Down Expand Up @@ -790,12 +784,6 @@ func buildPayloads(msg courier.Msg, h *handler, clog *courier.ChannelLog) ([]int
}

} else {
// do we have a template?
var templating *MsgTemplating
templating, err := h.getTemplate(msg)
if err != nil {
return nil, errors.Wrapf(err, "unable to decode template: %s for channel: %s", string(msg.Metadata()), msg.Channel().UUID())
}
if templating != nil {
namespace := templating.Namespace
if namespace == "" {
Expand Down Expand Up @@ -836,6 +824,40 @@ func buildPayloads(msg courier.Msg, h *handler, clog *courier.ChannelLog) ([]int
}
payload.Template.Components = append(payload.Template.Components, *component)

if len(msg.Attachments()) > 0 {

header := &Component{Type: "header"}

mimeType, mediaURL := handlers.SplitAttachment(msg.Attachments()[0])
mediaID, err := h.fetchMediaID(msg, mimeType, mediaURL, clog)
if err != nil {
logrus.WithField("channel_uuid", msg.Channel().UUID().String()).WithError(err).Error("error while uploading media to whatsapp")
}
if err == nil && mediaID != "" {
mediaURL = ""
}
mimeType = strings.Split(mimeType, "/")[0]
if mimeType == "application" {
mimeType = "document"
}

media := mediaObject{ID: mediaID, Link: mediaURL}
if mimeType == "image" {
header.Parameters = append(header.Parameters, Param{Type: "image", Image: &media})
} else if mimeType == "video" {
header.Parameters = append(header.Parameters, Param{Type: "video", Video: &media})
} else if mimeType == "document" {
media.Filename, err = utils.BasePathForURL(mediaURL)
if err != nil {
return nil, err
}
header.Parameters = append(header.Parameters, Param{Type: "document", Document: &media})
} else {
return nil, fmt.Errorf("unknown attachment mime type: %s", mimeType)
}
payload.Template.Components = append(payload.Template.Components, *header)
}

payloads = append(payloads, payload)
}
} else {
Expand Down
30 changes: 30 additions & 0 deletions handlers/whatsapp/whatsapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,36 @@ var defaultSendTestCases = []ChannelSendTestCase{
ExpectedExternalID: "157b5e14568e8",
SendPrep: setSendURL,
},
{
Label: "Media Message Template Send - Image",
MsgText: "Media Message Msg", MsgURN: "whatsapp:250788123123",
ExpectedMsgStatus: "W", ExpectedExternalID: "157b5e14568e8",
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "namespace": "wa_template_namespace", "language": "eng", "country": "US", "variables": ["Chef", "tomorrow"]}}`),
MsgAttachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
MockResponses: map[MockedRequest]*httpx.MockResponse{
{
Method: "POST",
Path: "/v1/messages",
Body: `{"to":"250788123123","type":"template","template":{"namespace":"wa_template_namespace","name":"revive_issue","language":{"policy":"deterministic","code":"en_US"},"components":[{"type":"body","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]},{"type":"header","parameters":[{"type":"image","image":{"link":"https://foo.bar/image.jpg"}}]}]}}`,
}: httpx.NewMockResponse(201, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
},
SendPrep: setSendURL,
},
{
Label: "Media Message Template Send - Video",
MsgText: "Media Message Msg", MsgURN: "whatsapp:250788123123",
ExpectedMsgStatus: "W", ExpectedExternalID: "157b5e14568e8",
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "namespace": "wa_template_namespace", "language": "eng", "country": "US", "variables": ["Chef", "tomorrow"]}}`),
MsgAttachments: []string{"video/mp4:https://foo.bar/video.mp4"},
MockResponses: map[MockedRequest]*httpx.MockResponse{
{
Method: "POST",
Path: "/v1/messages",
Body: `{"to":"250788123123","type":"template","template":{"namespace":"wa_template_namespace","name":"revive_issue","language":{"policy":"deterministic","code":"en_US"},"components":[{"type":"body","parameters":[{"type":"text","text":"Chef"},{"type":"text","text":"tomorrow"}]},{"type":"header","parameters":[{"type":"video","video":{"link":"https://foo.bar/video.mp4"}}]}]}}`,
}: httpx.NewMockResponse(201, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
},
SendPrep: setSendURL,
},
{
Label: "Template Invalid Language",
MsgText: "templated message",
Expand Down

0 comments on commit d64a705

Please sign in to comment.