Skip to content

Commit

Permalink
Make sure text are sent after audio attachments for WA channels
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed May 30, 2022
1 parent f68b9e0 commit 1b68664
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
27 changes: 27 additions & 0 deletions handlers/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,8 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
isInteractiveMsgCompatible := semver.Compare(wppVersion, interactiveMsgMinSupVersion)
isInteractiveMsg := (isInteractiveMsgCompatible >= 0) && (len(qrs) > 0)

textAsCaption := false

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

Expand Down Expand Up @@ -608,6 +610,7 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
}
if attachmentCount == 0 && !isInteractiveMsg {
mediaPayload.Caption = msg.Text()
textAsCaption = true
}
mediaPayload.Filename, err = utils.BasePathForURL(fileURL)

Expand All @@ -624,6 +627,7 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
}
if attachmentCount == 0 && !isInteractiveMsg {
mediaPayload.Caption = msg.Text()
textAsCaption = true
}
payload.Image = mediaPayload
payloads = append(payloads, payload)
Expand All @@ -634,6 +638,7 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
}
if attachmentCount == 0 && !isInteractiveMsg {
mediaPayload.Caption = msg.Text()
textAsCaption = true
}
payload.Video = mediaPayload
payloads = append(payloads, payload)
Expand All @@ -646,6 +651,28 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
}
}

if !textAsCaption && !isInteractiveMsg {
for _, part := range parts {

//check if you have a link
var payload mtTextPayload
if strings.Contains(part, "https://") || strings.Contains(part, "http://") {
payload = mtTextPayload{
To: msg.URN().Path(),
Type: "text",
PreviewURL: true,
}
} else {
payload = mtTextPayload{
To: msg.URN().Path(),
Type: "text",
}
}
payload.Text.Body = part
payloads = append(payloads, payload)
}
}

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
Expand Down
10 changes: 9 additions & 1 deletion handlers/whatsapp/whatsapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ var defaultSendTestCases = []ChannelSendTestCase{
RequestBody: `{"to":"250788123123","type":"text","text":{"body":"Error"}}`,
SendPrep: setSendURL},
{Label: "Audio Send",
Text: "audio has no caption",
Text: "audio has no caption, sent as text",
URN: "whatsapp:250788123123",
Status: "W", ExternalID: "157b5e14568e8",
Attachments: []string{"audio/mpeg:https://foo.bar/audio.mp3"},
Expand All @@ -426,6 +426,14 @@ var defaultSendTestCases = []ChannelSendTestCase{
Status: 201,
Body: `{ "messages": [{"id": "157b5e14568e8"}] }`,
},
MockedRequest{
Method: "POST",
Path: "/v1/messages",
Body: `{"to":"250788123123","type":"text","text":{"body":"audio has no caption, sent as text"}}`,
}: MockedResponse{
Status: 201,
Body: `{ "messages": [{"id": "157b5e14568e8"}] }`,
},
},
SendPrep: setSendURL,
},
Expand Down

0 comments on commit 1b68664

Please sign in to comment.