go-telegram / bot

Telegram Bot API Go framework
MIT License
674 stars 60 forks source link

Looks like RegisterHandlerMatchFunc does not work properly #45

Closed archimed-shaman closed 10 months ago

archimed-shaman commented 11 months ago

Hi, I have the following function to handle unsubscriptions:

func registerOnMyChatMember(api *bot.Bot, h bot.HandlerFunc) {
    api.RegisterHandlerMatchFunc(func(update *models.Update) bool {
        return update.MyChatMember != nil
    }, h)
}

However, it doesn't work with the active default handler, because this check isn't even called.

    opts := []bot.Option{
        bot.WithDefaultHandler(func(ctx context.Context, api *bot.Bot, update *models.Update) {
            defaultHandler(ctx, update, workers, loc)
        }),
        bot.WithMiddlewares(IgnoreChats, IgnoreBots, AutoRespond),
    }

    api, err := bot.New(token, opts...)

I'm not sure why. Maybe it's because RegisterHandlerMatchFunc does not set the handlerType field. Maybe it's because findHandler is called by bot.ProcessUpdate only for non-empty Message and CallbackQuery, not for MyChatMember. Or maybe I'm doing something wrong.

To reproduce this, create a bot instance with a default handler, register any function with RegisterHandlerMatchFunc, and try to handle "Delete and block".

negasus commented 10 months ago

Added example handler_match_func