inline keyboard

This commit is contained in:
2026-01-21 15:53:30 +03:00
parent 684d56acba
commit ce13b19676
4 changed files with 88 additions and 14 deletions

43
bot.go
View File

@@ -291,13 +291,20 @@ func (b *Bot) handleMessage(update *Update, ctx *MsgContext) {
} }
func (b *Bot) handleCallback(update *Update, ctx *MsgContext) { func (b *Bot) handleCallback(update *Update, ctx *MsgContext) {
data := new(CallbackData)
err := json.Unmarshal([]byte(update.CallbackQuery.Data), data)
if err != nil {
b.logger.Error(err)
return
}
for _, plugin := range b.plugins { for _, plugin := range b.plugins {
for payload := range plugin.Payloads { _, ok := plugin.Payloads[data.Command]
if !strings.HasPrefix(update.CallbackQuery.Data, payload) { if !ok {
continue continue
}
go plugin.ExecutePayload(payload, ctx, b.dbContext)
} }
go plugin.ExecutePayload(data.Command, ctx, b.dbContext)
break
} }
} }
@@ -312,27 +319,39 @@ func (b *Bot) checkPrefixes(text string) (string, bool) {
type AnswerMessage struct { type AnswerMessage struct {
MessageID int MessageID int
Text string
IsMedia bool IsMedia bool
Keyboard *InlineKeyboard
ctx *MsgContext ctx *MsgContext
} }
func (ctx *MsgContext) Answer(text string) *AnswerMessage { func (ctx *MsgContext) answer(text string, keyboard *InlineKeyboard) *AnswerMessage {
msg, err := ctx.Bot.SendMessage(&SendMessageP{ params := &SendMessageP{
ChatID: ctx.Msg.Chat.ID, ChatID: ctx.Msg.Chat.ID,
Text: text, Text: text,
ParseMode: ParseMD, ParseMode: ParseMD,
}) }
if keyboard != nil {
params.ReplyMarkup = keyboard.Get()
}
msg, err := ctx.Bot.SendMessage(params)
if err != nil { if err != nil {
ctx.Bot.logger.Error(err) ctx.Bot.logger.Error(err)
return nil return nil
} }
return &AnswerMessage{ return &AnswerMessage{
MessageID: msg.MessageID, ctx: ctx, IsMedia: false, MessageID: msg.MessageID, ctx: ctx, IsMedia: false, Text: text,
} }
} }
func (ctx *MsgContext) Answer(text string) *AnswerMessage {
return ctx.answer(text, nil)
}
func (ctx *MsgContext) Answerf(template string, args ...any) *AnswerMessage { func (ctx *MsgContext) Answerf(template string, args ...any) *AnswerMessage {
test := fmt.Sprintf(template, args...) return ctx.answer(fmt.Sprintf(template, args...), nil)
return ctx.Answer(test) }
func (ctx *MsgContext) Keyboard(text string, kb *InlineKeyboard) *AnswerMessage {
return ctx.answer(text, kb)
} }
func (ctx *MsgContext) AnswerPhoto(photoId string, text string) *AnswerMessage { func (ctx *MsgContext) AnswerPhoto(photoId string, text string) *AnswerMessage {
@@ -346,7 +365,7 @@ func (ctx *MsgContext) AnswerPhoto(photoId string, text string) *AnswerMessage {
ctx.Bot.logger.Error(err) ctx.Bot.logger.Error(err)
} }
return &AnswerMessage{ return &AnswerMessage{
MessageID: ctx.Msg.MessageID, ctx: ctx, IsMedia: true, MessageID: ctx.Msg.MessageID, ctx: ctx, IsMedia: true, Text: text,
} }
} }

View File

@@ -1,4 +1,53 @@
package laniakea package laniakea
import "encoding/json"
type InlineKeyboard struct { type InlineKeyboard struct {
CurrentLine []InlineKeyboardButton
Lines [][]InlineKeyboardButton
}
func NewInlineKeyboard() *InlineKeyboard {
return &InlineKeyboard{
CurrentLine: make([]InlineKeyboardButton, 0),
Lines: make([][]InlineKeyboardButton, 0),
}
}
func (in *InlineKeyboard) append(button InlineKeyboardButton) *InlineKeyboard {
in.CurrentLine = append(in.CurrentLine, button)
return in
}
func (in *InlineKeyboard) AddUrlButton(text, url string) *InlineKeyboard {
return in.append(InlineKeyboardButton{Text: text, URL: url})
}
func (in *InlineKeyboard) AddCallbackButton(text string, data CallbackData) *InlineKeyboard {
return in.append(InlineKeyboardButton{Text: text, CallbackData: data.ToJson()})
}
func (in *InlineKeyboard) AddLine() *InlineKeyboard {
in.Lines = append(in.Lines, in.CurrentLine)
in.CurrentLine = make([]InlineKeyboardButton, 0)
return in
}
func (in *InlineKeyboard) Get() *InlineKeyboardMarkup {
if len(in.CurrentLine) > 0 {
in.Lines = append(in.Lines, in.CurrentLine)
}
return &InlineKeyboardMarkup{
InlineKeyboard: in.Lines,
}
}
type CallbackData struct {
Command string `json:"cmd"`
Args []any `json:"args"`
}
func (d *CallbackData) ToJson() string {
data, err := json.Marshal(d)
if err != nil {
return `{"cmd":""}`
}
return string(data)
} }

View File

@@ -62,7 +62,7 @@ type SendMessageP struct {
AllowPaidBroadcast bool `json:"allow_paid_broadcast,omitempty"` AllowPaidBroadcast bool `json:"allow_paid_broadcast,omitempty"`
MessageEffectID string `json:"message_effect_id,omitempty"` MessageEffectID string `json:"message_effect_id,omitempty"`
ReplyParameters *ReplyParameters `json:"reply_parameters,omitempty"` ReplyParameters *ReplyParameters `json:"reply_parameters,omitempty"`
InlineKeyboardMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"`
// ReplyKeyboardMarkup *ReplyKeyboardMarkup `json:"reply_markup,omitempty"` // ReplyKeyboardMarkup *ReplyKeyboardMarkup `json:"reply_markup,omitempty"`
} }

View File

@@ -43,6 +43,10 @@ type Chat struct {
IsForum bool `json:"is_forum,omitempty"` IsForum bool `json:"is_forum,omitempty"`
} }
type MessageReplyMarkup struct {
InlineKeyboard *InlineKeyboardMarkup `json:"inline_keyboard"`
}
type Message struct { type Message struct {
MessageID int `json:"message_id"` MessageID int `json:"message_id"`
MessageThreadID int `json:"message_thread_id,omitempty"` MessageThreadID int `json:"message_thread_id,omitempty"`
@@ -53,6 +57,8 @@ type Message struct {
Photo []*PhotoSize `json:"photo,omitempty"` Photo []*PhotoSize `json:"photo,omitempty"`
Caption string `json:"caption,omitempty"` Caption string `json:"caption,omitempty"`
ReplyToMessage *Message `json:"reply_to_message"` ReplyToMessage *Message `json:"reply_to_message"`
ReplyMarkup *MessageReplyMarkup `json:"reply_markup,omitempty"`
} }
type InaccessableMessage struct { type InaccessableMessage struct {
@@ -103,7 +109,7 @@ type LinkPreviewOptions struct {
} }
type InlineKeyboardMarkup struct { type InlineKeyboardMarkup struct {
InlineKeyboard [][]*InlineKeyboardButton `json:"inline_keyboard"` InlineKeyboard [][]InlineKeyboardButton `json:"inline_keyboard"`
} }
type InlineKeyboardButton struct { type InlineKeyboardButton struct {