Keyboard layouts

You can save the keyboard as a layout and reuse it.

Why do you need to use keyboard layouts?

If you have a reusable keyboard (like a menu back button), you can export it as a layout and reuse it on other keyboards in different files

Create keyboard layout

You can simply create a Keyboard class instance and export it

menu.keyboard.ts
import { Keyboard, KeyboardTypes } from 'nestgram';

export const MenuKeyboard = new Keyboard(KeyboardTypes.underTheMessage)
  .btn('btn1', 'one') // you can create buttons
  .btn('btn2', 'two')
  .row() // you can also create rows
  .btn('Menu', 'menu'); // one button in the next row

Use keyboard layout

To use keyboard layout, just call .use Keyboard method

.use method takes argument: layout you want to use (Keyboard class instance)

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

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

  @OnCommand('start')
  async start(): Promise<MessageSend> {
    return new MessageSend(
      'Keyboard layout',
      new Keyboard(KeyboardTypes.underTheMessage)
        .btn('First button', 'first')
        .btn('Second button', 'second')
        .row() // you can create rows
        .btn('Third button', 'third')
        .use(MenuKeyboard) // add first row in layout to the button, and add second row from menu layout
        .btn('More buttons', 'fourth'), // you can continue adding buttons
    );
  }
}

Last updated