Skip to content

Commit

Permalink
Merge pull request #193 from weni-ai/feat/wpp-buttons
Browse files Browse the repository at this point in the history
feat: add buttons support to wpp broadcasts
  • Loading branch information
paulobernardoaf authored Nov 27, 2024
2 parents d970766 + ab37536 commit 4870236
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
22 changes: 21 additions & 1 deletion core/models/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,9 @@ func newOutgoingMsgWpp(rt *runtime.Runtime, org *Org, channel *Channel, contactI
metadata["interaction_type"] = msgWpp.InteractionType()
metadata["order_details_message"] = msgWpp.OrderDetailsMessage()
}
if len(msgWpp.Buttons()) > 0 {
metadata["buttons"] = msgWpp.Buttons()
}
if msgWpp.TextLanguage != "" {
metadata["text_language"] = msgWpp.TextLanguage
}
Expand Down Expand Up @@ -1459,6 +1462,7 @@ type WppBroadcastMessage struct {
FlowMessage flows.FlowMessage `json:"flow_message,omitempty"`
ListMessage flows.ListMessage `json:"list_message,omitempty"`
CTAMessage flows.CTAMessage `json:"cta_message,omitempty"`
Buttons []flows.ButtonComponent `json:"buttons,omitempty"`
}

type WppBroadcast struct {
Expand Down Expand Up @@ -1695,13 +1699,29 @@ func CreateWppBroadcastMessages(ctx context.Context, rt *runtime.Runtime, oa *Or
}
}

// evaluate our buttons
buttons := make([]flows.ButtonComponent, 0)
for _, button := range bcast.Msg().Buttons {
var newButton flows.ButtonComponent
newButton.SubType, _ = excellent.EvaluateTemplate(oa.Env(), evaluationCtx, button.SubType, nil)

for _, param := range button.Parameters {
var newParam flows.ButtonParam
newParam.Type, _ = excellent.EvaluateTemplate(oa.Env(), evaluationCtx, param.Type, nil)
newParam.Text, _ = excellent.EvaluateTemplate(oa.Env(), evaluationCtx, param.Text, nil)
newButton.Parameters = append(newButton.Parameters, newParam)
}

buttons = append(buttons, newButton)
}

// don't do anything if we have no text or attachments
if text == "" && len(attachments) == 0 {
return nil, nil
}

// create our outgoing message
out := flows.NewMsgWppOut(urn, channel.ChannelReference(), bcast.Msg().InteractionType, headerType, headerText, text, footerText, ctaMessage, listMessage, flowMessage, orderDetails, attachments, quickReplies, templating, flows.NilMsgTopic)
out := flows.NewMsgWppOut(urn, channel.ChannelReference(), bcast.Msg().InteractionType, headerType, headerText, text, footerText, ctaMessage, listMessage, flowMessage, orderDetails, attachments, quickReplies, buttons, templating, flows.NilMsgTopic)
msg, err := NewOutgoingWppBroadcastMsg(rt, oa.Org(), channel, c.ID(), out, time.Now(), bcast.BroadcastID())
if err != nil {
return nil, errors.Wrapf(err, "error creating outgoing message")
Expand Down
33 changes: 33 additions & 0 deletions core/tasks/msgs/send_wpp_broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ func TestWppBroadcastTask(t *testing.T) {
},
}

buttonsMsg := models.WppBroadcastMessage{
Text: "hello @contact.name",
Buttons: []flows.ButtonComponent{
{
SubType: "url",
Parameters: []flows.ButtonParam{
{
Type: "text",
Text: "button param text",
},
},
},
},
}

tcs := []struct {
BroadcastID models.BroadcastID
URNs []urns.URN
Expand Down Expand Up @@ -273,6 +288,18 @@ func TestWppBroadcastTask(t *testing.T) {
"Welcome Alexandia!",
models.NilChannelID,
},
{
models.NilBroadcastID,
nil,
cathyOnly,
nil,
queue.HandlerQueue,
1,
1,
buttonsMsg,
"hello Cathy",
models.NilChannelID,
},
}

lastNow := time.Now()
Expand Down Expand Up @@ -359,6 +386,12 @@ func TestWppBroadcastTask(t *testing.T) {
Returns(1, "%d: unexpected order details message count", i)
}

// assert our buttons are being sent
if len(tc.Msg.Buttons) > 0 {
testsuite.AssertQuery(t, db, `SELECT count(*) FROM msgs_msg WHERE org_id = 1 AND created_on > $1 AND text = $2 AND metadata LIKE '%' || 'buttons' || '%'`, lastNow, tc.MsgText).
Returns(1, "%d: unexpected buttons count", i)
}

// assert our template is being sent
if tc.Msg.Template.UUID != "" {
testsuite.AssertQuery(t, db, `SELECT count(*) FROM msgs_msg WHERE org_id = 1 AND created_on > $1 AND text = $2 AND metadata = $3`,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ go 1.17

replace github.com/nyaruka/gocommon => github.com/Ilhasoft/gocommon v1.16.2-weni

replace github.com/nyaruka/goflow => github.com/weni-ai/goflow v1.5.4
replace github.com/nyaruka/goflow => github.com/weni-ai/goflow v1.5.5
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLD
github.com/tj/go-elastic v0.0.0-20171221160941-36157cbbebc2/go.mod h1:WjeM0Oo1eNAjXGDx2yma7uG2XoyRZTq1uv3M/o7imD0=
github.com/tj/go-kinesis v0.0.0-20171128231115-08b17f58cb1b/go.mod h1:/yhzCV0xPfx6jb1bBgRFjl5lytqVqZXEaeqWP8lTEao=
github.com/tj/go-spin v1.1.0/go.mod h1:Mg1mzmePZm4dva8Qz60H2lHwmJ2loum4VIrLgVnKwh4=
github.com/weni-ai/goflow v1.5.4 h1:8s4pwLcOv2mkceWLjcvQn52iOFvAvlssUVxVxezlzSY=
github.com/weni-ai/goflow v1.5.4/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c=
github.com/weni-ai/goflow v1.5.5 h1:nXF70PsHq1+vMWMYtn51NKZbCWyCALrrQR/ofFAqS+0=
github.com/weni-ai/goflow v1.5.5/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down

0 comments on commit 4870236

Please sign in to comment.