2023-08-19 09:12:26 +03:00
|
|
|
package tg
|
2023-07-09 01:28:59 +03:00
|
|
|
|
|
|
|
import (
|
2023-09-25 19:52:57 +03:00
|
|
|
//tgbotapi "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-09-08 17:37:32 +03:00
|
|
|
// The general keyboard type used both in Reply and Inline.
|
2023-07-09 01:28:59 +03:00
|
|
|
type Keyboard struct {
|
2023-09-09 07:28:06 +03:00
|
|
|
// The action is called if there is no
|
|
|
|
// defined action for the button.
|
2023-09-19 14:38:57 +03:00
|
|
|
Action Action
|
2023-07-12 00:33:51 +03:00
|
|
|
Rows []ButtonRow
|
2023-09-08 17:37:32 +03:00
|
|
|
}
|
2023-08-15 16:02:14 +03:00
|
|
|
|
2023-09-19 14:38:57 +03:00
|
|
|
// Returns the new keyboard with specified rows.
|
2024-03-28 10:41:09 +03:00
|
|
|
func NewKeyboard(rows ...ButtonRow) Keyboard {
|
2024-03-29 14:30:48 +03:00
|
|
|
ret := Keyboard{}
|
2023-10-10 12:42:05 +03:00
|
|
|
for _, row := range rows {
|
|
|
|
if row != nil && len(row) > 0 {
|
|
|
|
ret.Rows = append(ret.Rows, row)
|
|
|
|
}
|
|
|
|
}
|
2023-09-08 17:37:32 +03:00
|
|
|
return ret
|
2023-08-15 16:02:14 +03:00
|
|
|
}
|
|
|
|
|
2024-03-28 10:41:09 +03:00
|
|
|
func (kbd Keyboard) RowNum() int {
|
2023-12-12 19:32:30 +03:00
|
|
|
return len(kbd.Rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (kbd *Keyboard) RemoveRow(i int) {
|
|
|
|
if i<0 || i > len(kbd.Rows) - 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
kbd.Rows = append(kbd.Rows[:i], kbd.Rows[i+1:]...)
|
|
|
|
}
|
|
|
|
|
2023-08-10 15:49:25 +03:00
|
|
|
// Adds a new button row to the current keyboard.
|
2024-03-28 10:41:09 +03:00
|
|
|
func (kbd Keyboard) Row(btns ...Button) Keyboard {
|
2023-09-08 17:37:32 +03:00
|
|
|
// For empty row. We do not need that.
|
|
|
|
if len(btns) < 1 {
|
|
|
|
return kbd
|
|
|
|
}
|
2024-03-29 14:30:48 +03:00
|
|
|
|
|
|
|
retBtns := make([]Button, 0, len(btns))
|
2023-10-07 13:20:49 +03:00
|
|
|
for _, btn := range btns {
|
2024-03-29 14:30:48 +03:00
|
|
|
if !btn.Valid {
|
2023-10-07 13:20:49 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
retBtns = append(retBtns, btn)
|
|
|
|
}
|
2023-10-10 12:42:05 +03:00
|
|
|
// Add only if there is something to add.
|
|
|
|
if len(retBtns) > 0 {
|
|
|
|
kbd.Rows = append(kbd.Rows, retBtns)
|
|
|
|
}
|
2023-09-08 17:37:32 +03:00
|
|
|
return kbd
|
|
|
|
}
|
2023-09-09 07:28:06 +03:00
|
|
|
|
2023-10-02 21:45:21 +03:00
|
|
|
// Adds buttons as one column list.
|
2024-03-28 10:41:09 +03:00
|
|
|
func (kbd Keyboard) List(btns ...Button) Keyboard {
|
2023-10-02 21:45:21 +03:00
|
|
|
for _, btn := range btns {
|
2024-03-29 14:30:48 +03:00
|
|
|
if !btn.Valid {
|
2023-10-07 13:20:49 +03:00
|
|
|
continue
|
|
|
|
}
|
2023-10-02 21:45:21 +03:00
|
|
|
kbd.Rows = append(kbd.Rows, ButtonRow{btn})
|
|
|
|
}
|
|
|
|
return kbd
|
|
|
|
}
|
|
|
|
|
2023-09-19 14:38:57 +03:00
|
|
|
// Set the default action when no button provides
|
|
|
|
// key to the data we got.
|
2024-03-28 10:41:09 +03:00
|
|
|
func (kbd Keyboard) WithAction(a Action) Keyboard {
|
2023-09-19 14:38:57 +03:00
|
|
|
kbd.Action = a
|
2023-09-09 07:28:06 +03:00
|
|
|
return kbd
|
|
|
|
}
|
|
|
|
|
2024-03-28 10:41:09 +03:00
|
|
|
// Returns the map of buttons. Where the key
|
|
|
|
// is button data and the value is Action.
|
2023-09-19 14:38:57 +03:00
|
|
|
func (kbd Keyboard) ButtonMap() ButtonMap {
|
2024-03-29 14:30:48 +03:00
|
|
|
return kbd.MakeButtonMap()
|
2023-12-19 22:35:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the map of buttons on the most fresh version of the keyboard.
|
|
|
|
func (kbd Keyboard) MakeButtonMap() ButtonMap {
|
2023-09-19 14:38:57 +03:00
|
|
|
ret := make(ButtonMap)
|
|
|
|
for _, vi := range kbd.Rows {
|
|
|
|
for _, vj := range vi {
|
|
|
|
ret[vj.Key()] = vj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-16 14:34:17 +03:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2023-09-19 14:38:57 +03:00
|
|
|
// Convert the keyboard to the more specific inline one.
|
2024-03-28 10:41:09 +03:00
|
|
|
func (kbd Keyboard) Inline() Inline {
|
|
|
|
ret := Inline{}
|
2023-09-19 14:38:57 +03:00
|
|
|
ret.Keyboard = kbd
|
|
|
|
return ret
|
2023-08-10 15:49:25 +03:00
|
|
|
}
|
|
|
|
|
2023-09-25 19:52:57 +03:00
|
|
|
// Convert the keyboard to the more specific reply one.
|
2024-03-29 14:30:48 +03:00
|
|
|
// By default OneTime = true.
|
2024-03-28 10:41:09 +03:00
|
|
|
func (kbd Keyboard) Reply() Reply {
|
2024-03-29 14:30:48 +03:00
|
|
|
ret := Reply{}
|
2023-09-19 14:38:57 +03:00
|
|
|
ret.Keyboard = kbd
|
2023-10-07 13:20:49 +03:00
|
|
|
// it is used more often than not once.
|
|
|
|
ret.OneTime = true
|
2023-09-19 14:38:57 +03:00
|
|
|
return ret
|
2023-09-09 07:28:06 +03:00
|
|
|
}
|
|
|
|
|