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
  • What's scopes?
  • How it works?
  • Example
  • On scope enter event
  1. Nestgram Features

Scopes

IN DEVELOPMENT. You can enter/leave scope

What's scopes?

If you want to ask user something, you need to use scope. Scope - an isolated place for the user, in which the handlers of ordinary controllers don't work, but only the handlers described in the scope work

How it works?

You call the .scope answer method. Then all custom updates are handled by scope handlers, other handlers don't. You can leave at any time, just call the .unscope answer method

.scope answer method takes argument: scope id (resource name), e.g. for NameScope class it will be 'name'. If you try to enter a scope with haven't id, you will get an error

Example

app.module.ts
import { Module } from 'nestgram';
import { AppService } from './app.service';
import { AppController } from './app.controller';
import { NameModule } from './scopes/name/name.module';

@Module({
  imports: [NameModule],
  controllers: [AppController],
  services: [AppService],
})
export class AppModule {}
app.controller.ts
import { Answer, Controller, GetAnswer, Keyboard, KeyboardTypes, MessageSend, OnClick, OnCommand } from 'nestgram';
import { AppService } from './app.service';

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

  @OnCommand('start')
  async start(@GetAnswer() answer: Answer) {
    try {
      return new MessageSend(
        'Hello! Do you want to enter your name?',
        new Keyboard(KeyboardTypes.underTheMessage)
          .btn('Enter my name', 'scope'),
      );
    } catch (e) {
      console.error(e);
    }
  }

  @OnClick('scope')
  async enterScope(@GetAnswer() answer: Answer) {
    await answer.scope('name');
    return 'Enter your name';
  }
}
name.module.ts
import { Module } from 'nestgram';
import { NameScope } from './name.scope';
import { NameService } from './name.service';

@Module({
  scopes: [NameScope],
  services: [NameService],
})
export class NameModule {}
name.scope.ts
import { Answer, GetAnswer, Keyboard, KeyboardTypes, MessageSend, OnClick, OnText, Scope, Text } from 'nestgram';

@Scope()
export class NameScope {
  @OnText()
  onNameSend(@Text() name: string) {
    return new MessageSend(
      `Hello, ${name}!`,
      new Keyboard(KeyboardTypes.underTheMessage)
        .btn('Leave scope', 'leave'),
    );
  }

  @OnClick('leave')
  async leaveScope(@GetAnswer() answer: Answer) {
    await answer.unscope();
    return 'Unscope';
  }
}

On scope enter event

You can handle when the user enters the scope with the @OnEnter decorator

app.controller.ts
import { Answer, Controller, GetAnswer, Keyboard, KeyboardTypes, MessageSend, OnClick, OnCommand } from 'nestgram';
import { AppService } from './app.service';

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

  @OnCommand('start')
  async start(@GetAnswer() answer: Answer) {
    try {
      return new MessageSend(
        'Hello! Do you want to enter your name?',
        new Keyboard(KeyboardTypes.underTheMessage)
          .btn('Enter my name', 'scope'),
      );
    } catch (e) {
      console.error(e);
    }
  }

  @OnClick('scope')
  async enterScope(@GetAnswer() answer: Answer) {
    await answer.scope('name');
  }
}
name.scope.ts
import { Answer, GetAnswer, Keyboard, KeyboardTypes, MessageSend, OnClick, OnEnter, OnText, Scope, Text } from 'nestgram';

@Scope()
export class NameScope {
  @OnEnter()
  onScopeEnter() {
    return 'Enter your name';
  }

  @OnText()
  onNameSend(@Text() name: string) {
    return new MessageSend(
      `Hello, ${name}!`,
      new Keyboard(KeyboardTypes.underTheMessage)
        .btn('Leave scope', 'leave'),
    );
  }

  @OnClick('leave')
  async leaveScope(@GetAnswer() answer: Answer) {
    await answer.unscope();
    return 'Unscope';
  }
}
PreviousAnswer classNextStates

Last updated 2 years ago

You can read more about Answer class

You can read more about Keyboards

🪶
here
here