Skip to content

Commit

Permalink
Merge pull request #76 from weni-ai/fix/brain-only-send-attachments-w…
Browse files Browse the repository at this point in the history
…hen-defaults

[FLOWS-3006] Fix: brain only send attachments when defaults
  • Loading branch information
paulobernardoaf committed Sep 18, 2024
2 parents 8e0cb8b + d2c198d commit 5c22918
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 10 deletions.
9 changes: 8 additions & 1 deletion flows/actions/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions flows/actions/call_brain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
16 changes: 16 additions & 0 deletions flows/actions/testdata/_assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
129 changes: 128 additions & 1 deletion flows/actions/testdata/call_brain.json
Original file line number Diff line number Diff line change
@@ -1 +1,128 @@
[]
[
{
"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"
}
]
}
]

0 comments on commit 5c22918

Please sign in to comment.