Handling other updates

Handle other updates

How to handle other updates

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, bot says Hello, world if user writes command /start (@OnCommand is responsible for this). But you can also handle other updates: when user sends some message, channel writes a post, user edits a message, channel edits a post, user writes message with some entities or other updates. To handle other updates, you just need to import the appropriate decorator and use it. You can see an example below, in this, bot handles update when a channel publishes a post

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

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

  @OnPost()
  async post(@Message() message: IMessage): Promise<any> {
    console.log(message);
  }
}

Allowed updates

Decorator
Description
Params

OnCommand

When the user writes some command (e.g. /start)

text?: string - The command text you want to listen to. Optional. If not specified, listens for all commands.

OnText

When the user writes some text messages (e.g. "Hello!")

text?: string - The text you want to listen to. Optional. If not specified, listens for all text messages.

OnMessage

When the user sends some message

subtype?: 'edit' | 'post' | 'edit_post'

OnMessageEdit

When the user edits a message

none

OnPost

When the channel publishes some post

none

OnPostEdit

When the channel edits a post

none

OnEntity

When the user sends a message with some entity (e.g. url, command, bold)

entity?: string

When the user clicks on the underTheMessage button

buttonId: string

OnMedia

When the user sends some media file

fileType?: string - Media file type you want to listen to

OnPhoto

When the user sends some photo

none

OnVideo

When the user sends some video

none

OnAudio

When the user sends some audio

none

OnAnimation

When the user sends some animation (for example, gif)

none

OnVoice

When the user sends some voice message

none

OnVideoNote

When the user sends some video note message

none

OnDocument

When the user sends some document

none

OnLocation

When the user sends a location

none

OnVenue

When the user sends a venue

none

OnContact

When the user sends a contact

none

OnPoll

When the user or channel sends a poll

none

OnPollEdit

When the poll edited (for example, when the channel closed the poll)

none

OnPollAnswer

When the user answers to the poll (only not anonymous)

none

OnUpdate

When the bot gets some update

none

OnForward

When the user forwards some message

none

OnDice

When the user sends dice

none

OnJoinRequest

When the user sends chat join request

none

Last updated