Skip to content

Commit

Permalink
bot: refactor Trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed May 28, 2024
1 parent e0c24df commit 4722793
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,22 +175,33 @@ var (
//
// b.Handle("/ban", onBan, middleware.Whitelist(ids...))
func (b *Bot) Handle(endpoint interface{}, h HandlerFunc, m ...MiddlewareFunc) {
end := extractEndpoint(endpoint)
if end == "" {
panic("telebot: unsupported endpoint")
}

if len(b.group.middleware) > 0 {
m = appendMiddleware(b.group.middleware, m)
}

handler := func(c Context) error {
b.handlers[end] = func(c Context) error {
return applyMiddleware(h, m...)(c)
}
}

switch end := endpoint.(type) {
case string:
b.handlers[end] = handler
case CallbackEndpoint:
b.handlers[end.CallbackUnique()] = handler
default:
panic("telebot: unsupported endpoint")
// Trigger executes the registered handler by the endpoint.
func (b *Bot) Trigger(endpoint interface{}, c Context) error {
end := extractEndpoint(endpoint)
if end == "" {
return fmt.Errorf("telebot: unsupported endpoint")
}

handler, ok := b.handlers[end]
if !ok {
return fmt.Errorf("telebot: no handler found for given endpoint")
}

return handler(c)
}

// Start brings bot into motion by consuming incoming
Expand Down Expand Up @@ -1261,24 +1272,12 @@ 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
)

func extractEndpoint(endpoint interface{}) string {
switch end := endpoint.(type) {
case string:
handler, ok = b.handlers[end]
return 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 end.CallbackUnique()
}

return handler(c)
return ""
}

0 comments on commit 4722793

Please sign in to comment.