Sending messages correctly
How to send message correctly in a controller handler
Sending message as answer
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
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:
Sending message line
You can also send more than one message, use the .next
method on the MessageSend class
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