Keyboard types, building keyboard

You can add keyboard to the chat or to the message using Keyboard class

underTheChat keyboard (or keyboard)

The keyboard underTheChat is the keyboard you can always see. You can scroll up, but this keyboard won't be removed. The user can only hide it with the button next to the input, or you can remove it in code using the removeUnderTheChat keyboard type. This type of keyboards, when pressed, simply sends a message from the user

underTheMessage keyboard (or inline_keyboard)

The keyboard underTheMessage is a keyboard under the message, with some buttons that you can click to perform some actions in the code.

Building keyboard

Create keyboard and add it to the message

You can create a keyboard using Keyboard class and pass keyboard type as constructor argument using KeyboardTypes enum. Then you can add buttons to the keyboard using button type as method name (e.g. text, btn, url. You can see list of supported button types below). Then, if you want to add a new row, call .row method and continue creating new buttons. In some buttons you can pass "hidden" argument as the last, and if it will be true, button will be hide. You can do this in .row method too.

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

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

  @OnCommand('start')
  async start(): Promise<MessageSend> {
    return new MessageSend(
      'underTheMessage (or inline_keyboard) keyboard example',
      new Keyboard(KeyboardTypes.underTheMessage) // keyboard type
        .btn('First button', 'first') // creates a button. 1st argument is text, 2nd argument is button id
        .btn('Second button', 'second')
        .row() // creates a row with newly added buttons
        .btn('Third button', 'third') // for the last buttons row is created automatically
    );
  }
}

underTheChat keyboard params

You can set the placeholder for the underTheChat keyboard using the .setPlaceholder method

You can add keyboard once (one time keyboard) using .oneTime method

Supported button types

Supported button typeSupports keyboard typesArguments

text

underTheChat

text: string, hidden?: boolean

btn

underTheMessage

text: string, buttonId: string, hidden?: boolean

url

underTheMessage

text: string, url: string, hidden?: boolean

contact

underTheChat

text: string, hidden?: boolean

location

underTheChat

text: string, hidden?: boolean

switch

underTheMessage

text: string, switchQueryId: string, hidden?: boolean

pay

underTheMessage

text: string

webApp

underTheMessage, underTheChat

text: string, url: string, hidden?: boolean

Handling click on underTheMessage keyboard button

If you want to handle when user clicks on underTheMessage keyboard button, you can use @OnClick decorator for method and pass button id to it. More about this here

Last updated