Skip to content

Commit

Permalink
Merge pull request nyaruka#431 from Ilhasoft/feature/preview-links-wpp
Browse files Browse the repository at this point in the history
Support for viewing sent links in Whatsapp messages
  • Loading branch information
rowanseymour authored Apr 14, 2022
2 parents 1968da2 + c15c3d6 commit e61ea45
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
18 changes: 15 additions & 3 deletions handlers/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ var waIgnoreStatuses = map[string]bool{
type mtTextPayload struct {
To string `json:"to" validate:"required"`
Type string `json:"type" validate:"required"`
PreviewURL bool `json:"preview_url,omitempty"`
Text struct {
Body string `json:"body" validate:"required"`
} `json:"text"`
Expand Down Expand Up @@ -799,9 +800,20 @@ func buildPayloads(msg courier.Msg, h *handler) ([]interface{}, []*courier.Chann
}
} else {
for _, part := range parts {
payload := mtTextPayload{
To: msg.URN().Path(),
Type: "text",

//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)
Expand Down
6 changes: 6 additions & 0 deletions handlers/whatsapp/whatsapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,12 @@ func setSendURL(s *httptest.Server, h courier.ChannelHandler, c courier.Channel,
}

var defaultSendTestCases = []ChannelSendTestCase{
{Label: "Link Sending",
Text: "Link Sending https://link.com", URN: "whatsapp:250788123123", Path: "/v1/messages",
Status: "W", ExternalID: "157b5e14568e8",
ResponseBody: `{ "messages": [{"id": "157b5e14568e8"}] }`, ResponseStatus: 201,
RequestBody: `{"to":"250788123123","type":"text","preview_url":true,"text":{"body":"Link Sending https://link.com"}}`,
SendPrep: setSendURL},
{Label: "Plain Send",
Text: "Simple Message", URN: "whatsapp:250788123123", Path: "/v1/messages",
Status: "W", ExternalID: "157b5e14568e8",
Expand Down

0 comments on commit e61ea45

Please sign in to comment.