From e8cc43d6e29055faca20fc7257e272a66c6576e3 Mon Sep 17 00:00:00 2001 From: Robi9 Date: Thu, 15 Jun 2023 17:02:17 -0300 Subject: [PATCH] Add metadata in trigger.params --- core/tasks/handler/worker.go | 47 ++++++++++++++++++++++++++++++++++-- go.mod | 2 +- go.sum | 4 +-- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/core/tasks/handler/worker.go b/core/tasks/handler/worker.go index f4da8fbb2..64bc3d003 100644 --- a/core/tasks/handler/worker.go +++ b/core/tasks/handler/worker.go @@ -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") } @@ -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 { diff --git a/go.mod b/go.mod index 281f61055..1f4110396 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 5b3aa9021..dbf4ee0c8 100644 --- a/go.sum +++ b/go.sum @@ -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=