Skip to content

Commit

Permalink
respect flow channels, if defined.
Browse files Browse the repository at this point in the history
  • Loading branch information
baracudda committed Feb 28, 2024
1 parent a35fbae commit 3dc882a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
24 changes: 23 additions & 1 deletion flows/actions/send_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"math/rand"
)

func init() {
Expand Down Expand Up @@ -83,7 +84,16 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow

evaluatedText, evaluatedAttachments, evaluatedQuickReplies := a.evaluateMessage(run, nil, a.Text, a.Attachments, a.QuickReplies, logEvent)

destinations := run.Contact().ResolveDestinations(a.AllURNs)
var destinations []flows.Destination
//<*((==<
flowChannels := run.Flow().Channels()
if len(flowChannels) > 0 {
destinations = []flows.Destination{}
// just add one "null" entry
destinations = append(destinations, flows.Destination{URN: nil, Channel: nil})
} else {
destinations = run.Contact().ResolveDestinations(a.AllURNs)
}

sa := run.Session().Assets()

Expand All @@ -92,6 +102,18 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow
var channelRef *assets.ChannelReference
if dest.Channel != nil {
channelRef = assets.NewChannelReference(dest.Channel.UUID(), dest.Channel.Name())
} else if dest.URN == nil {
//<*((==<
channelRef = flowChannels[rand.Intn(len(flowChannels))]
//pick URN from contact based on channel we have
dest.URN = run.Contact().ResolveURN(channelRef.UUID)
if dest.URN == nil {
logEvent(events.NewErrorf("no matching URN for contact [%s] with channel [%s]",
run.Contact().UUID(),
channelRef.UUID,
))
return nil
}
}

var templating *flows.MsgTemplating
Expand Down
12 changes: 12 additions & 0 deletions flows/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,18 @@ func (c *Contact) ResolveDestinations(all bool) []Destination {
return destinations
}

//<*((==<
// ResolveURN resolves possible URN given channel to use
func (c *Contact) ResolveURN(aChannelID assets.ChannelUUID) *ContactURN {
for _, u := range c.urns {
channel := c.assets.Channels().Get(aChannelID)
if channel != nil && channel.SupportsScheme(u.URN().Scheme()) {
return u
}
}
return nil
}

// PreferredURN gets the preferred URN for this contact, i.e. the URN we would use for sending
func (c *Contact) PreferredURN() *ContactURN {
destinations := c.ResolveDestinations(false)
Expand Down
15 changes: 13 additions & 2 deletions flows/definition/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type flow struct {
expireAfterMinutes int
localization flows.Localization
nodes []flows.Node
channels []*assets.ChannelReference //<*((==<

// optional properties not used by engine itself
ui json.RawMessage
Expand All @@ -50,7 +51,7 @@ type flow struct {
}

// NewFlow creates a new flow
func NewFlow(uuid assets.FlowUUID, name string, language envs.Language, flowType flows.FlowType, revision int, expireAfterMinutes int, localization flows.Localization, nodes []flows.Node, ui json.RawMessage, a assets.Flow) (flows.Flow, error) {
func NewFlow(uuid assets.FlowUUID, name string, language envs.Language, flowType flows.FlowType, revision int, expireAfterMinutes int, localization flows.Localization, nodes []flows.Node, ui json.RawMessage, a assets.Flow, channels []*assets.ChannelReference) (flows.Flow, error) {
f := &flow{
uuid: uuid,
name: name,
Expand All @@ -64,6 +65,7 @@ func NewFlow(uuid assets.FlowUUID, name string, language envs.Language, flowType
nodeMap: make(map[flows.NodeUUID]flows.Node, len(nodes)),
ui: ui,
asset: a,
channels: channels,
}

for _, node := range f.nodes {
Expand All @@ -88,6 +90,7 @@ func (f *flow) Nodes() []flows.Node { return f.nodes }
func (f *flow) Localization() flows.Localization { return f.localization }
func (f *flow) UI() json.RawMessage { return f.ui }
func (f *flow) GetNode(uuid flows.NodeUUID) flows.Node { return f.nodeMap[uuid] }
func (f *flow) Channels() []*assets.ChannelReference { return f.channels } //<*((==<

func (f *flow) validate() error {
// track UUIDs used by nodes and actions to ensure that they are unique
Expand Down Expand Up @@ -315,6 +318,8 @@ type flowEnvelope struct {
Localization localization `json:"localization"`
Nodes []*node `json:"nodes"`
UI json.RawMessage `json:"_ui,omitempty"`
//<*((==<
ChannelUUIDs []assets.ChannelUUID `json:"channels,omitempty"`
}

// ReadFlow reads a flow definition from the passed in byte array, migrating it to the spec version of the engine if necessary
Expand Down Expand Up @@ -355,7 +360,13 @@ func readFlow(data json.RawMessage, mc *migrations.Config, a assets.Flow) (flows
e.Localization = make(localization)
}

return NewFlow(e.UUID, e.Name, e.Language, e.Type, e.Revision, e.ExpireAfterMinutes, e.Localization, nodes, e.UI, a)
//<*((==<
channels := make([]*assets.ChannelReference, len(e.ChannelUUIDs))
for i := range e.ChannelUUIDs {
channels[i] = assets.NewChannelReference(e.ChannelUUIDs[i], string("Name Of: "+e.ChannelUUIDs[i]))
}

return NewFlow(e.UUID, e.Name, e.Language, e.Type, e.Revision, e.ExpireAfterMinutes, e.Localization, nodes, e.UI, a, channels)
}

// MarshalJSON marshals this flow into JSON
Expand Down
1 change: 1 addition & 0 deletions flows/definition/flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func TestNewFlow(t *testing.T) {
},
nil, // no UI
nil, // no asset
nil,
)
require.NoError(t, err)

Expand Down
1 change: 1 addition & 0 deletions flows/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ type Flow interface {
UI() json.RawMessage
Nodes() []Node
GetNode(uuid NodeUUID) Node
Channels() []*assets.ChannelReference //<*((==<

Asset() assets.Flow
Reference() *assets.FlowReference
Expand Down

0 comments on commit 3dc882a

Please sign in to comment.