Nestgram
  • ⭐About Nestgram
  • 📄Guide
  • 📰News
  • 🔼Handling updates
    • Handling commands
    • Handling text messages
    • Handling other updates
    • Entities: url, email and other
    • Other arguments in handler
    • Handling media files and download it
  • 🪶Nestgram Features
    • Services
    • Middlewares and Params
    • Sending messages correctly
    • Answer class
    • Scopes
    • States
    • Views
  • 💬Messages
    • Sending a photo, video and other media
    • Sending a media group
    • Send location (live) or venue
    • Send contact
    • Send poll
    • Send dice
    • Edit/delete messages
    • Copy or Forward a message
  • ⌨️Keyboards
    • Keyboard types, building keyboard
    • Handle underTheMessage keyboard button click by Alert or Toast
    • Keyboard layouts
  • ⚙️Config
    • CLI
    • Webhooks and run config
    • Api class
    • Modules
      • Mongo module
    • Controller Helper class
  • 🤖API Reference
    • Set chat action
    • Save user profile photo
    • Ban, unban user or chat
    • Restrict or Promote user
    • Set chat permissions
    • Set chat admin custom title
    • Chat invite links
    • Join requests
    • Update chat info, photo, title, description and more
    • Pin or unpin messages
    • Get chat info, leave chat
    • Check user subscription
    • Chat sticker set
    • My commands
    • My default admin rights
    • Menu button
Powered by GitBook
On this page
  • Sending message as answer
  • Sending message line
  1. Nestgram Features

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:

Argument
Description
Required
1

Content (media or text)

Required

2

Optional

3

Optional

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'));
  }
}
PreviousMiddlewares and ParamsNextAnswer class

Last updated 2 years ago

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
Keyboard
Options