Skip to content

Commit

Permalink
Merge pull request #192 from weni-ai/feat/wpp-broadcast-channel
Browse files Browse the repository at this point in the history
feat: add channel option to whatsapp broadcasts
  • Loading branch information
paulobernardoaf authored Nov 27, 2024
2 parents 8c6871e + dc8a11e commit d970766
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
12 changes: 11 additions & 1 deletion core/models/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,7 @@ type WppBroadcast struct {
OrgID OrgID `json:"org_id" db:"org_id"`
ParentID BroadcastID `json:"parent_id,omitempty" db:"parent_id"`
Msg WppBroadcastMessage `json:"msg"`
ChannelID ChannelID `json:"channel_id,omitempty"`
}
}

Expand All @@ -1479,18 +1480,20 @@ func (b *WppBroadcast) ContactIDs() []ContactID { return b.b.ContactIDs }
func (b *WppBroadcast) GroupIDs() []GroupID { return b.b.GroupIDs }
func (b *WppBroadcast) URNs() []urns.URN { return b.b.URNs }
func (b *WppBroadcast) Msg() WppBroadcastMessage { return b.b.Msg }
func (b *WppBroadcast) ChannelID() ChannelID { return b.b.ChannelID }

func (b *WppBroadcast) MarshalJSON() ([]byte, error) { return json.Marshal(b.b) }
func (b *WppBroadcast) UnmarshalJSON(data []byte) error { return json.Unmarshal(data, &b.b) }

func NewWppBroadcast(orgID OrgID, id BroadcastID, msg WppBroadcastMessage, urns []urns.URN, contactIDs []ContactID, groupIDs []GroupID) *WppBroadcast {
func NewWppBroadcast(orgID OrgID, id BroadcastID, msg WppBroadcastMessage, urns []urns.URN, contactIDs []ContactID, groupIDs []GroupID, channelID ChannelID) *WppBroadcast {
bcast := &WppBroadcast{}
bcast.b.OrgID = orgID
bcast.b.BroadcastID = id
bcast.b.Msg = msg
bcast.b.URNs = urns
bcast.b.ContactIDs = contactIDs
bcast.b.GroupIDs = groupIDs
bcast.b.ChannelID = channelID

return bcast
}
Expand All @@ -1500,6 +1503,7 @@ func (b *WppBroadcast) CreateBatch(contactIDs []ContactID) *WppBroadcastBatch {
batch.b.BroadcastID = b.b.BroadcastID
batch.b.Msg = b.b.Msg
batch.b.OrgID = b.b.OrgID
batch.b.ChannelID = b.b.ChannelID
batch.b.ContactIDs = contactIDs
return batch
}
Expand All @@ -1512,6 +1516,7 @@ type WppBroadcastBatch struct {
ContactIDs []ContactID `json:"contact_ids,omitempty"`
IsLast bool `json:"is_last"`
OrgID OrgID `json:"org_id"`
ChannelID ChannelID `json:"channel_id,omitempty"`
}
}

Expand All @@ -1521,6 +1526,7 @@ func (b *WppBroadcastBatch) URNs() map[ContactID]urns.URN { return b.b.UR
func (b *WppBroadcastBatch) SetURNs(urns map[ContactID]urns.URN) { b.b.URNs = urns }
func (b *WppBroadcastBatch) OrgID() OrgID { return b.b.OrgID }
func (b *WppBroadcastBatch) Msg() WppBroadcastMessage { return b.b.Msg }
func (b *WppBroadcastBatch) ChannelID() ChannelID { return b.b.ChannelID }

func (b *WppBroadcastBatch) IsLast() bool { return b.b.IsLast }
func (b *WppBroadcastBatch) SetIsLast(last bool) { b.b.IsLast = last }
Expand Down Expand Up @@ -1602,6 +1608,10 @@ func CreateWppBroadcastMessages(ctx context.Context, rt *runtime.Runtime, oa *Or
}
}

if bcast.ChannelID() != NilChannelID {
channel = oa.ChannelByID(bcast.ChannelID())
}

// no urn and channel? move on
if channel == nil {
return nil, nil
Expand Down
32 changes: 30 additions & 2 deletions core/tasks/msgs/send_wpp_broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestWppBroadcastTask(t *testing.T) {
// add an extra URN for cathy
testdata.InsertContactURN(db, testdata.Org1, testdata.Cathy, urns.URN("tel:+12065551212"), 1001)

// change alexandrias URN to a twitter URN and set her language to eng so that a template gets used for her
// change alexandrias URN to a whatsapp URN and set her language to eng so that a template gets used for her
db.MustExec(`UPDATE contacts_contacturn SET identity = 'whatsapp:559899999999', path='559899999999', scheme='whatsapp' WHERE contact_id = $1`, testdata.Alexandria.ID)
db.MustExec(`UPDATE contacts_contact SET language='eng' WHERE id = $1`, testdata.Alexandria.ID)

Expand Down Expand Up @@ -149,6 +149,7 @@ func TestWppBroadcastTask(t *testing.T) {
MsgCount int
Msg models.WppBroadcastMessage
MsgText string
ChannelID models.ChannelID
}{
{
models.NilBroadcastID,
Expand All @@ -160,6 +161,7 @@ func TestWppBroadcastTask(t *testing.T) {
121,
baseMsg,
"hello world",
models.NilChannelID,
},
{
existingID,
Expand All @@ -171,6 +173,7 @@ func TestWppBroadcastTask(t *testing.T) {
1,
evaluationMsg,
"hello Cathy",
models.NilChannelID,
},
{
models.NilBroadcastID,
Expand All @@ -182,6 +185,19 @@ func TestWppBroadcastTask(t *testing.T) {
1,
evaluationMsg,
"hello Cathy",
testdata.WhatsAppCloudChannel.ID,
},
{
models.NilBroadcastID,
nil,
cathyOnly,
nil,
queue.HandlerQueue,
1,
1,
evaluationMsg,
"hello Cathy",
models.NilChannelID,
},
{
models.NilBroadcastID,
Expand All @@ -193,6 +209,7 @@ func TestWppBroadcastTask(t *testing.T) {
1,
replyMsg,
"hello Cathy, how are you doing today?",
models.NilChannelID,
},
{
models.NilBroadcastID,
Expand All @@ -204,6 +221,7 @@ func TestWppBroadcastTask(t *testing.T) {
1,
listMsg,
"hello Cathy",
models.NilChannelID,
},
{
models.NilBroadcastID,
Expand All @@ -215,6 +233,7 @@ func TestWppBroadcastTask(t *testing.T) {
1,
ctaMsg,
"hello Cathy",
models.NilChannelID,
},
{
models.NilBroadcastID,
Expand All @@ -226,6 +245,7 @@ func TestWppBroadcastTask(t *testing.T) {
1,
flowMsg,
"hello Cathy",
models.NilChannelID,
},
{
models.NilBroadcastID,
Expand All @@ -237,6 +257,7 @@ func TestWppBroadcastTask(t *testing.T) {
1,
orderDetailsMsg,
"hello Cathy",
models.NilChannelID,
},
{
models.NilBroadcastID,
Expand All @@ -250,6 +271,7 @@ func TestWppBroadcastTask(t *testing.T) {
1,
templateMsg,
"Welcome Alexandia!",
models.NilChannelID,
},
}

Expand All @@ -258,7 +280,7 @@ func TestWppBroadcastTask(t *testing.T) {

for i, tc := range tcs {
// handle our start task
bcast := models.NewWppBroadcast(oa.OrgID(), tc.BroadcastID, tc.Msg, tc.URNs, tc.ContactIDs, tc.GroupIDs)
bcast := models.NewWppBroadcast(oa.OrgID(), tc.BroadcastID, tc.Msg, tc.URNs, tc.ContactIDs, tc.GroupIDs, tc.ChannelID)
err = msgs.CreateWppBroadcastBatches(ctx, rt, bcast)
assert.NoError(t, err)

Expand Down Expand Up @@ -346,6 +368,12 @@ func TestWppBroadcastTask(t *testing.T) {
).Returns(1, "%d: unexpected template count", i)
}

// assert our channel is being set
if tc.ChannelID != models.NilChannelID {
testsuite.AssertQuery(t, db, `SELECT count(*) FROM msgs_msg WHERE org_id = 1 AND created_on > $1 AND text = $2 AND channel_id = $3`, lastNow, tc.MsgText, tc.ChannelID).
Returns(1, "%d: unexpected channel count", i)
}

// make sure our broadcast is marked as sent
if tc.BroadcastID != models.NilBroadcastID {
testsuite.AssertQuery(t, db, `SELECT count(*) FROM msgs_broadcast WHERE id = $1 AND status = 'S'`, tc.BroadcastID).
Expand Down

0 comments on commit d970766

Please sign in to comment.