Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 46 additions & 21 deletions handlers/dialog360/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/nyaruka/courier/utils"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/gocommon/urns"
"github.com/pkg/errors"
)

const (
Expand All @@ -29,6 +30,15 @@ var (
maxMsgLength = 4096
)

// see https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#supported-media-types
var mediaSupport = map[handlers.MediaType]handlers.MediaTypeSupport{
handlers.MediaType("image/webp"): {Types: []string{"image/webp"}, MaxBytes: 100 * 1024, MaxWidth: 512, MaxHeight: 512},
handlers.MediaTypeImage: {Types: []string{"image/jpeg", "image/png"}, MaxBytes: 5 * 1024 * 1024},
handlers.MediaTypeAudio: {Types: []string{"audio/aac", "audio/mp4", "audio/mpeg", "audio/amr", "audio/ogg"}, MaxBytes: 16 * 1024 * 1024},
handlers.MediaTypeVideo: {Types: []string{"video/mp4", "video/3gp"}, MaxBytes: 16 * 1024 * 1024},
handlers.MediaTypeApplication: {MaxBytes: 100 * 1024 * 1024},
}

func init() {
courier.RegisterHandler(newWAHandler(courier.ChannelType("D3C"), "360Dialog"))
}
Expand Down Expand Up @@ -160,6 +170,8 @@ func (h *handler) processWhatsAppPayload(ctx context.Context, channel courier.Ch
} else if msg.Type == "image" && msg.Image != nil {
text = msg.Image.Caption
mediaURL, err = h.resolveMediaURL(channel, msg.Image.ID, clog)
} else if msg.Type == "sticker" && msg.Sticker != nil {
mediaURL, err = h.resolveMediaURL(channel, msg.Sticker.ID, clog)
} else if msg.Type == "video" && msg.Video != nil {
text = msg.Video.Caption
mediaURL, err = h.resolveMediaURL(channel, msg.Video.ID, clog)
Expand Down Expand Up @@ -311,23 +323,28 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.Sen

var payloadAudio whatsapp.SendRequest

for i := 0; i < len(msgParts)+len(msg.Attachments()); i++ {
attachments, err := handlers.ResolveAttachments(ctx, h.Backend(), msg.Attachments(), mediaSupport, false, clog)
if err != nil {
return errors.Wrap(err, "error resolving attachments")
}

for i := 0; i < len(msgParts)+len(attachments); i++ {
payload := whatsapp.SendRequest{MessagingProduct: "whatsapp", RecipientType: "individual", To: msg.URN().Path()}

if len(msg.Attachments()) == 0 {
if len(attachments) == 0 {
// do we have a template?
if msg.Templating() != nil {
payload.Type = "template"
payload.Template = whatsapp.GetTemplatePayload(msg.Templating())
} else {
if i < (len(msgParts) + len(msg.Attachments()) - 1) {
if i < (len(msgParts) + len(attachments) - 1) {
// this is still a msg part
text := &whatsapp.Text{PreviewURL: false}
payload.Type = "text"
if strings.Contains(msgParts[i-len(msg.Attachments())], "https://") || strings.Contains(msgParts[i-len(msg.Attachments())], "http://") {
if strings.Contains(msgParts[i-len(attachments)], "https://") || strings.Contains(msgParts[i-len(attachments)], "http://") {
text.PreviewURL = true
}
text.Body = msgParts[i-len(msg.Attachments())]
text.Body = msgParts[i-len(attachments)]
payload.Text = text
} else {
if len(qrs) > 0 {
Expand All @@ -343,7 +360,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.Sen
if len(qrs) <= 3 {
interactive := whatsapp.Interactive{Type: "button", Body: struct {
Text string "json:\"text\""
}{Text: msgParts[i-len(msg.Attachments())]}}
}{Text: msgParts[i-len(attachments)]}}

btns := make([]whatsapp.Button, len(qrs))
for i, qr := range qrs {
Expand All @@ -362,7 +379,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.Sen
} else {
interactive := whatsapp.Interactive{Type: "list", Body: struct {
Text string "json:\"text\""
}{Text: msgParts[i-len(msg.Attachments())]}}
}{Text: msgParts[i-len(attachments)]}}

section := whatsapp.Section{
Rows: make([]whatsapp.SectionRow, len(qrs)),
Expand All @@ -388,31 +405,38 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.Sen
// this is still a msg part
text := &whatsapp.Text{PreviewURL: false}
payload.Type = "text"
if strings.Contains(msgParts[i-len(msg.Attachments())], "https://") || strings.Contains(msgParts[i-len(msg.Attachments())], "http://") {
if strings.Contains(msgParts[i-len(attachments)], "https://") || strings.Contains(msgParts[i-len(attachments)], "http://") {
text.PreviewURL = true
}
text.Body = msgParts[i-len(msg.Attachments())]
text.Body = msgParts[i-len(attachments)]
payload.Text = text
}
}
}

} else if i < len(msg.Attachments()) && (len(qrs) == 0 || len(qrs) > 3) {
attType, attURL := handlers.SplitAttachment(msg.Attachments()[i])
attType = strings.Split(attType, "/")[0]
} else if i < len(attachments) && (len(qrs) == 0 || len(qrs) > 3) {
attURL := attachments[i].Media.URL()
attType := attachments[i].Type
attContentType := attachments[i].Media.ContentType()

if attType == "application" {
attType = "document"
}
payload.Type = attType
payload.Type = string(attType)
media := whatsapp.Media{Link: attURL}

if len(msgParts) == 1 && attType != "audio" && len(msg.Attachments()) == 1 && len(msg.QuickReplies()) == 0 {
if len(msgParts) == 1 && attType != "audio" && len(attachments) == 1 && len(msg.QuickReplies()) == 0 {
media.Caption = msgParts[i]
hasCaption = true
}

if attType == "image" {
payload.Image = &media
if attContentType == "image/webp" {
payload.Type = "sticker"
payload.Sticker = &media
} else {
payload.Image = &media
}
} else if attType == "audio" {
payload.Audio = &media
} else if attType == "video" {
Expand Down Expand Up @@ -442,10 +466,11 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.Sen
Text string "json:\"text\""
}{Text: msgParts[i]}}

if len(msg.Attachments()) > 0 {
if len(attachments) > 0 {
hasCaption = true
attType, attURL := handlers.SplitAttachment(msg.Attachments()[i])
attType = strings.Split(attType, "/")[0]
attURL := attachments[i].Media.URL()
attType := attachments[i].Type

if attType == "application" {
attType = "document"
}
Expand Down Expand Up @@ -517,7 +542,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.Sen
} else {
interactive := whatsapp.Interactive{Type: "list", Body: struct {
Text string "json:\"text\""
}{Text: msgParts[i-len(msg.Attachments())]}}
}{Text: msgParts[i-len(attachments)]}}

section := whatsapp.Section{
Rows: make([]whatsapp.SectionRow, len(qrs)),
Expand All @@ -543,10 +568,10 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.Sen
// this is still a msg part
text := &whatsapp.Text{PreviewURL: false}
payload.Type = "text"
if strings.Contains(msgParts[i-len(msg.Attachments())], "https://") || strings.Contains(msgParts[i-len(msg.Attachments())], "http://") {
if strings.Contains(msgParts[i-len(attachments)], "https://") || strings.Contains(msgParts[i-len(attachments)], "http://") {
text.PreviewURL = true
}
text.Body = msgParts[i-len(msg.Attachments())]
text.Body = msgParts[i-len(attachments)]
payload.Text = text
}
}
Expand Down
Loading