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
|
|
|
)
|
|
|
|
|
|
|
|
// The type wraps Telegram API's button to provide Action functionality.
|
|
|
|
type Button struct {
|
2023-08-10 15:49:25 +03:00
|
|
|
Text string
|
|
|
|
Data string
|
|
|
|
Url string
|
2023-07-12 00:33:51 +03:00
|
|
|
Action Action
|
2023-07-09 01:28:59 +03:00
|
|
|
}
|
|
|
|
|
2023-08-10 15:49:25 +03:00
|
|
|
type ButtonMap map[string]*Button
|
2023-07-09 01:28:59 +03:00
|
|
|
|
|
|
|
// Represents the reply button row.
|
|
|
|
type ButtonRow []*Button
|
|
|
|
|
|
|
|
// Returns new button with specified text and action.
|
2023-08-10 15:49:25 +03:00
|
|
|
func NewButton() *Button {
|
|
|
|
return &Button{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (btn *Button) WithText(text string) *Button {
|
|
|
|
btn.Text = text
|
|
|
|
return btn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (btn *Button) WithUrl(url string) *Button {
|
|
|
|
btn.Url = url
|
|
|
|
return btn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (btn *Button) WithAction(a Action) *Button {
|
|
|
|
btn.Action = a
|
|
|
|
return btn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (btn *Button) ActionFunc(fn ActionFunc) *Button {
|
|
|
|
return btn.WithAction(fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (btn *Button) ScreenChange(sc ScreenChange) *Button {
|
|
|
|
return btn.WithAction(sc)
|
2023-07-09 01:28:59 +03:00
|
|
|
}
|
|
|
|
|
2023-07-12 02:02:33 +03:00
|
|
|
func (btn *Button) ToTelegram() apix.KeyboardButton {
|
|
|
|
return apix.NewKeyboardButton(btn.Text)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (btn *Button) ToTelegramInline() apix.InlineKeyboardButton {
|
|
|
|
if btn.Data != "" {
|
|
|
|
return apix.NewInlineKeyboardButtonData(btn.Text, btn.Data)
|
|
|
|
}
|
2023-08-10 15:49:25 +03:00
|
|
|
|
2023-07-12 02:02:33 +03:00
|
|
|
if btn.Url != "" {
|
|
|
|
return apix.NewInlineKeyboardButtonURL(btn.Text, btn.Url)
|
|
|
|
}
|
2023-08-10 15:49:25 +03:00
|
|
|
|
2023-07-12 02:02:33 +03:00
|
|
|
// If no match then return the data one with data the same as the text.
|
|
|
|
return apix.NewInlineKeyboardButtonData(btn.Text, btn.Text)
|
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:05 +03:00
|
|
|
// Return the key of the button to identify it by messages and callbacks.
|
|
|
|
func (btn *Button) Key() string {
|
|
|
|
if btn.Data != "" {
|
|
|
|
return btn.Data
|
|
|
|
}
|
2023-08-10 15:49:25 +03:00
|
|
|
|
2023-07-12 14:06:05 +03:00
|
|
|
// If no match then return the data one with data the same as the text.
|
|
|
|
return btn.Text
|
|
|
|
}
|
2023-07-12 02:02:33 +03:00
|
|
|
|
2023-07-09 01:28:59 +03:00
|
|
|
func NewButtonRow(btns ...*Button) ButtonRow {
|
|
|
|
return btns
|
|
|
|
}
|