From 795547c68e87e05539770b9117318824e63198ca Mon Sep 17 00:00:00 2001 From: Paulo Bernardo Date: Wed, 18 Sep 2024 13:09:04 -0300 Subject: [PATCH 1/2] fix: only send attachments to brain when entry is @input.text --- flows/actions/call_brain.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/flows/actions/call_brain.go b/flows/actions/call_brain.go index 757a5eb60..ce7d3db8e 100644 --- a/flows/actions/call_brain.go +++ b/flows/actions/call_brain.go @@ -43,18 +43,22 @@ func (a *CallBrainAction) Execute(run flows.FlowRun, step flows.Step, logModifie // Execute runs this action func (a *CallBrainAction) call(run flows.FlowRun, step flows.Step, logEvent flows.EventCallback) error { - attachmentsString, _ := run.EvaluateTemplate("@input.attachments") - trimmedString := strings.Trim(attachmentsString, "[]") - attachments := strings.Split(trimmedString, ", ") - if len(attachments) == 1 && strings.Trim(attachments[0], " ") == "" { - attachments = nil + var entry = a.Entry + if len(entry) == 0 { + entry = "@input.text" } - if len(a.Entry) == 0 { - a.Entry = "@input.text" + var attachments []string = nil + if entry == "@input.text" { + attachmentsString, _ := run.EvaluateTemplate("@input.attachments") + trimmedString := strings.Trim(attachmentsString, "[]") + attachments = strings.Split(trimmedString, ", ") + if len(attachments) == 1 && strings.Trim(attachments[0], " ") == "" { + attachments = nil + } } - evaluatedText, evaluatedAttachment, _ := a.evaluateMessage(run, nil, a.Entry, attachments, nil, logEvent) + evaluatedText, evaluatedAttachment, _ := a.evaluateMessage(run, nil, entry, attachments, nil, logEvent) contactURN := run.Contact().PreferredURN() svc, err := run.Session().Engine().Services().Brain(run.Session()) From d2c198d0ef07ce80c474212611e669db592e418d Mon Sep 17 00:00:00 2001 From: Paulo Bernardo Date: Wed, 18 Sep 2024 13:09:58 -0300 Subject: [PATCH 2/2] test: add call_brain tests --- flows/actions/base_test.go | 9 +- flows/actions/testdata/_assets.json | 16 +++ flows/actions/testdata/call_brain.json | 129 ++++++++++++++++++++++++- 3 files changed, 152 insertions(+), 2 deletions(-) diff --git a/flows/actions/base_test.go b/flows/actions/base_test.go index abe464f1e..9e80c22c6 100644 --- a/flows/actions/base_test.go +++ b/flows/actions/base_test.go @@ -24,6 +24,7 @@ import ( "github.com/nyaruka/goflow/flows/resumes" "github.com/nyaruka/goflow/flows/triggers" "github.com/nyaruka/goflow/services/airtime/dtone" + "github.com/nyaruka/goflow/services/brain" "github.com/nyaruka/goflow/services/classification/wit" "github.com/nyaruka/goflow/services/email/smtp" "github.com/nyaruka/goflow/services/webhooks" @@ -129,8 +130,13 @@ func testActionType(t *testing.T, assetsJSON json.RawMessage, typeName string) { // pick a suitable "holder" flow in our assets JSON flowIndex := 0 flowUUID := assets.FlowUUID("bead76f5-dac4-4c9d-996c-c62b326e8c0a") - if tc.InFlowType == flows.FlowTypeVoice { + if typeName == "call_brain" { flowIndex = 1 + flowUUID = assets.FlowUUID("d8c4f0e9-3f3b-4b0b-9d5d-0b4f2e9d3b1b") + } + + if tc.InFlowType == flows.FlowTypeVoice { + flowIndex = 2 flowUUID = assets.FlowUUID("7a84463d-d209-4d3e-a0ff-79f977cd7bd0") } @@ -232,6 +238,7 @@ func testActionType(t *testing.T, assetsJSON json.RawMessage, typeName string) { WithAirtimeServiceFactory(func(flows.Session) (flows.AirtimeService, error) { return dtone.NewService(http.DefaultClient, nil, "nyaruka", "123456789"), nil }). + WithBrainServiceFactory(brain.NewServiceFactory(http.DefaultClient, nil, nil, map[string]string{"User-Agent": "goflow-testing"}, 10000, "token", "http://127.0.0.1:49994")). Build() // create session diff --git a/flows/actions/testdata/_assets.json b/flows/actions/testdata/_assets.json index 0515bb956..4bfb4ef8b 100644 --- a/flows/actions/testdata/_assets.json +++ b/flows/actions/testdata/_assets.json @@ -20,6 +20,22 @@ } ] }, + { + "uuid": "d8c4f0e9-3f3b-4b0b-9d5d-0b4f2e9d3b1b", + "name": "Action Without Exit Tester", + "spec_version": "13.2", + "language": "eng", + "type": "messaging", + "revision": 123, + "localization": {}, + "nodes": [ + { + "uuid": "83b1f5df-49f9-45df-94c9-d86f7ea064e5", + "actions": [], + "exits": [] + } + ] + }, { "uuid": "7a84463d-d209-4d3e-a0ff-79f977cd7bd0", "name": "Voice Action Tester", diff --git a/flows/actions/testdata/call_brain.json b/flows/actions/testdata/call_brain.json index 0637a088a..6a85d09bd 100644 --- a/flows/actions/testdata/call_brain.json +++ b/flows/actions/testdata/call_brain.json @@ -1 +1,128 @@ -[] \ No newline at end of file +[ + { + "description": "Entry is replaced with default value (@input.text) if empty and attachments are sent", + "action": { + "type": "call_brain", + "uuid": "bd154980-7bf7-4ab8-8728-545fd6378912", + "entry": "" + }, + "http_mocks": { + "http://127.0.0.1:49994/messages?token=token": [ + { + "status": 200 + } + ] + }, + "events": [ + { + "type": "webhook_called", + "created_on": "2018-10-18T14:20:30.000123456Z", + "step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c", + "url": "http://127.0.0.1:49994/messages?token=token", + "status_code": 200, + "status": "success", + "request": "POST /messages?token=token HTTP/1.1\r\nHost: 127.0.0.1:49994\r\nUser-Agent: goflow-testing\r\nContent-Length: 253\r\nAccept-Encoding: gzip\r\n\r\n{\"project_uuid\":\"\",\"text\":\"Hi everybody\",\"contact_urn\":\"tel:+12065551212?channel=57f1078f-88aa-46f4-a59a-948a5739c03d\\u0026id=123\",\"attachments\":[\"image/jpeg:http://http://s3.amazon.com/bucket/test.jpg\",\"audio/mp3:http://s3.amazon.com/bucket/test.mp3\"]}", + "response": "HTTP/1.0 200 OK\r\nContent-Length: 0\r\n\r\n", + "elapsed_ms": 0, + "retries": 0, + "extraction": "none" + } + ] + }, + { + "description": "Attachments are not included if entry is not @input.text", + "action": { + "type": "call_brain", + "uuid": "bd154980-7bf7-4ab8-8728-545fd6378912", + "entry": "Different entry" + }, + "http_mocks": { + "http://127.0.0.1:49994/messages?token=token": [ + { + "status": 200 + } + ] + }, + "events": [ + { + "type": "webhook_called", + "created_on": "2018-10-18T14:20:30.000123456Z", + "step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c", + "url": "http://127.0.0.1:49994/messages?token=token", + "status_code": 200, + "status": "success", + "request": "POST /messages?token=token HTTP/1.1\r\nHost: 127.0.0.1:49994\r\nUser-Agent: goflow-testing\r\nContent-Length: 151\r\nAccept-Encoding: gzip\r\n\r\n{\"project_uuid\":\"\",\"text\":\"Different entry\",\"contact_urn\":\"tel:+12065551212?channel=57f1078f-88aa-46f4-a59a-948a5739c03d\\u0026id=123\",\"attachments\":[]}", + "response": "HTTP/1.0 200 OK\r\nContent-Length: 0\r\n\r\n", + "elapsed_ms": 0, + "retries": 0, + "extraction": "none" + } + ] + }, + { + "description": "Creates error events if service call fails", + "action": { + "type": "call_brain", + "uuid": "bd154980-7bf7-4ab8-8728-545fd6378912", + "entry": "" + }, + "http_mocks": { + "http://127.0.0.1:49994/messages?token=token": [ + { + "status": 0, + "body": "" + } + ] + }, + "events": [ + { + "created_on": "2018-10-18T14:20:30.000123456Z", + "step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c", + "text": "unable to connect to server", + "type": "error" + }, + { + "type": "webhook_called", + "created_on": "2018-10-18T14:20:30.000123456Z", + "step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c", + "url": "http://127.0.0.1:49994/messages?token=token", + "status": "connection_error", + "request": "POST /messages?token=token HTTP/1.1\r\nHost: 127.0.0.1:49994\r\nUser-Agent: goflow-testing\r\nContent-Length: 253\r\nAccept-Encoding: gzip\r\n\r\n{\"project_uuid\":\"\",\"text\":\"Hi everybody\",\"contact_urn\":\"tel:+12065551212?channel=57f1078f-88aa-46f4-a59a-948a5739c03d\\u0026id=123\",\"attachments\":[\"image/jpeg:http://http://s3.amazon.com/bucket/test.jpg\",\"audio/mp3:http://s3.amazon.com/bucket/test.mp3\"]}", + "elapsed_ms": 0, + "retries": 0, + "extraction": "none" + } + ] + }, + { + "description": "Create the webhook event for bad request response", + "action": { + "type": "call_brain", + "uuid": "bd154980-7bf7-4ab8-8728-545fd6378912", + "entry": "" + }, + "http_mocks": { + "http://127.0.0.1:49994/messages?token=token": [ + { + "status": 400, + "body": "{ \"errors\": [\"bad_request\"] }" + } + ] + }, + "events": [ + { + "type": "webhook_called", + "created_on": "2018-10-18T14:20:30.000123456Z", + "step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c", + "url": "http://127.0.0.1:49994/messages?token=token", + "status_code": 400, + "status": "response_error", + "request": "POST /messages?token=token HTTP/1.1\r\nHost: 127.0.0.1:49994\r\nUser-Agent: goflow-testing\r\nContent-Length: 253\r\nAccept-Encoding: gzip\r\n\r\n{\"project_uuid\":\"\",\"text\":\"Hi everybody\",\"contact_urn\":\"tel:+12065551212?channel=57f1078f-88aa-46f4-a59a-948a5739c03d\\u0026id=123\",\"attachments\":[\"image/jpeg:http://http://s3.amazon.com/bucket/test.jpg\",\"audio/mp3:http://s3.amazon.com/bucket/test.mp3\"]}", + "response": "HTTP/1.0 400 Bad Request\r\nContent-Length: 29\r\n\r\n{ \"errors\": [\"bad_request\"] }", + "elapsed_ms": 0, + "retries": 0, + "extraction": "valid" + } + ] + } +] \ No newline at end of file