From 04bd7559602da8b4ee6799e2b09c59b033feb68b Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Wed, 19 Jan 2022 11:48:31 +0200 Subject: [PATCH] Send flow name as user_data to HX --- backends/rapidpro/msg.go | 25 ++++++++++++++++ handlers/highconnection/highconnection.go | 2 +- .../highconnection/highconnection_test.go | 30 ++++++++++++++++--- handlers/test.go | 4 +++ msg.go | 5 ++++ test.go | 22 ++++++++++++++ 6 files changed, 83 insertions(+), 5 deletions(-) diff --git a/backends/rapidpro/msg.go b/backends/rapidpro/msg.go index cd96c5b4e..fca7d334f 100644 --- a/backends/rapidpro/msg.go +++ b/backends/rapidpro/msg.go @@ -518,6 +518,8 @@ type DBMsg struct { SessionWaitStartedOn_ *time.Time `json:"session_wait_started_on,omitempty"` SessionStatus_ string `json:"session_status,omitempty"` + Flow_ json.RawMessage `json:"flow,omitempty"` + channel *DBChannel workerToken queue.WorkerToken alreadyWritten bool @@ -542,6 +544,26 @@ func (m *DBMsg) IsResend() bool { return m.IsResend_ } func (m *DBMsg) Channel() courier.Channel { return m.channel } func (m *DBMsg) SessionStatus() string { return m.SessionStatus_ } +func (m *DBMsg) Flow() json.RawMessage { + return m.Flow_ +} + +func (m *DBMsg) FlowName() string { + if m.Flow_ == nil { + return "" + } + name, _, _, _ := jsonparser.Get(m.Flow_, "name") + return string(name) +} + +func (m *DBMsg) FlowUUID() string { + if m.Flow_ == nil { + return "" + } + flowUUID, _, _, _ := jsonparser.Get(m.Flow_, "uuid") + return string(flowUUID) +} + func (m *DBMsg) QuickReplies() []string { if m.quickReplies != nil { return m.quickReplies @@ -597,6 +619,9 @@ func (m *DBMsg) WithUUID(uuid courier.MsgUUID) courier.Msg { m.UUID_ = uuid; ret // WithMetadata can be used to add metadata to a Msg func (m *DBMsg) WithMetadata(metadata json.RawMessage) courier.Msg { m.Metadata_ = metadata; return m } +// WithFlow can be used to add flow to a Msg +func (m *DBMsg) WithFlow(flow json.RawMessage) courier.Msg { m.Flow_ = flow; return m } + // WithAttachment can be used to append to the media urls for a message func (m *DBMsg) WithAttachment(url string) courier.Msg { m.Attachments_ = append(m.Attachments_, url) diff --git a/handlers/highconnection/highconnection.go b/handlers/highconnection/highconnection.go index c03c8c2ce..70f2cec7c 100644 --- a/handlers/highconnection/highconnection.go +++ b/handlers/highconnection/highconnection.go @@ -142,7 +142,7 @@ func (h *handler) SendMsg(ctx context.Context, msg courier.Msg) (courier.MsgStat "to": []string{msg.URN().Path()}, "ret_id": []string{msg.ID().String()}, "datacoding": []string{"8"}, - "user_data": []string{"textit"}, + "user_data": []string{msg.FlowName()}, "ret_url": []string{statusURL}, "ret_mo_url": []string{receiveURL}, } diff --git a/handlers/highconnection/highconnection_test.go b/handlers/highconnection/highconnection_test.go index 61b419cff..8a7b8f370 100644 --- a/handlers/highconnection/highconnection_test.go +++ b/handlers/highconnection/highconnection_test.go @@ -1,6 +1,7 @@ package highconnection import ( + "encoding/json" "net/http/httptest" "testing" "time" @@ -57,6 +58,7 @@ var defaultSendTestCases = []ChannelSendTestCase{ Text: "Simple Message", URN: "tel:+250788383383", Status: "W", + Flow: json.RawMessage(`{"uuid": "9de3663f-c5c5-4c92-9f45-ecbc09abcc85", "name": "Favorites"}`), URLParams: map[string]string{ "accountid": "Username", "password": "Password", @@ -64,7 +66,24 @@ var defaultSendTestCases = []ChannelSendTestCase{ "to": "+250788383383", "ret_id": "10", "datacoding": "8", - "user_data": "textit", + "user_data": "Favorites", + "ret_url": "https://localhost/c/hx/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status", + "ret_mo_url": "https://localhost/c/hx/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/receive", + }, + ResponseStatus: 200, + SendPrep: setSendURL}, + {Label: "Plain Send without flow", + Text: "Simple Message", + URN: "tel:+250788383383", + Status: "W", + URLParams: map[string]string{ + "accountid": "Username", + "password": "Password", + "text": "Simple Message", + "to": "+250788383383", + "ret_id": "10", + "datacoding": "8", + "user_data": "", "ret_url": "https://localhost/c/hx/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status", "ret_mo_url": "https://localhost/c/hx/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/receive", }, @@ -74,6 +93,7 @@ var defaultSendTestCases = []ChannelSendTestCase{ Text: "☺", URN: "tel:+250788383383", Status: "W", + Flow: json.RawMessage(`{"uuid": "9de3663f-c5c5-4c92-9f45-ecbc09abcc85", "name": "Favorites"}`), URLParams: map[string]string{ "accountid": "Username", "password": "Password", @@ -81,7 +101,7 @@ var defaultSendTestCases = []ChannelSendTestCase{ "to": "+250788383383", "ret_id": "10", "datacoding": "8", - "user_data": "textit", + "user_data": "Favorites", "ret_url": "https://localhost/c/hx/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status", "ret_mo_url": "https://localhost/c/hx/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/receive", }, @@ -91,6 +111,7 @@ var defaultSendTestCases = []ChannelSendTestCase{ Text: "This is a longer message than 160 characters and will cause us to split it into two separate parts, isn't that right but it is even longer than before I say, I need to keep adding more things to make it work", URN: "tel:+250788383383", Status: "W", + Flow: json.RawMessage(`{"uuid": "9de3663f-c5c5-4c92-9f45-ecbc09abcc85", "name": "Favorites"}`), URLParams: map[string]string{ "accountid": "Username", "password": "Password", @@ -98,7 +119,7 @@ var defaultSendTestCases = []ChannelSendTestCase{ "to": "+250788383383", "ret_id": "10", "datacoding": "8", - "user_data": "textit", + "user_data": "Favorites", "ret_url": "https://localhost/c/hx/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status", "ret_mo_url": "https://localhost/c/hx/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/receive", }, @@ -109,6 +130,7 @@ var defaultSendTestCases = []ChannelSendTestCase{ Attachments: []string{"image/jpeg:https://foo.bar/image.jpg"}, URN: "tel:+250788383383", Status: "W", + Flow: json.RawMessage(`{"uuid": "9de3663f-c5c5-4c92-9f45-ecbc09abcc85", "name": "Favorites"}`), URLParams: map[string]string{ "accountid": "Username", "password": "Password", @@ -116,7 +138,7 @@ var defaultSendTestCases = []ChannelSendTestCase{ "to": "+250788383383", "ret_id": "10", "datacoding": "8", - "user_data": "textit", + "user_data": "Favorites", "ret_url": "https://localhost/c/hx/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/status", "ret_mo_url": "https://localhost/c/hx/8eb23e93-5ecb-45ba-b726-3b064e0c56ab/receive", }, diff --git a/handlers/test.go b/handlers/test.go index 9f5a88e3a..0c67d1f4e 100644 --- a/handlers/test.go +++ b/handlers/test.go @@ -89,6 +89,7 @@ type ChannelSendTestCase struct { HighPriority bool ResponseToExternalID string Metadata json.RawMessage + Flow json.RawMessage ResponseStatus int ResponseBody string @@ -231,6 +232,9 @@ func RunChannelSendTestCases(t *testing.T, channel courier.Channel, handler cour if len(testCase.Metadata) > 0 { msg.WithMetadata(testCase.Metadata) } + if len(testCase.Flow) > 0 { + msg.WithFlow(testCase.Flow) + } var testRequest *http.Request server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/msg.go b/msg.go index 809370f20..b45b51a5b 100644 --- a/msg.go +++ b/msg.go @@ -98,6 +98,10 @@ type Msg interface { ResponseToExternalID() string IsResend() bool + Flow() json.RawMessage + FlowName() string + FlowUUID() string + Channel() Channel ReceivedOn() *time.Time @@ -113,6 +117,7 @@ type Msg interface { WithAttachment(url string) Msg WithURNAuth(auth string) Msg WithMetadata(metadata json.RawMessage) Msg + WithFlow(flow json.RawMessage) Msg EventID() int64 SessionStatus() string diff --git a/test.go b/test.go index 012c67a1b..385375e71 100644 --- a/test.go +++ b/test.go @@ -11,6 +11,7 @@ import ( "sync" "time" + "github.com/buger/jsonparser" "github.com/nyaruka/gocommon/urns" "github.com/nyaruka/gocommon/uuids" @@ -562,6 +563,7 @@ type mockMsg struct { metadata json.RawMessage alreadyWritten bool isResend bool + flow json.RawMessage receivedOn *time.Time sentOn *time.Time @@ -570,6 +572,24 @@ type mockMsg struct { func (m *mockMsg) SessionStatus() string { return "" } +func (m *mockMsg) Flow() json.RawMessage { return m.flow } + +func (m *mockMsg) FlowName() string { + if m.flow == nil { + return "" + } + name, _, _, _ := jsonparser.Get(m.flow, "name") + return string(name) +} + +func (m *mockMsg) FlowUUID() string { + if m.flow == nil { + return "" + } + flowUUID, _, _, _ := jsonparser.Get(m.flow, "uuid") + return string(flowUUID) +} + func (m *mockMsg) Channel() Channel { return m.channel } func (m *mockMsg) ID() MsgID { return m.id } func (m *mockMsg) EventID() int64 { return int64(m.id) } @@ -603,6 +623,8 @@ func (m *mockMsg) WithAttachment(url string) Msg { } func (m *mockMsg) WithMetadata(metadata json.RawMessage) Msg { m.metadata = metadata; return m } +func (m *mockMsg) WithFlow(flow json.RawMessage) Msg { m.flow = flow; return m } + //----------------------------------------------------------------------------- // Mock status implementation //-----------------------------------------------------------------------------