Skip to content

Commit

Permalink
Merge pull request #15 from nndi-oss/fix/africastalking-concat
Browse files Browse the repository at this point in the history
fix: store session steps and concatenate africastalking input text closes#9
  • Loading branch information
zikani03 authored Jul 12, 2024
2 parents b0a58d8 + 5aaa5fb commit 5ad1571
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 7 deletions.
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)
}

0 comments on commit 5ad1571

Please sign in to comment.