Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: WhatsApp flows attachments #88

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions flows/actions/send_wpp_message.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -129,7 +133,25 @@ 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)
continue
}
base64Data := base64.StdEncoding.EncodeToString(trace.ResponseBody)
evaluatedFlowData[k] = base64Data
} else {
evaluatedFlowData[k] = evaluatedValue
}
}
}

Expand Down
17 changes: 15 additions & 2 deletions flows/actions/testdata/send_whatsapp_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {},
Expand Down Expand Up @@ -169,7 +181,8 @@
],
"extra": {
"foo": "bar"
}
},
"image": "ZmFrZSBpbWFnZSBkYXRh"
},
"flow_cta": "Button text"
}
Expand Down
Loading