Skip to content

Commit

Permalink
Support sending WA quick replies when we have attachments too
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Jan 19, 2022
1 parent fd95b68 commit e3f9600
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 9 deletions.
72 changes: 63 additions & 9 deletions handlers/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,13 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
var logs []*courier.ChannelLog
var err error

parts := handlers.SplitMsgByChannel(msg.Channel(), msg.Text(), maxMsgLength)

qrs := msg.QuickReplies()
wppVersion := msg.Channel().ConfigForKey("version", "0").(string)
isInteractiveMsgCompatible := semver.Compare(wppVersion, interactiveMsgMinSupVersion)
isInteractiveMsg := (isInteractiveMsgCompatible >= 0) && (len(qrs) > 0)

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

Expand Down Expand Up @@ -595,7 +602,7 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
To: msg.URN().Path(),
Type: "document",
}
if attachmentCount == 0 {
if attachmentCount == 0 && !isInteractiveMsg {
mediaPayload.Caption = msg.Text()
}
mediaPayload.Filename, err = utils.BasePathForURL(mediaURL)
Expand All @@ -611,7 +618,7 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
To: msg.URN().Path(),
Type: "image",
}
if attachmentCount == 0 {
if attachmentCount == 0 && !isInteractiveMsg {
mediaPayload.Caption = msg.Text()
}
payload.Image = mediaPayload
Expand All @@ -621,7 +628,7 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
To: msg.URN().Path(),
Type: "video",
}
if attachmentCount == 0 {
if attachmentCount == 0 && !isInteractiveMsg {
mediaPayload.Caption = msg.Text()
}
payload.Video = mediaPayload
Expand All @@ -634,6 +641,59 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
break
}
}

if isInteractiveMsg {
for i, part := range parts {
if i < (len(parts) - 1) { //if split into more than one message, the first parts will be text and the last interactive
payload := mtTextPayload{
To: msg.URN().Path(),
Type: "text",
}
payload.Text.Body = part
payloads = append(payloads, payload)

} else {
payload := mtInteractivePayload{
To: msg.URN().Path(),
Type: "interactive",
}

// up to 3 qrs the interactive message will be button type, otherwise it will be list
if len(qrs) <= 3 {
payload.Interactive.Type = "button"
payload.Interactive.Body.Text = part
btns := make([]mtButton, len(qrs))
for i, qr := range qrs {
btns[i] = mtButton{
Type: "reply",
}
btns[i].Reply.ID = fmt.Sprint(i)
btns[i].Reply.Title = qr
}
payload.Interactive.Action.Buttons = btns
payloads = append(payloads, payload)
} else {
payload.Interactive.Type = "list"
payload.Interactive.Body.Text = part
payload.Interactive.Action.Button = "Menu"
section := mtSection{
Rows: make([]mtSectionRow, len(qrs)),
}
for i, qr := range qrs {
section.Rows[i] = mtSectionRow{
ID: fmt.Sprint(i),
Title: qr,
}
}
payload.Interactive.Action.Sections = []mtSection{
section,
}
payloads = append(payloads, payload)
}
}
}
}

} else {
// do we have a template?
var templating *MsgTemplating
Expand Down Expand Up @@ -684,12 +744,6 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
payloads = append(payloads, payload)
}
} else {
parts := handlers.SplitMsgByChannel(msg.Channel(), msg.Text(), maxMsgLength)

qrs := msg.QuickReplies()
wppVersion := msg.Channel().ConfigForKey("version", "0").(string)
isInteractiveMsgCompatible := semver.Compare(wppVersion, interactiveMsgMinSupVersion)
isInteractiveMsg := (isInteractiveMsgCompatible >= 0) && (len(qrs) > 0)

if isInteractiveMsg {
for i, part := range parts {
Expand Down
46 changes: 46 additions & 0 deletions handlers/whatsapp/whatsapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,52 @@ var defaultSendTestCases = []ChannelSendTestCase{
ResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, ResponseStatus: 201,
RequestBody: `{"to":"250788123123","type":"interactive","interactive":{"type":"list","body":{"text":"Interactive List Msg"},"action":{"button":"Menu","sections":[{"rows":[{"id":"0","title":"ROW1"},{"id":"1","title":"ROW2"},{"id":"2","title":"ROW3"},{"id":"3","title":"ROW4"}]}]}}}`,
SendPrep: setSendURL},
{Label: "Interactive Button Message Send with attachment",
Text: "Interactive Button Msg", URN: "whatsapp:250788123123", QuickReplies: []string{"BUTTON1"},
Status: "W", ExternalID: "157b5e14568e8",
Attachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
Responses: map[MockedRequest]MockedResponse{
MockedRequest{
Method: "POST",
Path: "/v1/messages",
Body: `{"to":"250788123123","type":"image","image":{"link":"https://foo.bar/image.jpg"}}`,
}: MockedResponse{
Status: 201,
Body: `{ "messages": [{"id": "157b5e14568e8"}] }`,
},
MockedRequest{
Method: "POST",
Path: "/v1/messages",
Body: `{"to":"250788123123","type":"interactive","interactive":{"type":"button","body":{"text":"Interactive Button Msg"},"action":{"buttons":[{"type":"reply","reply":{"id":"0","title":"BUTTON1"}}]}}}`,
}: MockedResponse{
Status: 201,
Body: `{ "messages": [{"id": "157b5e14568e8"}] }`,
},
},
SendPrep: setSendURL},
{Label: "Interactive List Message Send with attachment",
Text: "Interactive List Msg", URN: "whatsapp:250788123123", QuickReplies: []string{"ROW1", "ROW2", "ROW3", "ROW4"},
Status: "W", ExternalID: "157b5e14568e8",
Attachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
Responses: map[MockedRequest]MockedResponse{
MockedRequest{
Method: "POST",
Path: "/v1/messages",
Body: `{"to":"250788123123","type":"image","image":{"link":"https://foo.bar/image.jpg"}}`,
}: MockedResponse{
Status: 201,
Body: `{ "messages": [{"id": "157b5e14568e8"}] }`,
},
MockedRequest{
Method: "POST",
Path: "/v1/messages",
Body: `{"to":"250788123123","type":"interactive","interactive":{"type":"list","body":{"text":"Interactive List Msg"},"action":{"button":"Menu","sections":[{"rows":[{"id":"0","title":"ROW1"},{"id":"1","title":"ROW2"},{"id":"2","title":"ROW3"},{"id":"3","title":"ROW4"}]}]}}}`,
}: MockedResponse{
Status: 201,
Body: `{ "messages": [{"id": "157b5e14568e8"}] }`,
},
},
SendPrep: setSendURL},
}

var mediaCacheSendTestCases = []ChannelSendTestCase{
Expand Down

0 comments on commit e3f9600

Please sign in to comment.