From dd0307e382fa9c6f6c6fc5963c7dd26558b317d1 Mon Sep 17 00:00:00 2001 From: nefrace Date: Thu, 13 Apr 2023 11:14:37 +0300 Subject: [PATCH] Constructors for keyboard and options, CallbackHandler --- bot.go | 6 ++++++ dispatcher.go | 18 ++++++++++++++++++ keyboards.go | 4 ++++ options.go | 4 ++++ 4 files changed, 32 insertions(+) diff --git a/bot.go b/bot.go index f1e11c1..3854b2b 100644 --- a/bot.go +++ b/bot.go @@ -36,5 +36,11 @@ func (b *bot) Update(u *echo.Update) { b.lock.Lock() defer b.lock.Unlock() + // defer func() { + // if r := recover(); r != nil { + // log.Printf("[%s] Recovered. Error: %s\n", upd.UpdateID.String(), r) + // } + // }() + b.handler(upd) } diff --git a/dispatcher.go b/dispatcher.go index 8f1807c..23d9444 100644 --- a/dispatcher.go +++ b/dispatcher.go @@ -2,6 +2,8 @@ package nechotron import ( "strings" + + "github.com/NicoNex/echotron/v3" ) type UpdateHandler func(u *Update) error @@ -52,6 +54,22 @@ func (d *Dispatcher) HandleFilter(filter FilterFn, handler UpdateHandler) *Dispa return d } +func (d *Dispatcher) HandleCallback(callback string, handler UpdateHandler) *Dispatcher { + newHandler := func(u *Update) (bool, error) { + if !u.IsCallback() { + return false, nil + } + defer u.Bot.AnswerCallbackQuery(u.CallbackQuery.ID, &echotron.CallbackQueryOptions{}) + if u.CallbackQuery.Data != callback { + return false, nil + } + err := handler(u) + return true, err + } + d.handlers = append(d.handlers, newHandler) + return d +} + func (d *Dispatcher) HandleReply(handler UpdateHandler) *Dispatcher { newHandler := func(u *Update) (bool, error) { if !u.IsMessage() { diff --git a/keyboards.go b/keyboards.go index e6228ca..23701be 100644 --- a/keyboards.go +++ b/keyboards.go @@ -6,6 +6,10 @@ type InKeyboard struct { Buttons [][]echotron.InlineKeyboardButton } +func NewInlineKeyboard() *InKeyboard { + return &InKeyboard{} +} + func (i *InKeyboard) Row(buttons ...echotron.InlineKeyboardButton) *InKeyboard { i.Buttons = append(i.Buttons, buttons) return i diff --git a/options.go b/options.go index 288837e..fd83c6b 100644 --- a/options.go +++ b/options.go @@ -16,6 +16,10 @@ type OptionsBuilder struct { hasSpoiler bool } +func NewOptions() *OptionsBuilder { + return &OptionsBuilder{} +} + func (o *OptionsBuilder) Caption(text string) *OptionsBuilder { o.caption = text return o