Skip to content

Commit 2e81f59

Browse files
committed
fix: func NewOnetimeKeyboard(buttons ...[]Button) *MessageKeyboard {
1 parent bb9e60b commit 2e81f59

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

botkb/keyboard.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ type Keyboard interface {
3535
var _ Keyboard = (*MessageKeyboard)(nil)
3636

3737
type MessageKeyboard struct {
38-
kbType KeyboardType
39-
Buttons [][]Button
38+
kbType KeyboardType
39+
isOneTime bool
40+
Buttons [][]Button
41+
}
42+
43+
func (k MessageKeyboard) IsOneTime() bool {
44+
return k.isOneTime
4045
}
4146

4247
func (k MessageKeyboard) KeyboardType() KeyboardType {
@@ -49,3 +54,9 @@ func NewMessageKeyboard(kbType KeyboardType, buttons ...[]Button) *MessageKeyboa
4954
Buttons: buttons,
5055
}
5156
}
57+
58+
func NewOnetimeKeyboard(buttons ...[]Button) *MessageKeyboard {
59+
kb := NewMessageKeyboard(KeyboardTypeBottom, buttons...)
60+
kb.isOneTime = true
61+
return kb
62+
}

botkb/keyboard_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,87 @@ import (
55
"testing"
66
)
77

8+
func TestNewOnetimeKeyboard(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
buttons [][]Button
12+
want *MessageKeyboard
13+
}{
14+
{
15+
name: "Empty onetime keyboard",
16+
buttons: [][]Button{},
17+
want: &MessageKeyboard{
18+
kbType: KeyboardTypeBottom,
19+
isOneTime: true,
20+
Buttons: [][]Button{},
21+
},
22+
},
23+
{
24+
name: "Onetime keyboard with one row of data buttons",
25+
buttons: [][]Button{
26+
{
27+
&DataButton{Text: "Button 1", Data: "data1"},
28+
&DataButton{Text: "Button 2", Data: "data2"},
29+
},
30+
},
31+
want: &MessageKeyboard{
32+
kbType: KeyboardTypeBottom,
33+
isOneTime: true,
34+
Buttons: [][]Button{
35+
{
36+
&DataButton{Text: "Button 1", Data: "data1"},
37+
&DataButton{Text: "Button 2", Data: "data2"},
38+
},
39+
},
40+
},
41+
},
42+
{
43+
name: "Onetime keyboard with multiple rows of mixed buttons",
44+
buttons: [][]Button{
45+
{
46+
&DataButton{Text: "Button 1", Data: "data1"},
47+
&UrlButton{Text: "URL 1", URL: "https://example.com"},
48+
},
49+
{
50+
&DataButton{Text: "Button 2", Data: "data2"},
51+
},
52+
},
53+
want: &MessageKeyboard{
54+
kbType: KeyboardTypeBottom,
55+
isOneTime: true,
56+
Buttons: [][]Button{
57+
{
58+
&DataButton{Text: "Button 1", Data: "data1"},
59+
&UrlButton{Text: "URL 1", URL: "https://example.com"},
60+
},
61+
{
62+
&DataButton{Text: "Button 2", Data: "data2"},
63+
},
64+
},
65+
},
66+
},
67+
}
68+
69+
for _, tt := range tests {
70+
t.Run(tt.name, func(t *testing.T) {
71+
got := NewOnetimeKeyboard(tt.buttons...)
72+
if !reflect.DeepEqual(got, tt.want) {
73+
t.Errorf("NewOnetimeKeyboard() = %v, want %v", got, tt.want)
74+
}
75+
76+
// Test the KeyboardType method
77+
if got.KeyboardType() != KeyboardTypeBottom {
78+
t.Errorf("MessageKeyboard.KeyboardType() = %v, want %v", got.KeyboardType(), KeyboardTypeBottom)
79+
}
80+
81+
// Test the IsOneTime method
82+
if !got.IsOneTime() {
83+
t.Errorf("MessageKeyboard.IsOneTime() = %v, want true", got.IsOneTime())
84+
}
85+
})
86+
}
87+
}
88+
889
func TestNewMessageKeyboard(t *testing.T) {
990
tests := []struct {
1091
name string

0 commit comments

Comments
 (0)