Skip to content

Commit 9ff2cd7

Browse files
committed
feat: botkb package + buttons.go + MessageKeyboard type
1 parent 79bf93f commit 9ff2cd7

4 files changed

Lines changed: 176 additions & 33 deletions

File tree

botkb/buttons.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package botkb
2+
3+
type ButtonType int
4+
5+
type Button interface {
6+
ButtonType() ButtonType
7+
}
8+
9+
const (
10+
ButtonTypeData ButtonType = iota
11+
ButtonTypeURL
12+
)
13+
14+
var _ Button = (*DataButton)(nil)
15+
var _ Button = (*UrlButton)(nil)
16+
17+
type DataButton struct {
18+
Text string
19+
Data string
20+
}
21+
22+
func (DataButton) ButtonType() ButtonType {
23+
return ButtonTypeData
24+
}
25+
26+
type UrlButton struct {
27+
Text string
28+
URL string
29+
}
30+
31+
func (UrlButton) ButtonType() ButtonType {
32+
return ButtonTypeURL
33+
}

botkb/keyboard.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package botkb
2+
3+
// KeyboardType defines MessageKeyboard type
4+
type KeyboardType int
5+
6+
//goland:noinspection GoUnusedConst
7+
const (
8+
// KeyboardTypeNone for no MessageKeyboard
9+
// Used by: Telegram
10+
KeyboardTypeNone KeyboardType = iota
11+
12+
// KeyboardTypeHide commands to hide MessageKeyboard
13+
// Used by: Telegram
14+
KeyboardTypeHide
15+
16+
// KeyboardTypeInline for inline MessageKeyboard
17+
// Used by: Telegram
18+
KeyboardTypeInline
19+
20+
// KeyboardTypeBottom for bottom MessageKeyboard
21+
// Used by: Telegram
22+
KeyboardTypeBottom
23+
24+
// KeyboardTypeForceReply to force reply from a user
25+
// Used by: Telegram
26+
KeyboardTypeForceReply
27+
)
28+
29+
// Keyboard defines MessageKeyboard
30+
type Keyboard interface {
31+
// KeyboardType defines MessageKeyboard type
32+
KeyboardType() KeyboardType
33+
}
34+
35+
var _ Keyboard = (*MessageKeyboard)(nil)
36+
37+
type MessageKeyboard struct {
38+
kbType KeyboardType
39+
Buttons [][]Button
40+
}
41+
42+
func (k MessageKeyboard) KeyboardType() KeyboardType {
43+
return k.kbType
44+
}
45+
46+
func NewMessageKeyboard(kbType KeyboardType, buttons ...[]Button) *MessageKeyboard {
47+
return &MessageKeyboard{
48+
kbType: kbType,
49+
Buttons: buttons,
50+
}
51+
}

botkb/keyboard_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package botkb
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestNewMessageKeyboard(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
kbType KeyboardType
12+
buttons [][]Button
13+
want *MessageKeyboard
14+
}{
15+
{
16+
name: "Empty keyboard with KeyboardTypeNone",
17+
kbType: KeyboardTypeNone,
18+
buttons: [][]Button{},
19+
want: &MessageKeyboard{
20+
kbType: KeyboardTypeNone,
21+
Buttons: [][]Button{},
22+
},
23+
},
24+
{
25+
name: "Empty keyboard with KeyboardTypeInline",
26+
kbType: KeyboardTypeInline,
27+
buttons: [][]Button{},
28+
want: &MessageKeyboard{
29+
kbType: KeyboardTypeInline,
30+
Buttons: [][]Button{},
31+
},
32+
},
33+
{
34+
name: "Keyboard with one row of data buttons",
35+
kbType: KeyboardTypeBottom,
36+
buttons: [][]Button{
37+
{
38+
&DataButton{Text: "Button 1", Data: "data1"},
39+
&DataButton{Text: "Button 2", Data: "data2"},
40+
},
41+
},
42+
want: &MessageKeyboard{
43+
kbType: KeyboardTypeBottom,
44+
Buttons: [][]Button{
45+
{
46+
&DataButton{Text: "Button 1", Data: "data1"},
47+
&DataButton{Text: "Button 2", Data: "data2"},
48+
},
49+
},
50+
},
51+
},
52+
{
53+
name: "Keyboard with multiple rows of mixed buttons",
54+
kbType: KeyboardTypeInline,
55+
buttons: [][]Button{
56+
{
57+
&DataButton{Text: "Button 1", Data: "data1"},
58+
&UrlButton{Text: "URL 1", URL: "https://example.com"},
59+
},
60+
{
61+
&DataButton{Text: "Button 2", Data: "data2"},
62+
},
63+
},
64+
want: &MessageKeyboard{
65+
kbType: KeyboardTypeInline,
66+
Buttons: [][]Button{
67+
{
68+
&DataButton{Text: "Button 1", Data: "data1"},
69+
&UrlButton{Text: "URL 1", URL: "https://example.com"},
70+
},
71+
{
72+
&DataButton{Text: "Button 2", Data: "data2"},
73+
},
74+
},
75+
},
76+
},
77+
}
78+
79+
for _, tt := range tests {
80+
t.Run(tt.name, func(t *testing.T) {
81+
got := NewMessageKeyboard(tt.kbType, tt.buttons...)
82+
if !reflect.DeepEqual(got, tt.want) {
83+
t.Errorf("NewMessageKeyboard() = %v, want %v", got, tt.want)
84+
}
85+
86+
// Also test the KeyboardType method
87+
if got.KeyboardType() != tt.kbType {
88+
t.Errorf("MessageKeyboard.KeyboardType() = %v, want %v", got.KeyboardType(), tt.kbType)
89+
}
90+
})
91+
}
92+
}

keyboard.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)