2023-09-25 19:52:57 +03:00
|
|
|
package tg
|
|
|
|
|
|
|
|
import (
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
|
|
)
|
|
|
|
|
|
|
|
// The type represents reply keyboards.
|
|
|
|
type Reply struct {
|
|
|
|
*Keyboard
|
|
|
|
// If true will be removed after one press.
|
|
|
|
OneTime bool
|
|
|
|
// If true will remove the keyboard on send.
|
|
|
|
Remove bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set if we should remove current keyboard on the user side
|
|
|
|
// when sending the keyboard.
|
|
|
|
func (kbd *Reply) WithRemove(remove bool) *Reply {
|
|
|
|
kbd.Remove = remove
|
|
|
|
return kbd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set if the keyboard should be hidden after
|
|
|
|
// one of buttons is pressede.
|
|
|
|
func (kbd *Reply) WithOneTime(oneTime bool) *Reply{
|
|
|
|
kbd.OneTime = oneTime
|
|
|
|
return kbd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the Keyboard to the Telegram API type of reply keyboard.
|
|
|
|
func (kbd *Reply) ToApi() any {
|
|
|
|
// Shades everything.
|
|
|
|
if kbd.Remove {
|
|
|
|
return tgbotapi.NewRemoveKeyboard(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows := [][]tgbotapi.KeyboardButton{}
|
|
|
|
for _, row := range kbd.Rows {
|
|
|
|
buttons := []tgbotapi.KeyboardButton{}
|
|
|
|
for _, button := range row {
|
|
|
|
buttons = append(buttons, button.ToTelegram())
|
|
|
|
}
|
|
|
|
rows = append(rows, buttons)
|
|
|
|
}
|
|
|
|
|
|
|
|
if kbd.OneTime {
|
|
|
|
return tgbotapi.NewOneTimeReplyKeyboard(rows...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tgbotapi.NewReplyKeyboard(rows...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The type implements reply keyboard widget.
|
2023-09-25 23:43:22 +03:00
|
|
|
type ReplyCompo struct {
|
2023-09-29 13:36:37 +03:00
|
|
|
*MessageCompo
|
2023-09-25 19:52:57 +03:00
|
|
|
*Reply
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implementing the sendable interface.
|
2023-09-27 14:09:49 +03:00
|
|
|
func (compo *ReplyCompo) SendConfig(
|
2023-10-10 12:42:05 +03:00
|
|
|
sid SessionId, bot *Bot,
|
2023-09-25 19:52:57 +03:00
|
|
|
) (*SendConfig) {
|
2023-10-10 12:42:05 +03:00
|
|
|
sendConfig := compo.MessageCompo.SendConfig(sid, bot)
|
2023-09-29 13:36:37 +03:00
|
|
|
sendConfig.Message.ReplyMarkup = compo.Reply.ToApi()
|
|
|
|
return sendConfig
|
2023-09-25 19:52:57 +03:00
|
|
|
}
|
|
|
|
|
2023-09-25 23:43:22 +03:00
|
|
|
func (compo *ReplyCompo) Filter(
|
2023-09-25 19:52:57 +03:00
|
|
|
u *Update,
|
|
|
|
) bool {
|
2023-09-25 23:43:22 +03:00
|
|
|
if compo == nil || u.Message == nil {
|
2023-09-25 19:52:57 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-09-27 14:09:49 +03:00
|
|
|
_, ok := compo.ButtonMap()[u.Message.Text]
|
2023-09-25 19:52:57 +03:00
|
|
|
if !ok {
|
|
|
|
if u.Message.Location != nil {
|
2023-09-27 14:09:49 +03:00
|
|
|
locBtn := compo.ButtonMap().LocationButton()
|
2023-09-25 19:52:57 +03:00
|
|
|
if locBtn == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-09-26 17:13:31 +03:00
|
|
|
// Implementing the UI interface.
|
2023-09-25 23:43:22 +03:00
|
|
|
func (compo *ReplyCompo) Serve(c *Context) {
|
2023-09-25 19:52:57 +03:00
|
|
|
for u := range c.Input() {
|
|
|
|
var btn *Button
|
|
|
|
text := u.Message.Text
|
2023-09-27 14:09:49 +03:00
|
|
|
btns := compo.ButtonMap()
|
2023-09-25 19:52:57 +03:00
|
|
|
|
|
|
|
btn, ok := btns[text]
|
|
|
|
if !ok {
|
|
|
|
if u.Message.Location != nil {
|
|
|
|
btn = btns.LocationButton()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if btn != nil {
|
2023-09-27 14:09:49 +03:00
|
|
|
c.WithUpdate(u).Run(btn.Action)
|
2023-09-25 19:52:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|