Skip to content

Commit

Permalink
bot: implement Trigger function (tucnak#687)
Browse files Browse the repository at this point in the history
* bot: trigger init

* bot: add execution of registered handlers in trigger func
  • Loading branch information
Nash-Well authored May 27, 2024
1 parent a26ba9f commit 77f77a6
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -1260,3 +1260,25 @@ func (b *Bot) botInfo(language, key string) (*BotInfo, error) {
}
return resp.Result, nil
}

// Trigger executes the registered handler by the endpoint.
func (b *Bot) Trigger(endpoint any, c Context) error {
var (
ok bool
handler HandlerFunc
)

switch end := endpoint.(type) {
case string:
handler, ok = b.handlers[end]
case CallbackEndpoint:
handler, ok = b.handlers[end.CallbackUnique()]
default:
return fmt.Errorf("telebot: unsupported endpoint")
}
if !ok {
return fmt.Errorf("telebot: no handler registered for provided endpoint")
}

return handler(c)
}

0 comments on commit 77f77a6

Please sign in to comment.