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 keyboard to be emdedded into the messages (inline in Telegram terms).
|
|
|
|
type Inline struct {
|
|
|
|
*Keyboard
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the inline keyboard to markup for the tgbotapi.
|
|
|
|
func (kbd *Inline) ToApi() tgbotapi.InlineKeyboardMarkup {
|
|
|
|
rows := [][]tgbotapi.InlineKeyboardButton{}
|
|
|
|
for _, row := range kbd.Rows {
|
2023-12-12 19:32:30 +03:00
|
|
|
if row == nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-09-25 19:52:57 +03:00
|
|
|
buttons := []tgbotapi.InlineKeyboardButton{}
|
|
|
|
for _, button := range row {
|
2023-12-12 19:32:30 +03:00
|
|
|
if button == nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-09-25 19:52:57 +03:00
|
|
|
buttons = append(buttons, button.ToTelegramInline())
|
|
|
|
}
|
|
|
|
rows = append(rows, buttons)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tgbotapi.NewInlineKeyboardMarkup(rows...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The type implements message with an inline keyboard.
|
2023-09-25 23:43:22 +03:00
|
|
|
type InlineCompo struct {
|
2023-09-29 13:36:37 +03:00
|
|
|
*MessageCompo
|
2023-09-25 19:52:57 +03:00
|
|
|
*Inline
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implementing the Sendable interface.
|
2023-09-29 13:36:37 +03:00
|
|
|
func (compo *InlineCompo) 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-12-19 22:35:57 +03:00
|
|
|
if len(compo.Inline.Rows) > 0 {
|
|
|
|
sendConfig.Message.ReplyMarkup = compo.Inline.ToApi()
|
|
|
|
}
|
2023-09-29 13:36:37 +03:00
|
|
|
|
|
|
|
return sendConfig
|
|
|
|
}
|
|
|
|
|
2023-12-19 22:35:57 +03:00
|
|
|
// Update the component on the client side.
|
2023-09-29 14:53:52 +03:00
|
|
|
func (compo *InlineCompo) Update(c *Context) {
|
2023-12-19 22:35:57 +03:00
|
|
|
if compo.Message != nil {
|
|
|
|
var edit tgbotapi.Chattable
|
|
|
|
markup := compo.Inline.ToApi()
|
|
|
|
ln := len(markup.InlineKeyboard)
|
|
|
|
if ln == 0 || compo.Inline.Rows == nil {
|
|
|
|
edit = tgbotapi.NewEditMessageText(
|
|
|
|
c.Session.Id.ToApi(),
|
|
|
|
compo.Message.MessageID,
|
|
|
|
compo.Text,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
edit = tgbotapi.NewEditMessageTextAndMarkup(
|
|
|
|
c.Session.Id.ToApi(),
|
|
|
|
compo.Message.MessageID,
|
|
|
|
compo.Text,
|
|
|
|
markup,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
msg, _ := c.Bot.Api.Send(edit)
|
|
|
|
compo.Message = &msg
|
2023-12-12 19:32:30 +03:00
|
|
|
}
|
2023-12-19 22:35:57 +03:00
|
|
|
compo.buttonMap = compo.MakeButtonMap()
|
2023-09-29 14:53:52 +03:00
|
|
|
}
|
|
|
|
|
2023-09-29 13:36:37 +03:00
|
|
|
// Implementing the Filterer interface.
|
|
|
|
func (compo *InlineCompo) Filter(u *Update) bool {
|
|
|
|
if compo == nil || u.CallbackQuery == nil {
|
|
|
|
return true
|
2023-09-25 19:52:57 +03:00
|
|
|
}
|
|
|
|
|
2023-09-29 13:36:37 +03:00
|
|
|
if u.CallbackQuery.Message.MessageID !=
|
|
|
|
compo.Message.MessageID {
|
|
|
|
return true
|
|
|
|
}
|
2023-09-25 19:52:57 +03:00
|
|
|
|
2023-09-29 13:36:37 +03:00
|
|
|
return false
|
2023-09-25 19:52:57 +03:00
|
|
|
}
|
|
|
|
|
2023-09-26 17:13:31 +03:00
|
|
|
// Implementing the Server interface.
|
2023-12-19 22:35:57 +03:00
|
|
|
func (compo *InlineCompo) Serve(c *Context) {
|
2023-09-25 19:52:57 +03:00
|
|
|
for u := range c.Input() {
|
2023-12-19 22:35:57 +03:00
|
|
|
compo.OnOneUpdate(c, u)
|
2023-09-25 19:52:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-19 22:35:57 +03:00
|
|
|
func (compo *InlineCompo) OnOneUpdate(c *Context, u *Update) {
|
|
|
|
var act Action
|
|
|
|
btns := compo.ButtonMap()
|
|
|
|
cb := tgbotapi.NewCallback(
|
|
|
|
u.CallbackQuery.ID,
|
|
|
|
u.CallbackQuery.Data,
|
|
|
|
)
|
|
|
|
data := u.CallbackQuery.Data
|
|
|
|
|
|
|
|
_, err := c.Bot.Api.Request(cb)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
btn, ok := btns[data]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if btn != nil {
|
|
|
|
act = btn.Action
|
|
|
|
} else if compo.Action != nil {
|
|
|
|
act = compo.Action
|
|
|
|
}
|
|
|
|
c.WithUpdate(u).Run(act)
|
|
|
|
}
|
2023-09-25 19:52:57 +03:00
|
|
|
|