tg/src/behx/button.go

75 lines
1.5 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"
)
// The type wraps Telegram API's button to provide Action functionality.
type Button struct {
Text string
Data string
Url string
Action Action
2023-07-09 01:28:59 +03:00
}
type ButtonMap map[string] *Button
// Represents the reply button row.
type ButtonRow []*Button
// Returns new button with specified text and action.
func NewButton(text string, action Action) *Button {
return &Button{
Text: text,
2023-07-09 01:28:59 +03:00
Action: action,
}
}
func NewButtonData(text string, data string, action Action) *Button {
return &Button{
Text: text,
Data: data,
Action: action,
}
}
func NewButtonUrl(text string, url string, action Action) *Button {
return &Button{
Text: text,
Url: url,
Action: action,
}
}
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)
}
if btn.Url != "" {
return apix.NewInlineKeyboardButtonURL(btn.Text, btn.Url)
}
// 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
}
// If no match then return the data one with data the same as the text.
return btn.Text
}
2023-07-09 01:28:59 +03:00
func NewButtonRow(btns ...*Button) ButtonRow {
return btns
}