> For the complete documentation index, see [llms.txt](https://degreetpro.gitbook.io/nestgram/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://degreetpro.gitbook.io/nestgram/nestgram-features/answer-class.md).

# Answer class

## 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

{% code title="app.contoller.ts" %}

```typescript
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');
  }
}
```

{% endcode %}

## 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

{% code title="app.controller.ts" %}

```typescript
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');
  }
}
```

{% endcode %}

## 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`

{% code title="app.controller.ts" %}

```typescript
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'),
    )
  }
}
```

{% endcode %}

{% hint style="info" %}
You can read more about keyboard [here](/nestgram/keyboards/keyboard-types-building-keyboard.md)
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://degreetpro.gitbook.io/nestgram/nestgram-features/answer-class.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
