Views

You can enter view

What are views?

Views are reusable handlers. You can create a view and call it at any time. View can send messages using the Answer class. For example, you can create your bot's menu using views

Create view

View is a simple function that has a name in format NameView. To create view, just export function

View function takes argument: Answer class

View function must have a name in NameView format

menu.view.ts
export async function MenuView(answer: Answer): Promise<void> {
  await answer.send('Menu');
}

Call view

At first, you need to use view, import it to views field in your module file

app.module.ts
import { Answer, Module } from 'nestgram';
import { AppService } from './app.service';
import { AppController } from './app.controller';
import { MenuView } from './menu.view.ts';

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

Then you can call it using .view answer method

.view answer method takes argument: view id, e.g. for MenuView class it will be 'menu'. If you try to enter a view with haven't id, you will get an error

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

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

  @OnCommand('start')
  async start(@GetAnswer() answer: Answer) {
    await answer.view('menu');
  }
}

Last updated