go-telegram / bot

Telegram Bot API Go framework
MIT License
502 stars 46 forks source link

Add support of handling different updates #60

Closed glebkin closed 4 months ago

glebkin commented 4 months ago

Currently, there's no way to handle updates, which does not contain Message or CallbackQuery:

func (b *Bot) ProcessUpdate(ctx context.Context, upd *models.Update) {
    h := b.defaultHandlerFunc

    defer func() {
        applyMiddlewares(h, b.middlewares...)(ctx, b, upd)
    }()

    if upd.Message != nil {
        h = b.findHandler(HandlerTypeMessageText, upd)
        return
    }
    if upd.CallbackQuery != nil {
        h = b.findHandler(HandlerTypeCallbackQueryData, upd)
        return
    }
}

Which is actually a problem, because you can't for example handle channel post updates.

negasus commented 4 months ago

For handle any updates, you can implement your logic in DefaultHandler

    opts := []bot.Option{
        bot.WithDefaultHandler(handler),
    }

    b, err := bot.New(os.Getenv("EXAMPLE_TELEGRAM_BOT_TOKEN"), opts...)

...

func handler(ctx context.Context, b *bot.Bot, update *models.Update) {
    if update.Message != nil {
        // handle message
        return
    }

    // ...

    if update.Poll != nil {
        // handle poll
    }

    // etc...
}
negasus commented 4 months ago

Released in #65