Nestgram
  • ⭐About Nestgram
  • 📄Guide
  • 📰News
  • 🔼Handling updates
    • Handling commands
    • Handling text messages
    • Handling other updates
    • Entities: url, email and other
    • Other arguments in handler
    • Handling media files and download it
  • 🪶Nestgram Features
    • Services
    • Middlewares and Params
    • Sending messages correctly
    • Answer class
    • Scopes
    • States
    • Views
  • 💬Messages
    • Sending a photo, video and other media
    • Sending a media group
    • Send location (live) or venue
    • Send contact
    • Send poll
    • Send dice
    • Edit/delete messages
    • Copy or Forward a message
  • ⌨️Keyboards
    • Keyboard types, building keyboard
    • Handle underTheMessage keyboard button click by Alert or Toast
    • Keyboard layouts
  • ⚙️Config
    • CLI
    • Webhooks and run config
    • Api class
    • Modules
      • Mongo module
    • Controller Helper class
  • 🤖API Reference
    • Set chat action
    • Save user profile photo
    • Ban, unban user or chat
    • Restrict or Promote user
    • Set chat permissions
    • Set chat admin custom title
    • Chat invite links
    • Join requests
    • Update chat info, photo, title, description and more
    • Pin or unpin messages
    • Get chat info, leave chat
    • Check user subscription
    • Chat sticker set
    • My commands
    • My default admin rights
    • Menu button
Powered by GitBook
On this page
  • How middleware works?
  • Creating middleware
  • Global middlewares
  1. Nestgram Features

Middlewares and Params

Middlewares

PreviousServicesNextSending messages correctly

Last updated 2 years ago

How middleware works?

When the bot gets update, it completes all middlewares, and only then completes a handler. For example, using middlewares, you can auth user and pass some params to the handler

Creating middleware

Let's create logger.middleware.ts file. It will be the middleware that will log received update. We will then apply this middleware

You can move middleware to different position

logger.middleware.ts
export function LoggerMiddleware(
  update: IUpdate, // received update
  answer: Answer, // Answer (you can send a message)
  params: any, // Params (you can pass params to the handler)
  next: NextFunction, // next function (it can be next middleware or handler)
  fail: NextFunction, // fail function (you need to call it if you don't want to call handler)
) {
  params.someParam = 1; // set some param that we want to get in handler
  console.log(update); // log received update
  next(); // call handler
}
app.controller.ts
import { OnText, Controller, Params } from 'nestgram';
import { LoggerMiddleware } from './logger.middleware';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService?: AppService) {}

  @OnText()
  @Middleware(LoggerMiddleware)
  async handleText(@Params() params: any): Promise<string> {
    console.log(params);
    return 'Hello, world!';
  }
}

Global middlewares

You can apply middleware for all controller, if you are uncomfortable to set it for every handler

app.module.ts
import { Module } from 'nestgram';
import { LoggerMiddleware } from './logger.middleware.ts';

import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppController],
  services: [AppService],
  middlewares: [LoggerMiddleware],
})
export class AppModule {}

You can read more about Answer class

🪶
here
How middleware works