Home / Docs / Middlewares
On this page

Middlewares

Los middlewares se ejecutan antes de los handlers. Ideales para logging, auth y error handling.

Logger

bot.use(async (ctx, next) => {
    console.log(`Update: ${ctx.update}`);
    await next();
});

Auth

bot.use(async (ctx, next) => {
    const allowed = [123456789, 987654321];
    if (!allowed.includes(ctx.userId)) {
        return ctx.reply('No autorizado');
    }
    await next();
});

Error Handler

bot.use(async (ctx, next) => {
    try {
        await next();
    } catch (e) {
        console.error(e);
        await ctx.reply('Error');
    }
});