forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
145 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package vk | ||
|
||
import ( | ||
"github.com/nyaruka/courier/utils" | ||
"github.com/nyaruka/gocommon/jsonx" | ||
) | ||
|
||
type Keyboard struct { | ||
One_Time bool `json:"one_time"` | ||
Buttons [][]ButtonPayload `json:"buttons"` | ||
Inline bool `json:"inline"` | ||
} | ||
|
||
type ButtonPayload struct { | ||
Action ButtonAction `json:"action"` | ||
Color string `json:"color"` | ||
} | ||
|
||
type ButtonAction struct { | ||
Type string `json:"type"` | ||
Label string `json:"label"` | ||
Payload string `json:"payload"` | ||
} | ||
|
||
// NewKeyboardFromReplies creates a keyboard from the given quick replies | ||
func NewKeyboardFromReplies(replies []string) *Keyboard { | ||
rows := utils.StringsToRows(replies, 10, 30, 2) | ||
buttons := make([][]ButtonPayload, len(rows)) | ||
|
||
for i := range rows { | ||
buttons[i] = make([]ButtonPayload, len(rows[i])) | ||
for j := range rows[i] { | ||
buttons[i][j].Action.Label = rows[i][j] | ||
buttons[i][j].Action.Type = "text" | ||
buttons[i][j].Action.Payload = string(jsonx.MustMarshal(rows[i][j])) | ||
buttons[i][j].Color = "primary" | ||
} | ||
} | ||
|
||
return &Keyboard{One_Time: true, Buttons: buttons, Inline: false} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package vk_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nyaruka/courier/handlers/vk" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestKeyboardFromReplies(t *testing.T) { | ||
tcs := []struct { | ||
replies []string | ||
expected *vk.Keyboard | ||
}{ | ||
{ | ||
|
||
[]string{"OK"}, | ||
&vk.Keyboard{ | ||
true, | ||
[][]vk.ButtonPayload{ | ||
{ | ||
{vk.ButtonAction{Type: "text", Label: "OK", Payload: "\"OK\""}, "primary"}, | ||
}, | ||
}, | ||
false, | ||
}, | ||
}, | ||
{ | ||
[]string{"Yes", "No", "Maybe"}, | ||
&vk.Keyboard{ | ||
true, | ||
[][]vk.ButtonPayload{ | ||
{ | ||
{vk.ButtonAction{Type: "text", Label: "Yes", Payload: "\"Yes\""}, "primary"}, | ||
{vk.ButtonAction{Type: "text", Label: "No", Payload: "\"No\""}, "primary"}, | ||
{vk.ButtonAction{Type: "text", Label: "Maybe", Payload: "\"Maybe\""}, "primary"}, | ||
}, | ||
}, | ||
false, | ||
}, | ||
}, | ||
{ | ||
[]string{"Vanilla", "Chocolate", "Mint", "Lemon Sorbet", "Papaya", "Strawberry"}, | ||
&vk.Keyboard{ | ||
true, | ||
[][]vk.ButtonPayload{ | ||
|
||
{{vk.ButtonAction{Type: "text", Label: "Vanilla", Payload: "\"Vanilla\""}, "primary"}}, | ||
{{vk.ButtonAction{Type: "text", Label: "Chocolate", Payload: "\"Chocolate\""}, "primary"}}, | ||
{{vk.ButtonAction{Type: "text", Label: "Mint", Payload: "\"Mint\""}, "primary"}}, | ||
{{vk.ButtonAction{Type: "text", Label: "Lemon Sorbet", Payload: "\"Lemon Sorbet\""}, "primary"}}, | ||
{{vk.ButtonAction{Type: "text", Label: "Papaya", Payload: "\"Papaya\""}, "primary"}}, | ||
{{vk.ButtonAction{Type: "text", Label: "Strawberry", Payload: "\"Strawberry\""}, "primary"}}, | ||
}, | ||
false, | ||
}, | ||
}, | ||
{ | ||
[]string{"A", "B", "C", "D", "Chicken", "Fish", "Peanut Butter Pickle"}, | ||
&vk.Keyboard{ | ||
true, | ||
[][]vk.ButtonPayload{ | ||
|
||
{{vk.ButtonAction{Type: "text", Label: "A", Payload: "\"A\""}, "primary"}}, | ||
{{vk.ButtonAction{Type: "text", Label: "B", Payload: "\"B\""}, "primary"}}, | ||
{{vk.ButtonAction{Type: "text", Label: "C", Payload: "\"C\""}, "primary"}}, | ||
{{vk.ButtonAction{Type: "text", Label: "D", Payload: "\"D\""}, "primary"}}, | ||
{{vk.ButtonAction{Type: "text", Label: "Chicken", Payload: "\"Chicken\""}, "primary"}}, | ||
{{vk.ButtonAction{Type: "text", Label: "Fish", Payload: "\"Fish\""}, "primary"}}, | ||
{{vk.ButtonAction{Type: "text", Label: "Peanut Butter Pickle", Payload: "\"Peanut Butter Pickle\""}, "primary"}}, | ||
}, | ||
false, | ||
}, | ||
}, | ||
{ | ||
[]string{"A", "B", "C", "D", "E"}, | ||
&vk.Keyboard{ | ||
true, | ||
[][]vk.ButtonPayload{ | ||
|
||
{ | ||
{vk.ButtonAction{Type: "text", Label: "A", Payload: "\"A\""}, "primary"}, | ||
{vk.ButtonAction{Type: "text", Label: "B", Payload: "\"B\""}, "primary"}, | ||
{vk.ButtonAction{Type: "text", Label: "C", Payload: "\"C\""}, "primary"}, | ||
{vk.ButtonAction{Type: "text", Label: "D", Payload: "\"D\""}, "primary"}, | ||
{vk.ButtonAction{Type: "text", Label: "E", Payload: "\"E\""}, "primary"}, | ||
}, | ||
}, | ||
false, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
kb := vk.NewKeyboardFromReplies(tc.replies) | ||
assert.Equal(t, tc.expected, kb, "keyboard mismatch for replies %v", tc.replies) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters