Skip to content

Commit

Permalink
Send flow name as user_data to HX
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Jan 20, 2022
1 parent 3c8c801 commit 04bd755
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 5 deletions.
25 changes: 25 additions & 0 deletions backends/rapidpro/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion handlers/highconnection/highconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}
Expand Down
30 changes: 26 additions & 4 deletions handlers/highconnection/highconnection_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package highconnection

import (
"encoding/json"
"net/http/httptest"
"testing"
"time"
Expand Down Expand Up @@ -57,14 +58,32 @@ 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",
"text": "Simple Message",
"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",
},
Expand All @@ -74,14 +93,15 @@ 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",
"text": "☺",
"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",
},
Expand All @@ -91,14 +111,15 @@ 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",
"text": "I need to keep adding more things to make it work",
"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",
},
Expand All @@ -109,14 +130,15 @@ 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",
"text": "My pic!\nhttps://foo.bar/image.jpg",
"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",
},
Expand Down
4 changes: 4 additions & 0 deletions handlers/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type ChannelSendTestCase struct {
HighPriority bool
ResponseToExternalID string
Metadata json.RawMessage
Flow json.RawMessage

ResponseStatus int
ResponseBody string
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ type Msg interface {
ResponseToExternalID() string
IsResend() bool

Flow() json.RawMessage
FlowName() string
FlowUUID() string

Channel() Channel

ReceivedOn() *time.Time
Expand All @@ -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
Expand Down
22 changes: 22 additions & 0 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sync"
"time"

"github.com/buger/jsonparser"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/gocommon/uuids"

Expand Down Expand Up @@ -562,6 +563,7 @@ type mockMsg struct {
metadata json.RawMessage
alreadyWritten bool
isResend bool
flow json.RawMessage

receivedOn *time.Time
sentOn *time.Time
Expand All @@ -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) }
Expand Down Expand Up @@ -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
//-----------------------------------------------------------------------------
Expand Down

0 comments on commit 04bd755

Please sign in to comment.