tg/src/behx/keyboard.go

76 lines
1.7 KiB
Go
Raw Normal View History

2023-07-09 01:28:59 +03:00
package behx
import (
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
/*
var otherKeyboard = tgbotapi.NewReplyKeyboard(
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("a"),
tgbotapi.NewKeyboardButton("b"),
tgbotapi.NewKeyboardButton("c"),
),
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("d"),
tgbotapi.NewKeyboardButton("e"),
tgbotapi.NewKeyboardButton("f"),
),
)*/
type KeyboardId string
2023-07-09 01:28:59 +03:00
// The type represents reply keyboard which
// is supposed to be showed on a Screen.
type Keyboard struct {
Rows []ButtonRow
2023-07-09 01:28:59 +03:00
}
type KeyboardMap map[KeyboardId] *Keyboard
2023-07-09 01:28:59 +03:00
// Return the new reply keyboard with rows as specified.
func NewKeyboard(rows ...ButtonRow) *Keyboard {
return &Keyboard{
Rows: rows,
2023-07-09 01:28:59 +03:00
}
}
// Convert the Keyboard to the Telegram API type.
func (kbd *Keyboard) ToTelegram() apix.ReplyKeyboardMarkup {
2023-07-09 01:28:59 +03:00
rows := [][]apix.KeyboardButton{}
for _, row := range kbd.Rows {
2023-07-09 01:28:59 +03:00
buttons := []apix.KeyboardButton{}
for _, button := range row {
buttons = append(buttons, button.ToTelegram())
2023-07-09 01:28:59 +03:00
}
rows = append(rows, buttons)
}
return apix.NewReplyKeyboard(rows...)
2023-07-09 01:28:59 +03:00
}
func (kbd *Keyboard) ToTelegramInline() apix.InlineKeyboardMarkup {
rows := [][]apix.InlineKeyboardButton{}
for _, row := range kbd.Rows {
buttons := []apix.InlineKeyboardButton{}
for _, button := range row {
buttons = append(buttons, button.ToTelegramInline())
}
rows = append(rows, buttons)
}
return apix.NewInlineKeyboardMarkup(rows...)
}
// Returns the map of buttons. Used to define the Action.
func (kbd *Keyboard) buttonMap() ButtonMap {
ret := make(ButtonMap)
for _, vi := range kbd.Rows {
for _, vj := range vi {
2023-07-12 14:06:05 +03:00
ret[vj.Key()] = vj
}
}
return ret
}
2023-07-09 01:28:59 +03:00