Skip to content

Commit

Permalink
bot,context: implement ProcessContext, NewContext
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Aug 8, 2024
1 parent 8b8010b commit 074ee91
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
5 changes: 1 addition & 4 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 7 additions & 1 deletion update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 074ee91

Please sign in to comment.