From fc093be58d51f78e6cdf30910b4247d73cc1925b Mon Sep 17 00:00:00 2001 From: nash Date: Tue, 27 Feb 2024 15:43:24 +0200 Subject: [PATCH 1/2] context: implement RespondText,RespondAlert methods --- context.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/context.go b/context.go index 3306c8f7..ba6b7dd5 100644 --- a/context.go +++ b/context.go @@ -152,6 +152,12 @@ type Context interface { // See Respond from bot.go. Respond(resp ...*CallbackResponse) error + // RespondText sends a popup response for the current callback query. + RespondText(t string) error + + // RespondAlert sends an alert response for the current callback query. + RespondAlert(t string) error + // Get retrieves data from the context. Get(key string) interface{} @@ -481,6 +487,14 @@ func (c *nativeContext) Respond(resp ...*CallbackResponse) error { return c.b.Respond(c.u.Callback, resp...) } +func (c *nativeContext) RespondText(t string) error { + return c.respondTextAlert(t) +} + +func (c *nativeContext) RespondAlert(t string) error { + return c.respondTextAlert(t, true) +} + func (c *nativeContext) Answer(resp *QueryResponse) error { if c.u.Query == nil { return errors.New("telebot: context inline query is nil") @@ -503,3 +517,13 @@ func (c *nativeContext) Get(key string) interface{} { defer c.lock.RUnlock() return c.store[key] } + +func (c *nativeContext) respondTextAlert(text string, show ...bool) error { + if c.u.Callback == nil { + return errors.New("telebot: context callback is nil") + } + return c.b.Respond(c.u.Callback, &CallbackResponse{ + Text: text, + ShowAlert: len(show) > 0 && show[0], + }) +} From 5c1b27a332917d94d0d176f928f8d993e65e9faa Mon Sep 17 00:00:00 2001 From: nash Date: Wed, 28 Feb 2024 18:34:00 +0200 Subject: [PATCH 2/2] context: refactor --- context.go | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/context.go b/context.go index ba6b7dd5..20e5bb85 100644 --- a/context.go +++ b/context.go @@ -153,10 +153,10 @@ type Context interface { Respond(resp ...*CallbackResponse) error // RespondText sends a popup response for the current callback query. - RespondText(t string) error + RespondText(text string) error // RespondAlert sends an alert response for the current callback query. - RespondAlert(t string) error + RespondAlert(text string) error // Get retrieves data from the context. Get(key string) interface{} @@ -487,12 +487,12 @@ func (c *nativeContext) Respond(resp ...*CallbackResponse) error { return c.b.Respond(c.u.Callback, resp...) } -func (c *nativeContext) RespondText(t string) error { - return c.respondTextAlert(t) +func (c *nativeContext) RespondText(text string) error { + return c.Respond(&CallbackResponse{Text: text}) } -func (c *nativeContext) RespondAlert(t string) error { - return c.respondTextAlert(t, true) +func (c *nativeContext) RespondAlert(text string) error { + return c.Respond(&CallbackResponse{Text: text, ShowAlert: true}) } func (c *nativeContext) Answer(resp *QueryResponse) error { @@ -517,13 +517,3 @@ func (c *nativeContext) Get(key string) interface{} { defer c.lock.RUnlock() return c.store[key] } - -func (c *nativeContext) respondTextAlert(text string, show ...bool) error { - if c.u.Callback == nil { - return errors.New("telebot: context callback is nil") - } - return c.b.Respond(c.u.Callback, &CallbackResponse{ - Text: text, - ShowAlert: len(show) > 0 && show[0], - }) -}