-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add send_optin action and optin_sent event
- Loading branch information
1 parent
caa8b8b
commit a7f63f8
Showing
9 changed files
with
208 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package actions | ||
|
||
import ( | ||
"github.com/nyaruka/goflow/assets" | ||
"github.com/nyaruka/goflow/flows" | ||
"github.com/nyaruka/goflow/flows/events" | ||
) | ||
|
||
func init() { | ||
registerType(TypeSendOptIn, func() flows.Action { return &SendOptInAction{} }) | ||
} | ||
|
||
// TypeSendOptIn is the type for the send optin action | ||
const TypeSendOptIn string = "send_optin" | ||
|
||
// SendOptInAction can be used to send an optin to the contact if the channel supports that. | ||
// | ||
// An [event:optin_sent] event will be created if the optin was sent. | ||
// | ||
// { | ||
// "uuid": "8eebd020-1af5-431c-b943-aa670fc74da9", | ||
// "type": "send_optin", | ||
// "optin": { | ||
// "uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb", | ||
// "name": "Joke Of The Day" | ||
// } | ||
// } | ||
// | ||
// @action send_optin | ||
type SendOptInAction struct { | ||
baseAction | ||
onlineAction | ||
|
||
OptIn *assets.OptInReference `json:"optin" validate:"required,dive"` | ||
} | ||
|
||
// NewSendOptIn creates a new send optin action | ||
func NewSendOptIn(uuid flows.ActionUUID, optIn *assets.OptInReference) *SendOptInAction { | ||
return &SendOptInAction{ | ||
baseAction: newBaseAction(TypeSendOptIn, uuid), | ||
OptIn: optIn, | ||
} | ||
} | ||
|
||
// Execute creates the optin events | ||
func (a *SendOptInAction) Execute(run flows.Run, step flows.Step, logModifier flows.ModifierCallback, logEvent flows.EventCallback) error { | ||
optIn := run.Session().Assets().OptIns().Get(a.OptIn.UUID) | ||
destinations := run.Contact().ResolveDestinations(false) | ||
|
||
if len(destinations) > 0 && destinations[0].Channel.HasFeature(assets.ChannelFeatureOptIns) { | ||
logEvent(events.NewOptInSent(optIn)) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
[ | ||
{ | ||
"description": "NOOP when channel doesn't support optins feature", | ||
"action": { | ||
"type": "send_optin", | ||
"uuid": "ad154980-7bf7-4ab8-8728-545fd6378912", | ||
"optin": { | ||
"uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb", | ||
"name": "Joke Of The Day" | ||
} | ||
}, | ||
"events": [] | ||
}, | ||
{ | ||
"description": "Event created when channel does support optins feature", | ||
"contact": { | ||
"uuid": "5d76d86b-3bb9-4d5a-b822-c9d86f5d8e4f", | ||
"name": "Ryan Lewis", | ||
"language": "eng", | ||
"timezone": "America/Guayaquil", | ||
"urns": [ | ||
"facebook:1234567890" | ||
], | ||
"groups": [], | ||
"fields": {}, | ||
"created_on": "2018-06-20T11:40:30.123456789-00:00" | ||
}, | ||
"action": { | ||
"type": "send_optin", | ||
"uuid": "ad154980-7bf7-4ab8-8728-545fd6378912", | ||
"optin": { | ||
"uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb", | ||
"name": "Joke Of The Day" | ||
} | ||
}, | ||
"events": [ | ||
{ | ||
"type": "optin_sent", | ||
"created_on": "2018-10-18T14:20:30.000123456Z", | ||
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c", | ||
"optin": { | ||
"name": "Joke Of The Day", | ||
"uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb" | ||
} | ||
} | ||
] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package events | ||
|
||
import ( | ||
"github.com/nyaruka/goflow/assets" | ||
"github.com/nyaruka/goflow/flows" | ||
) | ||
|
||
func init() { | ||
registerType(TypeOptInSent, func() flows.Event { return &OptInSentEvent{} }) | ||
} | ||
|
||
// TypeOptInSent is our type for the optin event | ||
const TypeOptInSent string = "optin_sent" | ||
|
||
// OptInSentEvent events are created when an action has sent an optin. | ||
// | ||
// { | ||
// "type": "optin_sent", | ||
// "created_on": "2006-01-02T15:04:05Z", | ||
// "optin": { | ||
// "uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb", | ||
// "name": "Joke Of The Day" | ||
// } | ||
// } | ||
// | ||
// @event optin_sent | ||
type OptInSentEvent struct { | ||
BaseEvent | ||
|
||
OptIn *assets.OptInReference `json:"optin" validate:"required,dive"` | ||
} | ||
|
||
// NewOptInSent returns a new optin sent event | ||
func NewOptInSent(optIn *flows.OptIn) *OptInSentEvent { | ||
return &OptInSentEvent{ | ||
BaseEvent: NewBaseEvent(TypeOptInSent), | ||
OptIn: optIn.Reference(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters