-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #374 from nyaruka/better_telegram_keyboards
Smarter organization of quick replies into rows and columns for telegram keyboards
- Loading branch information
Showing
6 changed files
with
240 additions
and
56 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,32 @@ | ||
package telegram | ||
|
||
import "github.com/nyaruka/courier/utils" | ||
|
||
// KeyboardButton is button on a keyboard, see https://core.telegram.org/bots/api/#keyboardbutton | ||
type KeyboardButton struct { | ||
Text string `json:"text"` | ||
RequestContact bool `json:"request_contact,omitempty"` | ||
RequestLocation bool `json:"request_location,omitempty"` | ||
} | ||
|
||
// ReplyKeyboardMarkup models a keyboard, see https://core.telegram.org/bots/api/#replykeyboardmarkup | ||
type ReplyKeyboardMarkup struct { | ||
Keyboard [][]KeyboardButton `json:"keyboard"` | ||
ResizeKeyboard bool `json:"resize_keyboard"` | ||
OneTimeKeyboard bool `json:"one_time_keyboard"` | ||
} | ||
|
||
// NewKeyboardFromReplies creates a keyboard from the given quick replies | ||
func NewKeyboardFromReplies(replies []string) *ReplyKeyboardMarkup { | ||
rows := utils.StringsToRows(replies, 5, 30, 2) | ||
keyboard := make([][]KeyboardButton, len(rows)) | ||
|
||
for i := range rows { | ||
keyboard[i] = make([]KeyboardButton, len(rows[i])) | ||
for j := range rows[i] { | ||
keyboard[i][j].Text = rows[i][j] | ||
} | ||
} | ||
|
||
return &ReplyKeyboardMarkup{Keyboard: keyboard, ResizeKeyboard: true, OneTimeKeyboard: true} | ||
} |
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,62 @@ | ||
package telegram_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nyaruka/courier/handlers/telegram" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestKeyboardFromReplies(t *testing.T) { | ||
tcs := []struct { | ||
replies []string | ||
expected *telegram.ReplyKeyboardMarkup | ||
}{ | ||
{ | ||
|
||
[]string{"OK"}, | ||
&telegram.ReplyKeyboardMarkup{ | ||
[][]telegram.KeyboardButton{ | ||
{{Text: "OK"}}, | ||
}, | ||
true, true, | ||
}, | ||
}, | ||
{ | ||
[]string{"Yes", "No", "Maybe"}, | ||
&telegram.ReplyKeyboardMarkup{ | ||
[][]telegram.KeyboardButton{ | ||
{{Text: "Yes"}, {Text: "No"}, {Text: "Maybe"}}, | ||
}, | ||
true, true, | ||
}, | ||
}, | ||
{ | ||
[]string{"Vanilla", "Chocolate", "Mint", "Lemon Sorbet", "Papaya", "Strawberry"}, | ||
&telegram.ReplyKeyboardMarkup{ | ||
[][]telegram.KeyboardButton{ | ||
{{Text: "Vanilla"}, {Text: "Chocolate"}}, | ||
{{Text: "Mint"}, {Text: "Lemon Sorbet"}}, | ||
{{Text: "Papaya"}, {Text: "Strawberry"}}, | ||
}, | ||
true, true, | ||
}, | ||
}, | ||
{ | ||
[]string{"A", "B", "C", "D", "Chicken", "Fish", "Peanut Butter Pickle"}, | ||
&telegram.ReplyKeyboardMarkup{ | ||
[][]telegram.KeyboardButton{ | ||
{{Text: "A"}, {Text: "B"}, {Text: "C"}, {Text: "D"}}, | ||
{{Text: "Chicken"}, {Text: "Fish"}}, | ||
{{Text: "Peanut Butter Pickle"}}, | ||
}, | ||
true, true, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
kb := telegram.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
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
Oops, something went wrong.