From c2cd20c2c08f5448f62da7fc3ad2395c6ff335e7 Mon Sep 17 00:00:00 2001 From: Zikani Nyirenda Mwase Date: Fri, 12 Jul 2024 21:46:10 +0200 Subject: [PATCH 1/4] fix: store session steps and concatenate africastalking input text closes#9 --- cmd/dialoguss.go | 4 ++++ pkg/africastalking/africastalking.go | 6 ++++-- pkg/africastalking/input.go | 16 ++++++++++++++++ pkg/africastalking/input_test.go | 23 +++++++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 pkg/africastalking/input.go create mode 100644 pkg/africastalking/input_test.go diff --git a/cmd/dialoguss.go b/cmd/dialoguss.go index bc03d8e..cbcd264 100644 --- a/cmd/dialoguss.go +++ b/cmd/dialoguss.go @@ -231,6 +231,7 @@ sessionLoop: } step = NewStep(i, input, "") + output, err = ExecuteStep(step, s) if err != nil { return err @@ -240,6 +241,9 @@ sessionLoop: fmt.Printf("\n\n\tWARN: USSD response contains %d characters\n\twhich is %d more than the recommended limit\n\n", n, n-UssdCharacterLimit) } + step.Expect = output + s.Steps = append(s.Steps, step) + fmt.Println(output) if step.IsLast { break diff --git a/pkg/africastalking/africastalking.go b/pkg/africastalking/africastalking.go index b9b1ad5..3b0c2bd 100644 --- a/pkg/africastalking/africastalking.go +++ b/pkg/africastalking/africastalking.go @@ -31,8 +31,10 @@ func (s *AfricasTalkingRouteStep) ExecuteAsAfricasTalking(session *core.Session) if &text == nil { return "", errors.New("Input Text cannot be nil") } - data.Set("text", text) // TODO(zikani): concat the input - data.Set("channel", "") // TODO: Get the channel + + concatedText := ConcatText(session) + data.Set("text", concatedText) + data.Set("channel", "") res, err := session.Client.PostForm(session.Url, data) if err != nil { diff --git a/pkg/africastalking/input.go b/pkg/africastalking/input.go new file mode 100644 index 0000000..25c9ce5 --- /dev/null +++ b/pkg/africastalking/input.go @@ -0,0 +1,16 @@ +package africastalking + +import ( + "strings" + + "github.com/nndi-oss/dialoguss/pkg/core" +) + +func ConcatText(session *core.Session) string { + inputs := make([]string, 0) + for _, step := range session.Steps { + inputs = append(inputs, step.Text) + } + + return strings.Join(inputs, "*") +} diff --git a/pkg/africastalking/input_test.go b/pkg/africastalking/input_test.go new file mode 100644 index 0000000..db9de31 --- /dev/null +++ b/pkg/africastalking/input_test.go @@ -0,0 +1,23 @@ +package africastalking_test + +import ( + "testing" + + "github.com/nndi-oss/dialoguss/pkg/africastalking" + "github.com/nndi-oss/dialoguss/pkg/core" + "github.com/stretchr/testify/assert" +) + +func TestConcatText(t *testing.T) { + got := africastalking.ConcatText(&core.Session{ + Steps: []*core.Step{ + &core.Step{Text: "1"}, + &core.Step{Text: "2"}, + &core.Step{Text: "3"}, + &core.Step{Text: "4"}, + &core.Step{Text: "5"}, + }, + }) + + assert.Equal(t, "1*2*3*4*5", got) +} From 547059db2e78422fcee788f0a81d99a479e2353c Mon Sep 17 00:00:00 2001 From: Zikani Nyirenda Mwase Date: Fri, 12 Jul 2024 21:49:27 +0200 Subject: [PATCH 2/4] chore: handle nil steps and session --- pkg/africastalking/input.go | 4 ++++ pkg/africastalking/input_test.go | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/pkg/africastalking/input.go b/pkg/africastalking/input.go index 25c9ce5..db5050c 100644 --- a/pkg/africastalking/input.go +++ b/pkg/africastalking/input.go @@ -7,6 +7,10 @@ import ( ) func ConcatText(session *core.Session) string { + if session == nil { + return "" + } + inputs := make([]string, 0) for _, step := range session.Steps { inputs = append(inputs, step.Text) diff --git a/pkg/africastalking/input_test.go b/pkg/africastalking/input_test.go index db9de31..e4b3b20 100644 --- a/pkg/africastalking/input_test.go +++ b/pkg/africastalking/input_test.go @@ -8,6 +8,19 @@ import ( "github.com/stretchr/testify/assert" ) +func TestConcatTextWithNilSession(t *testing.T) { + got := africastalking.ConcatText(nil) + assert.Equal(t, "", got) +} + +func TestConcatTextWithNilSteps(t *testing.T) { + got := africastalking.ConcatText(&core.Session{ + Steps: nil, + }) + + assert.Equal(t, "", got) +} + func TestConcatText(t *testing.T) { got := africastalking.ConcatText(&core.Session{ Steps: []*core.Step{ From 6f3a2270898b796985fa1daf695334b647eb10ba Mon Sep 17 00:00:00 2001 From: Zikani Nyirenda Mwase Date: Fri, 12 Jul 2024 21:52:49 +0200 Subject: [PATCH 3/4] fix(docs): update README instructions --- README.md | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a2759ce..68f98d4 100644 --- a/README.md +++ b/README.md @@ -40,9 +40,15 @@ phoneNumber: 265888123456 ``` ```sh -$ dialoguss -i -f app.yaml -Sending *123*1234# to -USSD Response: +$ dialoguss --interactive --file=app.yaml simulate +``` + +`dialoguss` will start hitting your USSD endpoint to enable you to simulate +interaction, the whole session can be performed from the command-line. + +An example output is below: + +``` What is your name? > name: Zikani @@ -85,7 +91,7 @@ sessions: ``` ```sh -$ dialoguss -f app.yml +$ dialoguss --file=app.yml run All steps in session 12345678910 run successfully ``` @@ -104,4 +110,4 @@ it to production. --- -Copyright (c) 2018 - 2020, NNDI \ No newline at end of file +Copyright (c) 2018 - 2024, NNDI \ No newline at end of file From 5aaa5fbd8ab9253faf865d71c2a42e587710b42c Mon Sep 17 00:00:00 2001 From: Zikani Nyirenda Mwase Date: Fri, 12 Jul 2024 23:01:32 +0200 Subject: [PATCH 4/4] fix: handle empty result on concated text better --- pkg/africastalking/africastalking.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/africastalking/africastalking.go b/pkg/africastalking/africastalking.go index 3b0c2bd..230cd7d 100644 --- a/pkg/africastalking/africastalking.go +++ b/pkg/africastalking/africastalking.go @@ -32,8 +32,12 @@ func (s *AfricasTalkingRouteStep) ExecuteAsAfricasTalking(session *core.Session) return "", errors.New("Input Text cannot be nil") } - concatedText := ConcatText(session) - data.Set("text", concatedText) + if concatedText := ConcatText(session); concatedText != "" { + data.Set("text", concatedText) + } else { + data.Set("text", text) + } + data.Set("channel", "") res, err := session.Client.PostForm(session.Url, data)