From 074ee91ad12765ee2ccbb5abe0f6312bfcffce7f Mon Sep 17 00:00:00 2001 From: Demian Date: Fri, 9 Aug 2024 01:13:12 +0200 Subject: [PATCH] bot,context: implement ProcessContext, NewContext --- bot.go | 5 +---- context.go | 9 +++++++++ update.go | 8 +++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/bot.go b/bot.go index 72406a20..4ba3eac3 100644 --- a/bot.go +++ b/bot.go @@ -266,10 +266,7 @@ func (b *Bot) NewMarkup() *ReplyMarkup { // NewContext returns a new native context object, // field by the passed update. func (b *Bot) NewContext(u Update) Context { - return &nativeContext{ - b: b, - u: u, - } + return NewContext(b, u) } // Send accepts 2+ arguments, starting with destination chat, followed by diff --git a/context.go b/context.go index 815b862e..74f45aba 100644 --- a/context.go +++ b/context.go @@ -11,6 +11,15 @@ import ( // used to handle actual endpoints. type HandlerFunc func(Context) error +// NewContext returns a new native context object, +// field by the passed update. +func NewContext(b API, u Update) Context { + return &nativeContext{ + b: b, + u: u, + } +} + // Context wraps an update and represents the context of current event. type Context interface { // Bot returns the bot instance. diff --git a/update.go b/update.go index 63396dd4..fed3aa09 100644 --- a/update.go +++ b/update.go @@ -29,7 +29,13 @@ type Update struct { // ProcessUpdate processes a single incoming update. // A started bot calls this function automatically. func (b *Bot) ProcessUpdate(u Update) { - c := b.NewContext(u) + b.ProcessContext(b.NewContext(u)) +} + +// ProcessContext processes the given context. +// A started bot calls this function automatically. +func (b *Bot) ProcessContext(c Context) { + u := c.Update() if u.Message != nil { m := u.Message