-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkeyboard.go
64 lines (52 loc) · 1.25 KB
/
keyboard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package tbcomctl
import tb "gopkg.in/telebot.v3"
type BtnLabel string
type KbdOption func(k *Keyboard)
func KbdOptButtonsInRow(n int) KbdOption {
return func(k *Keyboard) {
if n > 0 {
k.btnsInRow = n
}
}
}
type Keyboard struct {
commonCtl
cmds KeyboardCommands
btnsInRow int
}
type KeyboardCmd struct {
Label BtnLabel
Handler tb.HandlerFunc
}
type KeyboardCommands []KeyboardCmd
// type KeyboardCommands map[BtnLabel]func(c tb.Context) error
func NewKeyboard(cmds KeyboardCommands, opts ...KbdOption) *Keyboard {
kbd := &Keyboard{
commonCtl: commonCtl{},
cmds: cmds,
btnsInRow: defNumButtons,
}
for _, opt := range opts {
opt(kbd)
}
return kbd
}
// Markup returns the markup to be sent to user.
func (k *Keyboard) Markup(b *tb.Bot, lang string) *tb.ReplyMarkup {
m := &tb.ReplyMarkup{ResizeKeyboard: true}
p := Printer(lang, k.fallbackLang)
var btns []tb.Btn
for _, kc := range k.cmds {
btn := m.Text(p.Sprintf(string(kc.Label)))
btns = append(btns, btn)
b.Handle(&btn, kc.Handler)
}
m.Reply(OrganizeButtons(btns, k.btnsInRow)...)
return m
}
// InitForLanguages initialises handlers for languages listed.
func (k *Keyboard) InitForLanguages(b *tb.Bot, lang ...string) {
for _, l := range lang {
k.Markup(b, l)
}
}