Sending messages correctly

How to send message correctly in a controller handler

Sending message as answer

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

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

  @OnCommand('start')
  async start(): Promise<string> {
    return 'Hello, world!';
  }
}

In this code in start method, you can see when the user writes command /start, the user gets the message Hello, world!. What's method returns will be sent to the user

app.controller.ts
import { OnCommand, Controller, MessageSend } 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('Hello, world!');
  }
}

MessageSend class-method is a wrapper for the message: it allows you to add keyboard, options, photo, or media to the message

If you don't know what will return your method (e.g. MessageSend or Edit) you can use MessageCreator class as return type (import from root)

MessageSend class-method take arguments:

ArgumentDescriptionRequired
1

Content (media or text)

Required

2

Optional

3

Optional

Sometimes you can't use the class methods, for example when you want to get some info about the current chat, for that you can use the Answer class

Sending message line

You can also send more than one message, use the .next method on the MessageSend class

.next method takes argument: content you want to send (it can be MessageSend class). It also can be a function or an async function

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

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

  @OnCommand('start')
  start() {
    return new MessageSend('Hello, world!')
      .next('Second message')
      .next(new MessageSend('Third message'));
  }
}

Last updated