Skip to content
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

fix: store session steps and concatenate africastalking input text closes#9 #15

Merged
merged 4 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ phoneNumber: 265888123456
```

```sh
$ dialoguss -i -f app.yaml
Sending *123*1234# to <app>
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

Expand Down Expand Up @@ -85,7 +91,7 @@ sessions:
```

```sh
$ dialoguss -f app.yml
$ dialoguss --file=app.yml run
All steps in session 12345678910 run successfully
```

Expand All @@ -104,4 +110,4 @@ it to production.

---

Copyright (c) 2018 - 2020, NNDI
Copyright (c) 2018 - 2024, NNDI
4 changes: 4 additions & 0 deletions cmd/dialoguss.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ sessionLoop:
}

step = NewStep(i, input, "")

output, err = ExecuteStep(step, s)
if err != nil {
return err
Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions pkg/africastalking/africastalking.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ 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

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)
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions pkg/africastalking/input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package africastalking

import (
"strings"

"github.com/nndi-oss/dialoguss/pkg/core"
)

func ConcatText(session *core.Session) string {
if session == nil {
return ""
}

inputs := make([]string, 0)
for _, step := range session.Steps {
inputs = append(inputs, step.Text)
}

return strings.Join(inputs, "*")
}
36 changes: 36 additions & 0 deletions pkg/africastalking/input_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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 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{
&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)
}
Loading