Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read templating from new templating msg field instead of metadata #729

Merged
merged 1 commit into from
Apr 17, 2024
Merged
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
38 changes: 20 additions & 18 deletions backends/rapidpro/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,20 @@

// Msg is our base struct to represent msgs both in our JSON and db representations
type Msg struct {
OrgID_ OrgID `json:"org_id" db:"org_id"`
ID_ courier.MsgID `json:"id" db:"id"`
UUID_ courier.MsgUUID `json:"uuid" db:"uuid"`
Direction_ MsgDirection ` db:"direction"`
Status_ courier.MsgStatus ` db:"status"`
Visibility_ MsgVisibility ` db:"visibility"`
HighPriority_ bool `json:"high_priority" db:"high_priority"`
Text_ string `json:"text" db:"text"`
Attachments_ pq.StringArray `json:"attachments" db:"attachments"`
QuickReplies_ pq.StringArray `json:"quick_replies" db:"quick_replies"`
Locale_ null.String `json:"locale" db:"locale"`
ExternalID_ null.String ` db:"external_id"`
Metadata_ json.RawMessage `json:"metadata" db:"metadata"`
OrgID_ OrgID `json:"org_id" db:"org_id"`
ID_ courier.MsgID `json:"id" db:"id"`
UUID_ courier.MsgUUID `json:"uuid" db:"uuid"`
Direction_ MsgDirection ` db:"direction"`
Status_ courier.MsgStatus ` db:"status"`
Visibility_ MsgVisibility ` db:"visibility"`
HighPriority_ bool `json:"high_priority" db:"high_priority"`
Text_ string `json:"text" db:"text"`
Attachments_ pq.StringArray `json:"attachments" db:"attachments"`
QuickReplies_ pq.StringArray `json:"quick_replies" db:"quick_replies"`
Locale_ null.String `json:"locale" db:"locale"`
Templating_ *courier.Templating `json:"templating" db:"templating"`
ExternalID_ null.String ` db:"external_id"`
Metadata_ json.RawMessage `json:"metadata" db:"metadata"`

ChannelID_ courier.ChannelID ` db:"channel_id"`
ContactID_ ContactID `json:"contact_id" db:"contact_id"`
Expand Down Expand Up @@ -142,11 +143,12 @@
func (m *Msg) Channel() courier.Channel { return m.channel }

// outgoing specific
func (m *Msg) QuickReplies() []string { return m.QuickReplies_ }
func (m *Msg) Locale() i18n.Locale { return i18n.Locale(string(m.Locale_)) }
func (m *Msg) URNAuth() string { return m.URNAuth_ }
func (m *Msg) Origin() courier.MsgOrigin { return m.Origin_ }
func (m *Msg) ContactLastSeenOn() *time.Time { return m.ContactLastSeenOn_ }
func (m *Msg) QuickReplies() []string { return m.QuickReplies_ }
func (m *Msg) Locale() i18n.Locale { return i18n.Locale(string(m.Locale_)) }
func (m *Msg) Templating() *courier.Templating { return m.Templating_ }
func (m *Msg) URNAuth() string { return m.URNAuth_ }
func (m *Msg) Origin() courier.MsgOrigin { return m.Origin_ }
func (m *Msg) ContactLastSeenOn() *time.Time { return m.ContactLastSeenOn_ }

Check warning on line 151 in backends/rapidpro/msg.go

View check run for this annotation

Codecov / codecov/patch

backends/rapidpro/msg.go#L147-L151

Added lines #L147 - L151 were not covered by tests
func (m *Msg) Topic() string {
if m.Metadata_ == nil {
return ""
Expand Down
9 changes: 2 additions & 7 deletions handlers/dialog360/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/nyaruka/courier/utils"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/gocommon/urns"
"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -317,13 +316,9 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.Sen

if len(msg.Attachments()) == 0 {
// do we have a template?
templating, err := whatsapp.GetTemplating(msg)
if err != nil {
return errors.Wrapf(err, "unable to decode template: %s for channel: %s", string(msg.Metadata()), msg.Channel().UUID())
}
if templating != nil {
if msg.Templating() != nil {
payload.Type = "template"
payload.Template = whatsapp.GetTemplatePayload(templating)
payload.Template = whatsapp.GetTemplatePayload(msg.Templating())
} else {
if i < (len(msgParts) + len(msg.Attachments()) - 1) {
// this is still a msg part
Expand Down
11 changes: 5 additions & 6 deletions handlers/dialog360/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dialog360

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -421,11 +420,11 @@ var SendTestCasesD3C = []OutgoingTestCase{
ExpectedExtIDs: []string{"157b5e14568e8"},
},
{
Label: "Template Send",
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng",
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type": "body", "params": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}], "language": "en_US"}}`),
Label: "Template Send",
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng",
MsgTemplating: `{"template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type": "body", "params": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}], "language": "en_US"}`,
MockResponses: map[string][]*httpx.MockResponse{
"https://waba-v2.360dialog.io/messages": {
httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
Expand Down
8 changes: 2 additions & 6 deletions handlers/meta/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,13 +805,9 @@ func (h *handler) sendWhatsAppMsg(ctx context.Context, msg courier.MsgOut, res *

if len(msg.Attachments()) == 0 {
// do we have a template?
templating, err := whatsapp.GetTemplating(msg)
if err != nil {
return errors.Wrapf(err, "unable to decode template: %s for channel: %s", string(msg.Metadata()), msg.Channel().UUID())
}
if templating != nil {
if msg.Templating() != nil {
payload.Type = "template"
payload.Template = whatsapp.GetTemplatePayload(templating)
payload.Template = whatsapp.GetTemplatePayload(msg.Templating())

} else {
if i < (len(msgParts) + len(msg.Attachments()) - 1) {
Expand Down
26 changes: 13 additions & 13 deletions handlers/meta/whataspp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package meta

import (
"context"
"encoding/json"
"testing"
"time"

Expand Down Expand Up @@ -390,11 +389,11 @@ var whatsappOutgoingTests = []OutgoingTestCase{
ExpectedExtIDs: []string{"157b5e14568e8"},
},
{
Label: "Template Send",
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng",
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type":"body", "params": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}], "language": "en_US"}}`),
Label: "Template Send",
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng",
MsgTemplating: `{"template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [{"type":"body", "params": [{"type":"text", "value":"Chef"}, {"type": "text" , "value": "tomorrow"}]}], "language": "en_US"}`,
MockResponses: map[string][]*httpx.MockResponse{
"*/12345_ID/messages": {
httpx.NewMockResponse(201, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
Expand All @@ -406,11 +405,11 @@ var whatsappOutgoingTests = []OutgoingTestCase{
ExpectedExtIDs: []string{"157b5e14568e8"},
},
{
Label: "Template Send, no variables",
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng",
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [], "variables": [], "language": "en_US"}}`),
Label: "Template Send, no variables",
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng",
MsgTemplating: `{"template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" }, "components": [], "variables": [], "language": "en_US"}`,
MockResponses: map[string][]*httpx.MockResponse{
"*/12345_ID/messages": {
httpx.NewMockResponse(200, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
Expand All @@ -426,7 +425,8 @@ var whatsappOutgoingTests = []OutgoingTestCase{
MsgText: "templated message",
MsgURN: "whatsapp:250788123123",
MsgLocale: "eng",
MsgMetadata: json.RawMessage(`{ "templating": { "template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" },"components": [
MsgTemplating: `{"template": { "name": "revive_issue", "uuid": "171f8a4d-f725-46d7-85a6-11aceff0bfe3" },
"components": [
{
"type": "body",
"name": "body",
Expand Down Expand Up @@ -461,7 +461,7 @@ var whatsappOutgoingTests = []OutgoingTestCase{
}
]
}
], "language": "en_US"}}`),
], "language": "en_US"}`,
MockResponses: map[string][]*httpx.MockResponse{
"*/12345_ID/messages": {
httpx.NewMockResponse(201, nil, []byte(`{ "messages": [{"id": "157b5e14568e8"}] }`)),
Expand Down
45 changes: 1 addition & 44 deletions handlers/meta/whatsapp/templates.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,12 @@
package whatsapp

import (
"encoding/json"
"strings"

"github.com/nyaruka/courier"
"github.com/nyaruka/courier/utils"
"github.com/pkg/errors"
)

type MsgTemplating struct {
Template struct {
Name string `json:"name" validate:"required"`
UUID string `json:"uuid" validate:"required"`
} `json:"template" validate:"required,dive"`
Namespace string `json:"namespace"`
Components []struct {
Type string `json:"type"`
Name string `json:"name"`
Params []struct {
Type string `json:"type"`
Value string `json:"value"`
} `json:"params"`
} `json:"components"`
Language string `json:"language"`
}

func GetTemplating(msg courier.MsgOut) (*MsgTemplating, error) {
if len(msg.Metadata()) == 0 {
return nil, nil
}

metadata := &struct {
Templating *MsgTemplating `json:"templating"`
}{}
if err := json.Unmarshal(msg.Metadata(), metadata); err != nil {
return nil, err
}

if metadata.Templating == nil {
return nil, nil
}

if err := utils.Validate(metadata.Templating); err != nil {
return nil, errors.Wrapf(err, "invalid templating definition")
}

return metadata.Templating, nil
}

func GetTemplatePayload(templating *MsgTemplating) *Template {
func GetTemplatePayload(templating *courier.Templating) *Template {
template := &Template{
Name: templating.Template.Name,
Language: &Language{Policy: "deterministic", Code: templating.Language},
Expand Down
45 changes: 6 additions & 39 deletions handlers/meta/whatsapp/templates_test.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,15 @@
package whatsapp_test

import (
"encoding/json"
"fmt"
"testing"

"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers/meta/whatsapp"
"github.com/nyaruka/courier/test"
"github.com/nyaruka/gocommon/jsonx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGetTemplating(t *testing.T) {
msg := test.NewMockMsg(1, "87995844-2017-4ba0-bc73-f3da75b32f9b", nil, "tel:+1234567890", "hi", nil)

// no metadata, no templating
tpl, err := whatsapp.GetTemplating(msg)
assert.NoError(t, err)
assert.Nil(t, tpl)

msg.WithMetadata(json.RawMessage(`{}`))

// no templating in metadata, no templating
tpl, err = whatsapp.GetTemplating(msg)
assert.NoError(t, err)
assert.Nil(t, tpl)

msg.WithMetadata(json.RawMessage(`{"templating": {"foo": "bar"}}`))

// invalid templating in metadata, error
tpl, err = whatsapp.GetTemplating(msg)
assert.Error(t, err, "x")
assert.Nil(t, tpl)

msg.WithMetadata(json.RawMessage(`{"templating": {"template": {"uuid": "4ed5000f-5c94-4143-9697-b7cbd230a381", "name": "Update"}}}`))

// valid templating, no error
tpl, err = whatsapp.GetTemplating(msg)
assert.NoError(t, err)
assert.Equal(t, "4ed5000f-5c94-4143-9697-b7cbd230a381", tpl.Template.UUID)
assert.Equal(t, "Update", tpl.Template.Name)
}

func TestGetTemplatePayload(t *testing.T) {
tcs := []struct {
templating string
Expand Down Expand Up @@ -121,12 +89,11 @@ func TestGetTemplatePayload(t *testing.T) {
}

for _, tc := range tcs {
metadata := json.RawMessage(fmt.Sprintf(`{"templating": %s}`, tc.templating))
msg := test.NewMockMsg(1, "87995844-2017-4ba0-bc73-f3da75b32f9b", nil, "tel:+1234567890", "hi", nil).WithMetadata(metadata)
templating, err := whatsapp.GetTemplating(msg)
require.NoError(t, err)
templating := &courier.Templating{}
jsonx.MustUnmarshal([]byte(tc.templating), templating)

actual := whatsapp.GetTemplatePayload(templating)
msg := test.NewMockMsg(1, "87995844-2017-4ba0-bc73-f3da75b32f9b", nil, "tel:+1234567890", "hi", nil).WithTemplating(templating)
actual := whatsapp.GetTemplatePayload(msg.Templating())

assert.Equal(t, tc.expected, actual)
}
Expand Down
10 changes: 6 additions & 4 deletions handlers/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
Expand All @@ -21,6 +20,7 @@
"github.com/nyaruka/courier/test"
"github.com/nyaruka/gocommon/httpx"
"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/gocommon/urns"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -301,9 +301,9 @@
MsgQuickReplies []string
MsgLocale i18n.Locale
MsgTopic string
MsgTemplating string
MsgHighPriority bool
MsgResponseToExternalID string
MsgMetadata json.RawMessage
MsgFlow *courier.FlowReference
MsgOptIn *courier.OptInReference
MsgUserID courier.UserID
Expand Down Expand Up @@ -337,8 +337,10 @@
if tc.MsgURNAuth != "" {
m.WithURNAuth(tc.MsgURNAuth)
}
if len(tc.MsgMetadata) > 0 {
m.WithMetadata(tc.MsgMetadata)
if tc.MsgTemplating != "" {
templating := &courier.Templating{}
jsonx.MustUnmarshal([]byte(tc.MsgTemplating), templating)
m.WithTemplating(templating)

Check warning on line 343 in handlers/test.go

View check run for this annotation

Codecov / codecov/patch

handlers/test.go#L340-L343

Added lines #L340 - L343 were not covered by tests
}
if tc.MsgFlow != nil {
m.WithFlow(tc.MsgFlow)
Expand Down
Loading
Loading