Skip to content

Set HX user_data parameter as flow name #400

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

Merged
merged 5 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions backends/rapidpro/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (ts *BackendTestSuite) TestMsgUnmarshal() {
"contact_id": 30,
"contact_urn_id": 14,
"error_count": 0,
"flow": {"uuid": "9de3663f-c5c5-4c92-9f45-ecbc09abcc85", "name": "Favorites"},
"modified_on": "2017-07-21T19:22:23.254133Z",
"id": 204,
"channel_uuid": "f3ad3eb6-d00d-4dc3-92e9-9f34f32940ba",
Expand Down Expand Up @@ -140,6 +141,8 @@ func (ts *BackendTestSuite) TestMsgUnmarshal() {
ts.Equal("external-id", msg.ResponseToExternalID())
ts.True(msg.HighPriority())
ts.True(msg.IsResend())
ts.Equal("Favorites", msg.Flow().Name)
ts.Equal("9de3663f-c5c5-4c92-9f45-ecbc09abcc85", msg.Flow().UUID)

msgJSONNoQR := `{
"status": "P",
Expand Down
7 changes: 7 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_ courier.MsgFlowRef `json:"flow,omitempty"`

channel *DBChannel
workerToken queue.WorkerToken
alreadyWritten bool
Expand All @@ -542,6 +544,8 @@ 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() courier.MsgFlowRef { return m.Flow_ }

func (m *DBMsg) QuickReplies() []string {
if m.quickReplies != nil {
return m.quickReplies
Expand Down Expand Up @@ -597,6 +601,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 courier.MsgFlowRef) 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"},
"userdata": []string{"textit"},
"user_data": []string{msg.Flow().Name},
"ret_url": []string{statusURL},
"ret_mo_url": []string{receiveURL},
}
Expand Down
41 changes: 37 additions & 4 deletions handlers/highconnection/highconnection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,35 @@ var defaultSendTestCases = []ChannelSendTestCase{
Text: "Simple Message",
URN: "tel:+250788383383",
Status: "W",
Flow: &struct {
UUID string
Name string
}{UUID: "9de3663f-c5c5-4c92-9f45-ecbc09abcc85", Name: "Favorites"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use FlowReference type

URLParams: map[string]string{
"accountid": "Username",
"password": "Password",
"text": "Simple Message",
"to": "+250788383383",
"ret_id": "10",
"datacoding": "8",
"userdata": "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": "",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A message not from a flow will have the user_data as empty string

"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 +95,18 @@ var defaultSendTestCases = []ChannelSendTestCase{
Text: "☺",
URN: "tel:+250788383383",
Status: "W",
Flow: &struct {
UUID string
Name string
}{UUID: "9de3663f-c5c5-4c92-9f45-ecbc09abcc85", Name: "Favorites"},
URLParams: map[string]string{
"accountid": "Username",
"password": "Password",
"text": "☺",
"to": "+250788383383",
"ret_id": "10",
"datacoding": "8",
"userdata": "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 +116,18 @@ 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: &struct {
UUID string
Name string
}{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",
"userdata": "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 +138,18 @@ var defaultSendTestCases = []ChannelSendTestCase{
Attachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
URN: "tel:+250788383383",
Status: "W",
Flow: &struct {
UUID string
Name string
}{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",
"userdata": "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
8 changes: 8 additions & 0 deletions handlers/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ type ChannelSendTestCase struct {
HighPriority bool
ResponseToExternalID string
Metadata json.RawMessage
Flow *struct {
UUID string
Name string
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use the FlowReference type here?


ResponseStatus int
ResponseBody string
Expand Down Expand Up @@ -231,6 +235,10 @@ func RunChannelSendTestCases(t *testing.T, channel courier.Channel, handler cour
if len(testCase.Metadata) > 0 {
msg.WithMetadata(testCase.Metadata)
}
if testCase.Flow != nil {
var flowRef = courier.MsgFlowRef{UUID: testCase.Flow.UUID, Name: testCase.Flow.Name}
msg.WithFlow(flowRef)
}

var testRequest *http.Request
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
10 changes: 10 additions & 0 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ func NewMsgUUIDFromString(uuidString string) MsgUUID {
return MsgUUID{uuid}
}

type MsgFlowRef struct {
UUID string `json:"uuid" validate:"uuid4"`
Name string `json:"name"`
}

var NilMsgFlowRef = MsgFlowRef{"", ""}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to serialize messages with "flow": {"uuid": "", "name": ""}.. should be a pointer on Msg which can be nil if there's no flow.

While you're changing this let's call it just FlowReference for simplicity and consistency with mailroom/goflow.


//-----------------------------------------------------------------------------
// Msg interface
//-----------------------------------------------------------------------------
Expand All @@ -98,6 +105,8 @@ type Msg interface {
ResponseToExternalID() string
IsResend() bool

Flow() MsgFlowRef

Channel() Channel

ReceivedOn() *time.Time
Expand All @@ -113,6 +122,7 @@ type Msg interface {
WithAttachment(url string) Msg
WithURNAuth(auth string) Msg
WithMetadata(metadata json.RawMessage) Msg
WithFlow(flow MsgFlowRef) Msg

EventID() int64
SessionStatus() string
Expand Down
5 changes: 5 additions & 0 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ type mockMsg struct {
metadata json.RawMessage
alreadyWritten bool
isResend bool
flow MsgFlowRef

receivedOn *time.Time
sentOn *time.Time
Expand All @@ -570,6 +571,8 @@ type mockMsg struct {

func (m *mockMsg) SessionStatus() string { return "" }

func (m *mockMsg) Flow() MsgFlowRef { return m.flow }

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 +606,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 MsgFlowRef) Msg { m.flow = flow; return m }

//-----------------------------------------------------------------------------
// Mock status implementation
//-----------------------------------------------------------------------------
Expand Down