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
  • Why do you need to use the Answer class?
  • Get Answer class and use it
  • More info about the Answer class
  • Add answer class to context
  1. Nestgram Features

Answer class

Sends many messages, reply to the messages in middlewares

Why do you need to use the Answer class?

We recommend using return when sending a message, but there are cases where this is impossible. For example, if you are using middleware, or you need to get response from the method, you need to use Answer class

Get Answer class and use it

If you want to get the Answer class in the middleware, get it as the 2nd argument. But if you're in a handler, you can get the Answer class using the @GetAnswer parameter decorator

app.contoller.ts
import { GetAnswer, Answer, OnCommand, Controller } from 'nestgram';
import { AppService } from './app.service';

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

  @OnCommand('start')
  async start(@GetAnswer() answer: Answer): Promise<void> {
    await answer.send('First message');
    await answer.send('Second message');
  }
}

More info about the Answer class

The Answer class is asynchronous. You can pass the MessageSend class as content to the Answer class too. If you're using the Answer class, you can return class-method too

app.controller.ts
import { GetAnswer, Answer, OnCommand, Controller, MessageSend } from 'nestgram';
import { AppService } from './app.service';

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

  @OnCommand('start')
  async start(@GetAnswer() answer: Answer): Promise<MessageSend> {
    await answer.send(new MessageSend('First message'));
    return new MessageSend('Second message');
  }
}

Add answer class to context

If you often use the Answer class, you shouldn't use the @GetAnswer decorator in every handler. You can use the @GetAnswerContext property decorator in the controller and use it like this.answer

app.controller.ts
import { Answer, Controller, GetAnswerContext, Keyboard, KeyboardTypes, MessageSend, OnCommand } from 'nestgram';
import { AppService } from './app.service';

@Controller()
export class AppController {
  @GetAnswerContext() answer: Answer;

  constructor(private readonly appService?: AppService) {}

  @OnCommand('start')
  async start() {
    await this.answer.send('Hello, world!');
    await this.answer.send(
      new MessageSend('How are you?'),
      new Keyboard(KeyboardTypes.underTheMessage)
        .btn('Fine!', 'fine'),
    )
  }
}
PreviousSending messages correctlyNextScopes

Last updated 2 years ago

You can read more about keyboard

🪶
here