2023-08-10 15:49:25 +03:00
|
|
|
package tx
|
2023-07-09 01:28:59 +03:00
|
|
|
|
|
|
|
import (
|
2023-08-10 15:49:25 +03:00
|
|
|
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
2023-07-09 01:28:59 +03:00
|
|
|
)
|
2023-08-10 15:49:25 +03:00
|
|
|
|
2023-07-09 01:28:59 +03:00
|
|
|
/*
|
|
|
|
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"),
|
|
|
|
),
|
|
|
|
)*/
|
|
|
|
|
2023-07-12 02:02:33 +03:00
|
|
|
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 {
|
2023-08-15 16:02:14 +03:00
|
|
|
// Text to be displayed with the keyboard.
|
|
|
|
Text string
|
|
|
|
// Rows to be displayed once the
|
|
|
|
// keyboard is sent.
|
2023-07-12 00:33:51 +03:00
|
|
|
Rows []ButtonRow
|
2023-08-15 16:02:14 +03:00
|
|
|
|
|
|
|
OneTime bool
|
|
|
|
Inline bool
|
2023-07-09 01:28:59 +03:00
|
|
|
}
|
|
|
|
|
2023-08-10 15:49:25 +03:00
|
|
|
type KeyboardMap map[KeyboardId]*Keyboard
|
2023-07-12 02:02:33 +03:00
|
|
|
|
2023-07-09 01:28:59 +03:00
|
|
|
// Return the new reply keyboard with rows as specified.
|
2023-08-15 16:02:14 +03:00
|
|
|
func NewKeyboard(text string) *Keyboard {
|
2023-07-09 01:28:59 +03:00
|
|
|
return &Keyboard{
|
2023-08-15 16:02:14 +03:00
|
|
|
Text: text,
|
2023-07-09 01:28:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 16:02:14 +03:00
|
|
|
func (kbd *Keyboard) TelegramMarkup() any {
|
|
|
|
if kbd.Inline {
|
|
|
|
return kbd.toTelegramInline()
|
|
|
|
}
|
|
|
|
|
|
|
|
return kbd.toTelegramReply()
|
|
|
|
}
|
|
|
|
|
2023-08-10 15:49:25 +03:00
|
|
|
// Adds a new button row to the current keyboard.
|
|
|
|
func (kbd *Keyboard) Row(btns ...*Button) *Keyboard {
|
2023-08-11 11:04:28 +03:00
|
|
|
// For empty row. We do not need that.
|
|
|
|
if len(btns) < 1 {
|
|
|
|
return kbd
|
|
|
|
}
|
2023-08-10 15:49:25 +03:00
|
|
|
kbd.Rows = append(kbd.Rows, btns)
|
|
|
|
return kbd
|
|
|
|
}
|
|
|
|
|
2023-08-15 16:02:14 +03:00
|
|
|
// Convert the Keyboard to the Telegram API type of reply keyboard.
|
|
|
|
func (kbd *Keyboard) toTelegramReply() apix.ReplyKeyboardMarkup {
|
2023-07-09 01:28:59 +03:00
|
|
|
rows := [][]apix.KeyboardButton{}
|
2023-07-12 00:33:51 +03:00
|
|
|
for _, row := range kbd.Rows {
|
2023-07-09 01:28:59 +03:00
|
|
|
buttons := []apix.KeyboardButton{}
|
|
|
|
for _, button := range row {
|
2023-07-12 02:02:33 +03:00
|
|
|
buttons = append(buttons, button.ToTelegram())
|
2023-07-09 01:28:59 +03:00
|
|
|
}
|
|
|
|
rows = append(rows, buttons)
|
2023-08-10 15:49:25 +03:00
|
|
|
}
|
|
|
|
|
2023-08-15 16:02:14 +03:00
|
|
|
if kbd.OneTime {
|
|
|
|
return apix.NewOneTimeReplyKeyboard(rows...)
|
|
|
|
}
|
|
|
|
|
2023-07-12 00:33:51 +03:00
|
|
|
return apix.NewReplyKeyboard(rows...)
|
2023-07-09 01:28:59 +03:00
|
|
|
}
|
|
|
|
|
2023-08-15 16:02:14 +03:00
|
|
|
func (kbd *Keyboard) toTelegramInline() apix.InlineKeyboardMarkup {
|
2023-07-12 02:02:33 +03:00
|
|
|
rows := [][]apix.InlineKeyboardButton{}
|
|
|
|
for _, row := range kbd.Rows {
|
|
|
|
buttons := []apix.InlineKeyboardButton{}
|
|
|
|
for _, button := range row {
|
|
|
|
buttons = append(buttons, button.ToTelegramInline())
|
|
|
|
}
|
|
|
|
rows = append(rows, buttons)
|
2023-08-10 15:49:25 +03:00
|
|
|
}
|
|
|
|
|
2023-07-12 02:02:33 +03:00
|
|
|
return apix.NewInlineKeyboardMarkup(rows...)
|
|
|
|
}
|
|
|
|
|
2023-08-15 16:02:14 +03:00
|
|
|
func (kbd *Keyboard) WithOneTime(oneTime bool) *Keyboard {
|
|
|
|
kbd.OneTime = oneTime
|
|
|
|
return kbd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (kbd *Keyboard) WithInline(inline bool) *Keyboard {
|
|
|
|
kbd.Inline = inline
|
|
|
|
return kbd
|
|
|
|
}
|
|
|
|
|
2023-07-12 00:33:51 +03:00
|
|
|
// 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
|
2023-07-12 00:33:51 +03:00
|
|
|
}
|
|
|
|
}
|
2023-08-10 15:49:25 +03:00
|
|
|
|
2023-07-12 00:33:51 +03:00
|
|
|
return ret
|
|
|
|
}
|