Skip to content

Commit b73af61

Browse files
committed
Actually implement quick_replies on reply actions
1 parent 16d30c4 commit b73af61

File tree

6 files changed

+56
-29
lines changed

6 files changed

+56
-29
lines changed

cmd/flowrunner/testdata/flows/all_actions.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@
118118
{
119119
"uuid": "62a30ab4-d73c-447d-a989-39c49115153e",
120120
"type": "reply",
121-
"text": "This is a reply with attachments",
122-
"attachments": ["image/jpeg:http://s3.amazon.com/bucket/test_en.jpg?a=@contact.fields.state"]
121+
"text": "This is a reply with attachments and quick replies",
122+
"attachments": ["image/jpeg:http://s3.amazon.com/bucket/test_en.jpg?a=@contact.fields.state"],
123+
"quick_replies": ["Yes", "No"]
123124
},
124125
{
125126
"uuid": "5508e6a7-26ce-4b3b-b32e-bb4e2e614f5d",

cmd/flowrunner/testdata/flows/all_actions_test.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,11 @@
259259
}
260260
],
261261
"created_on": "2000-01-01T00:00:00.000000000-00:00",
262-
"text": "This is a reply with attachments",
262+
"quick_replies": [
263+
"Yes",
264+
"No"
265+
],
266+
"text": "This is a reply with attachments and quick replies",
263267
"type": "send_msg"
264268
},
265269
"step_uuid": ""
@@ -582,7 +586,11 @@
582586
}
583587
],
584588
"created_on": "2000-01-01T00:00:00.000000000-00:00",
585-
"text": "This is a reply with attachments",
589+
"quick_replies": [
590+
"Yes",
591+
"No"
592+
],
593+
"text": "This is a reply with attachments and quick replies",
586594
"type": "send_msg"
587595
},
588596
{

flows/actions/base.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (a *BaseAction) resolveLabels(run flows.FlowRun, step flows.Step, reference
143143
}
144144

145145
// helper function for actions that send a message (text + attachments) that must be localized and evalulated
146-
func (a *BaseAction) evaluateMessage(run flows.FlowRun, step flows.Step, actionText string, actionAttachments []string, log flows.EventLog) (string, []string) {
146+
func (a *BaseAction) evaluateMessage(run flows.FlowRun, step flows.Step, actionText string, actionAttachments []string, actionQuickReplies []string, log flows.EventLog) (string, []string, []string) {
147147
// localize and evaluate the message text
148148
localizedText := run.GetText(flows.UUID(a.UUID()), "text", actionText)
149149
evaluatedText, err := excellent.EvaluateTemplateAsString(run.Environment(), run.Context(), localizedText)
@@ -165,7 +165,21 @@ func (a *BaseAction) evaluateMessage(run flows.FlowRun, step flows.Step, actionT
165165
evaluatedAttachments = append(evaluatedAttachments, evaluatedAttachment)
166166
}
167167

168-
return evaluatedText, evaluatedAttachments
168+
// localize and evaluate the quick replies
169+
translatedQuickReplies := run.GetTextArray(flows.UUID(a.UUID()), "quick_replies", actionQuickReplies)
170+
evaluatedQuickReplies := make([]string, 0, len(translatedQuickReplies))
171+
for n := range translatedQuickReplies {
172+
evaluatedQuickReply, err := excellent.EvaluateTemplateAsString(run.Environment(), run.Context(), translatedQuickReplies[n])
173+
if err != nil {
174+
log.Add(events.NewErrorEvent(err))
175+
} else if evaluatedQuickReply == "" {
176+
log.Add(events.NewErrorEvent(fmt.Errorf("quick reply text evaluated to empty string, skipping")))
177+
continue
178+
}
179+
evaluatedQuickReplies = append(evaluatedQuickReplies, evaluatedQuickReply)
180+
}
181+
182+
return evaluatedText, evaluatedAttachments, evaluatedQuickReplies
169183
}
170184

171185
func (a *BaseAction) resolveContactsAndGroups(run flows.FlowRun, step flows.Step, actionURNs []urns.URN, actionContacts []*flows.ContactReference, actionGroups []*flows.GroupReference, actionLegacyVars []string, log flows.EventLog) ([]urns.URN, []*flows.ContactReference, []*flows.GroupReference, error) {

flows/actions/reply.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ func (a *ReplyAction) Validate(assets flows.SessionAssets) error {
4141

4242
// Execute runs this action
4343
func (a *ReplyAction) Execute(run flows.FlowRun, step flows.Step, log flows.EventLog) error {
44-
evaluatedText, evaluatedAttachments := a.evaluateMessage(run, step, a.Text, a.Attachments, log)
44+
evaluatedText, evaluatedAttachments, evaluatedQuickReplies := a.evaluateMessage(run, step, a.Text, a.Attachments, a.QuickReplies, log)
4545

4646
urns := run.Contact().URNs()
4747

4848
if a.AllURNs && len(urns) > 0 {
49-
log.Add(events.NewSendMsgEvent(evaluatedText, evaluatedAttachments, urns, nil, nil))
49+
log.Add(events.NewSendMsgEvent(evaluatedText, evaluatedAttachments, evaluatedQuickReplies, urns, nil, nil))
5050
} else {
51-
log.Add(events.NewSendMsgToContactEvent(evaluatedText, evaluatedAttachments, run.Contact().Reference()))
51+
log.Add(events.NewSendMsgToContactEvent(evaluatedText, evaluatedAttachments, evaluatedQuickReplies, run.Contact().Reference()))
5252
}
5353

5454
return nil

flows/actions/send_msg.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ const TypeSendMsg string = "send_msg"
2727
// @action send_msg
2828
type SendMsgAction struct {
2929
BaseAction
30-
Text string `json:"text"`
31-
Attachments []string `json:"attachments"`
32-
URNs []urns.URN `json:"urns,omitempty"`
33-
Contacts []*flows.ContactReference `json:"contacts,omitempty" validate:"dive"`
34-
Groups []*flows.GroupReference `json:"groups,omitempty" validate:"dive"`
35-
LegacyVars []string `json:"legacy_vars,omitempty"`
30+
Text string `json:"text"`
31+
Attachments []string `json:"attachments"`
32+
QuickReplies []string `json:"quick_replies,omitempty"`
33+
URNs []urns.URN `json:"urns,omitempty"`
34+
Contacts []*flows.ContactReference `json:"contacts,omitempty" validate:"dive"`
35+
Groups []*flows.GroupReference `json:"groups,omitempty" validate:"dive"`
36+
LegacyVars []string `json:"legacy_vars,omitempty"`
3637
}
3738

3839
// Type returns the type of this action
@@ -45,14 +46,14 @@ func (a *SendMsgAction) Validate(assets flows.SessionAssets) error {
4546

4647
// Execute runs this action
4748
func (a *SendMsgAction) Execute(run flows.FlowRun, step flows.Step, log flows.EventLog) error {
48-
evaluatedText, evaluatedAttachments := a.evaluateMessage(run, step, a.Text, a.Attachments, log)
49+
evaluatedText, evaluatedAttachments, evaluatedQuickReplies := a.evaluateMessage(run, step, a.Text, a.Attachments, a.QuickReplies, log)
4950

5051
urnList, contactRefs, groupRefs, err := a.resolveContactsAndGroups(run, step, a.URNs, a.Contacts, a.Groups, a.LegacyVars, log)
5152
if err != nil {
5253
return err
5354
}
5455

55-
log.Add(events.NewSendMsgEvent(evaluatedText, evaluatedAttachments, urnList, contactRefs, groupRefs))
56+
log.Add(events.NewSendMsgEvent(evaluatedText, evaluatedAttachments, evaluatedQuickReplies, urnList, contactRefs, groupRefs))
5657

5758
return nil
5859
}

flows/events/send_msg.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const TypeSendMsg string = "send_msg"
1616
// "created_on": "2006-01-02T15:04:05Z",
1717
// "text": "hi, what's up",
1818
// "attachments": [],
19+
// "quick_replies": ["Doing Fine", "Got 99 problems"],
1920
// "urns": ["tel:+12065551212"],
2021
// "contacts": [{"uuid": "0e06f977-cbb7-475f-9d0b-a0c4aaec7f6a", "name": "Bob"}]
2122
// }
@@ -33,25 +34,27 @@ type SendMsgEvent struct {
3334
}
3435

3536
// NewSendMsgToContactEvent creates a new outgoing msg event to a single contact
36-
func NewSendMsgToContactEvent(text string, attachments []string, contact *flows.ContactReference) *SendMsgEvent {
37+
func NewSendMsgToContactEvent(text string, attachments []string, quickReples []string, contact *flows.ContactReference) *SendMsgEvent {
3738
event := SendMsgEvent{
38-
BaseEvent: NewBaseEvent(),
39-
Text: text,
40-
Attachments: attachments,
41-
Contacts: []*flows.ContactReference{contact},
39+
BaseEvent: NewBaseEvent(),
40+
Text: text,
41+
Attachments: attachments,
42+
QuickReplies: quickReples,
43+
Contacts: []*flows.ContactReference{contact},
4244
}
4345
return &event
4446
}
4547

4648
// NewSendMsgEvent creates a new outgoing msg event for the given recipients
47-
func NewSendMsgEvent(text string, attachments []string, urns []urns.URN, contacts []*flows.ContactReference, groups []*flows.GroupReference) *SendMsgEvent {
49+
func NewSendMsgEvent(text string, attachments []string, quickReples []string, urns []urns.URN, contacts []*flows.ContactReference, groups []*flows.GroupReference) *SendMsgEvent {
4850
event := SendMsgEvent{
49-
BaseEvent: NewBaseEvent(),
50-
Text: text,
51-
Attachments: attachments,
52-
URNs: urns,
53-
Contacts: contacts,
54-
Groups: groups,
51+
BaseEvent: NewBaseEvent(),
52+
Text: text,
53+
Attachments: attachments,
54+
QuickReplies: quickReples,
55+
URNs: urns,
56+
Contacts: contacts,
57+
Groups: groups,
5558
}
5659
return &event
5760
}

0 commit comments

Comments
 (0)