Api class

You can use an Api class to send a message to another users and more

Why do you need an Api class?

If you want to send a message to another user, call api method for a different bot, or get data from the api reference, you can use Api class methods

Get Api class in controller

To get Api class in controller, use @GetApi property decorator, and Api class as type

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

@Controller()
export class AppController {
  @GetApi() api: Api;

  constructor(private readonly appService?: AppService) {}

  @OnCommand('start')
  async start(): Promise<string> {
    await this.api.send('userId', 'Hello!'); // send message
    return 'Hello, world!';
  }
}

.send Api method take arguments:

Argument
Description
Required
1

User ID or channel ID (string or number)

Required

2

Message you want to send (you can use MessageSend class too)

Required

3

Keyboard

Optional

4

Optional

If you want to get a bot token, you can get it via .token api property

Api methods

You can use any method from the Answer class in the Api class

If Api method takes chat id or message id, you need to pass it as 1st and 2nd arguments

Get Api for different token

If you want to call some api method for different bot, use bot.to method

bot.to method takes argument: token you want to get api

main.ts
import { ChatAction } from 'nestgram';
import { bot } from './bot';

bot.to('token').send('chatId', new ChatAction('typing'));

Last updated