Skip to content

Commit

Permalink
Add metadata in trigger.params
Browse files Browse the repository at this point in the history
  • Loading branch information
Robi9 committed Jun 15, 2023
1 parent c2ba171 commit e8cc43d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
47 changes: 45 additions & 2 deletions core/tasks/handler/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,50 @@ func handleMsgEvent(ctx context.Context, rt *runtime.Runtime, event *MsgEvent) e
}

// otherwise build the trigger and start the flow directly
trigger := triggers.NewBuilder(oa.Env(), flow.FlowReference(), contact).Msg(msgIn).WithMatch(trigger.Match()).Build()
_, err = runner.StartFlowForContacts(ctx, rt, oa, flow, []flows.Trigger{trigger}, flowMsgHook, true)
trigger := triggers.NewBuilder(oa.Env(), flow.FlowReference(), contact).Msg(msgIn).WithMatch(trigger.Match())

if event.Metadata != nil {

var params *types.XObject
metadata := struct {
Headline string `json:"headline"`
Body string `json:"body"`
SourceType string `json:"source_type"`
SourceID string `json:"source_id"`
SourceURL string `json:"source_url"`
Image struct {
Caption string `json:"caption"`
Filename string `json:"filename"`
ID string `json:"id"`
Mimetype string `json:"mime_type"`
SHA256 string `json:"sha256"`
} `json:"image,omitempty"`
Video struct {
Caption string `json:"caption"`
Filename string `json:"filename"`
ID string `json:"id"`
Mimetype string `json:"mime_type"`
SHA256 string `json:"sha256"`
} `json:"video,omitempty"`
}{}
err := json.Unmarshal([]byte(event.Metadata), &metadata)
if err != nil {
log.WithError(err).Error("unable to unmarshal metadata from msg event")
}
asJSON, err := json.Marshal(metadata)
if err != nil {
log.WithError(err).Error("unable to marshal metadata from msg event")
}
params, err = types.ReadXObject(asJSON)
if err != nil {
log.WithError(err).Error("unable to marshal metadata from msg event")
}

trigger.WithParams(params)
}
triggerBuild := trigger.Build()

_, err = runner.StartFlowForContacts(ctx, rt, oa, flow, []flows.Trigger{triggerBuild}, flowMsgHook, true)
if err != nil {
return errors.Wrapf(err, "error starting flow for contact")
}
Expand Down Expand Up @@ -828,6 +870,7 @@ type MsgEvent struct {
Attachments []utils.Attachment `json:"attachments"`
NewContact bool `json:"new_contact"`
CreatedOn time.Time `json:"created_on"`
Metadata json.RawMessage `json:"metadata"`
}

type StopEvent struct {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ go 1.17

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

replace github.com/nyaruka/goflow => github.com/Ilhasoft/goflow v0.0.3-goflow-0.144.3
replace github.com/nyaruka/goflow => github.com/Ilhasoft/goflow v0.0.3-goflow-0.144.3-dev
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20O
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/Ilhasoft/gocommon v1.16.2-weni h1:IDDxPVNIVDMwSErQmTrAiziLMvEi6rbeRb3GG8D+XmA=
github.com/Ilhasoft/gocommon v1.16.2-weni/go.mod h1:pk8L9T79VoKO8OWTiZbtUutFPI3sGGKB5u8nNWDKuGE=
github.com/Ilhasoft/goflow v0.0.3-goflow-0.144.3 h1:L+HZFwumaV2ms0Wvi8fMbx6KnVjK5uRhR8Df/td05bc=
github.com/Ilhasoft/goflow v0.0.3-goflow-0.144.3/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c=
github.com/Ilhasoft/goflow v0.0.3-goflow-0.144.3-dev h1:zYClwTBrKcNKLnSoc0NP6yyhn3egVb1yfM8cFucTRbc=
github.com/Ilhasoft/goflow v0.0.3-goflow-0.144.3-dev/go.mod h1:o0xaVWP9qNcauBSlcNLa79Fm2oCPV+BDpheFRa/D40c=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down

0 comments on commit e8cc43d

Please sign in to comment.