v0.3.6; answerCallbackQuery

This commit is contained in:
2026-02-03 14:51:57 +03:00
parent 0921d306fd
commit 90e2f38c18
4 changed files with 65 additions and 19 deletions

View File

@@ -57,6 +57,7 @@ func (b *Bot) handleCallback(update *Update, ctx *MsgContext) {
ctx.From = update.CallbackQuery.From
ctx.Msg = update.CallbackQuery.Message
ctx.CallbackMsgId = update.CallbackQuery.Message.MessageID
ctx.CallbackQueryId = update.CallbackQuery.ID
ctx.Args = data.Args
for _, plugin := range b.plugins {

View File

@@ -138,3 +138,20 @@ func (b *Bot) DeleteMessage(params *DeleteMessageP) (bool, error) {
}
return *ok, err
}
type AnswerCallbackQueryP struct {
CallbackQueryID string `json:"callback_query_id"`
Text string `json:"text,omitempty"`
ShowAlert bool `json:"show_alert,omitempty"`
URL string `json:"url,omitempty"`
CacheTime int `json:"cache_time,omitempty"`
}
func (b *Bot) AnswerCallbackQuery(params *AnswerCallbackQueryP) (bool, error) {
req := NewRequest[bool]("answerCallbackQuery", params)
ok, err := req.Do(b)
if err != nil {
return false, err
}
return *ok, err
}

View File

@@ -8,6 +8,7 @@ type MsgContext struct {
Update *Update
From *User
CallbackMsgId int
CallbackQueryId string
FromID int
Prefix string
Text string
@@ -151,14 +152,41 @@ func (ctx *MsgContext) CallbackDelete() {
ctx.delete(ctx.CallbackMsgId)
}
func (ctx *MsgContext) Error(err error) {
_, sendErr := ctx.Bot.SendMessage(&SendMessageP{
ChatID: ctx.Msg.Chat.ID,
Text: fmt.Sprintf(ctx.Bot.errorTemplate, EscapeMarkdown(err.Error())),
func (ctx *MsgContext) answerCallbackQuery(url, text string, showAlert bool) {
if len(ctx.CallbackQueryId) == 0 {
return
}
_, err := ctx.Bot.AnswerCallbackQuery(&AnswerCallbackQueryP{
CallbackQueryID: ctx.CallbackQueryId,
Text: text, ShowAlert: showAlert, URL: url,
})
if err != nil {
ctx.Bot.logger.Errorln(err)
if sendErr != nil {
ctx.Bot.logger.Errorln(sendErr)
}
}
func (ctx *MsgContext) AnswerCbQuery() {
ctx.answerCallbackQuery("", "", false)
}
func (ctx *MsgContext) AnswerCbQueryText(text string) {
ctx.answerCallbackQuery("", text, false)
}
func (ctx *MsgContext) AnswerCbQueryAlert(text string) {
ctx.answerCallbackQuery("", text, true)
}
func (ctx *MsgContext) AnswerCbQueryUrl(u string) {
ctx.answerCallbackQuery(u, "", false)
}
func (ctx *MsgContext) error(err error) {
text := fmt.Sprintf(ctx.Bot.errorTemplate, EscapeMarkdown(err.Error()))
if ctx.CallbackQueryId != "" {
ctx.answerCallbackQuery("", text, false)
} else {
ctx.answer(text, nil)
}
ctx.Bot.logger.Errorln(err)
}
func (ctx *MsgContext) Error(err error) {
ctx.error(err)
}

View File

@@ -1,8 +1,8 @@
package laniakea
const (
VersionString = "0.3.2"
VersionString = "0.3.6"
VersionMajor = 0
VersionMinor = 3
VersionPatch = 2
VersionPatch = 6
)