From 937381c7aaa703af2da20aa1f9412fdcbd6bda10 Mon Sep 17 00:00:00 2001 From: Paulo Bernardo Date: Thu, 10 Oct 2024 14:40:33 -0300 Subject: [PATCH 1/3] feat: get attachment base64 on wpp flow value --- flows/actions/send_wpp_message.go | 51 ++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/flows/actions/send_wpp_message.go b/flows/actions/send_wpp_message.go index 9052c4dee..3a1d15e05 100644 --- a/flows/actions/send_wpp_message.go +++ b/flows/actions/send_wpp_message.go @@ -1,8 +1,11 @@ package actions import ( + "encoding/base64" "encoding/json" + "net/http" + "github.com/nyaruka/gocommon/httpx" "github.com/nyaruka/gocommon/urns" "github.com/nyaruka/goflow/assets" "github.com/nyaruka/goflow/flows" @@ -26,20 +29,21 @@ type SendWppMsgAction struct { } type createWppMsgAction struct { - HeaderType string `json:"header_type,omitempty"` - HeaderText string `json:"header_text,omitempty"` - Attachment string `json:"attachment,omitempty"` - Text string `json:"text,omitempty"` - Footer string `json:"footer,omitempty"` - ListItems []flows.ListItems `json:"list_items,omitempty"` - ButtonText string `json:"button_text,omitempty"` - QuickReplies []string `json:"quick_replies,omitempty"` - InteractionType string `json:"interaction_type,omitempty"` - ActionURL string `json:"action_url,omitempty"` - FlowID string `json:"flow_id,omitempty"` - FlowData flows.FlowData `json:"flow_data,omitempty"` - FlowScreen string `json:"flow_screen,omitempty"` - FlowMode string `json:"flow_mode,omitempty"` + HeaderType string `json:"header_type,omitempty"` + HeaderText string `json:"header_text,omitempty"` + Attachment string `json:"attachment,omitempty"` + Text string `json:"text,omitempty"` + Footer string `json:"footer,omitempty"` + ListItems []flows.ListItems `json:"list_items,omitempty"` + ButtonText string `json:"button_text,omitempty"` + QuickReplies []string `json:"quick_replies,omitempty"` + InteractionType string `json:"interaction_type,omitempty"` + ActionURL string `json:"action_url,omitempty"` + FlowID string `json:"flow_id,omitempty"` + FlowData flows.FlowData `json:"flow_data,omitempty"` + FlowScreen string `json:"flow_screen,omitempty"` + FlowMode string `json:"flow_mode,omitempty"` + FlowDataAttachmentNameMap map[string]string `json:"flow_data_attachment_name_map,omitempty"` } type Header struct { @@ -129,7 +133,24 @@ func (a *SendWppMsgAction) Execute(run flows.FlowRun, step flows.Step, logModifi if err == nil { evaluatedFlowData[k] = jsonValue } else { - evaluatedFlowData[k] = evaluatedValue + // check if the evaluated value is an attachment + if _, ok := a.FlowDataAttachmentNameMap[k]; ok { + // if the attachment is found, fetch it's content and save the base64 encoded content + client := &http.Client{} + req, err := http.NewRequest("GET", evaluatedValue, nil) + if err != nil { + run.LogError(step, err) + continue + } + trace, err := httpx.DoTrace(client, req, nil, nil, -1) + if err != nil { + run.LogError(step, err) + } + base64Data := base64.StdEncoding.EncodeToString(trace.ResponseBody) + evaluatedFlowData[k] = base64Data + } else { + evaluatedFlowData[k] = evaluatedValue + } } } From 001ad9ae6f9fa1bc557f19d70a946df1dd878acf Mon Sep 17 00:00:00 2001 From: Paulo Bernardo Date: Thu, 10 Oct 2024 14:51:39 -0300 Subject: [PATCH 2/3] feat: test wpp flow attachment base64 conversion --- flows/actions/testdata/send_whatsapp_msg.json | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/flows/actions/testdata/send_whatsapp_msg.json b/flows/actions/testdata/send_whatsapp_msg.json index b19a4fe0e..e2a0bcff7 100644 --- a/flows/actions/testdata/send_whatsapp_msg.json +++ b/flows/actions/testdata/send_whatsapp_msg.json @@ -123,6 +123,14 @@ }, { "description": "WhatsApp Flows message", + "http_mocks": { + "https://example.com/test.jpg": [ + { + "status": 200, + "body": "fake image data" + } + ] + }, "action": { "type": "send_whatsapp_msg", "uuid": "c67969d6-f400-4b5e-bb7c-e45115cd3aa4", @@ -136,7 +144,11 @@ "flow_data": { "name": "John Doe", "options": "[\"option 1\",\"option 2\"]", - "extra": "{\"foo\":\"bar\"}" + "extra": "{\"foo\":\"bar\"}", + "image": "https://example.com/test.jpg" + }, + "flow_data_attachment_name_map": { + "image": "test.jpg" } }, "localization": {}, @@ -169,7 +181,8 @@ ], "extra": { "foo": "bar" - } + }, + "image": "ZmFrZSBpbWFnZSBkYXRh" }, "flow_cta": "Button text" } From 5b6f8882d0e77fb2c9e8739c9407f731c6a23a5c Mon Sep 17 00:00:00 2001 From: Paulo Bernardo Date: Thu, 10 Oct 2024 14:55:44 -0300 Subject: [PATCH 3/3] feat: skip attachment if request error occurs --- flows/actions/send_wpp_message.go | 1 + 1 file changed, 1 insertion(+) diff --git a/flows/actions/send_wpp_message.go b/flows/actions/send_wpp_message.go index 3a1d15e05..5736e10c0 100644 --- a/flows/actions/send_wpp_message.go +++ b/flows/actions/send_wpp_message.go @@ -145,6 +145,7 @@ func (a *SendWppMsgAction) Execute(run flows.FlowRun, step flows.Step, logModifi trace, err := httpx.DoTrace(client, req, nil, nil, -1) if err != nil { run.LogError(step, err) + continue } base64Data := base64.StdEncoding.EncodeToString(trace.ResponseBody) evaluatedFlowData[k] = base64Data