Skip to content

Commit

Permalink
feat(docs): add scene support
Browse files Browse the repository at this point in the history
  • Loading branch information
xTCry committed Jan 27, 2023
1 parent 90750d3 commit 000a11c
Showing 1 changed file with 75 additions and 2 deletions.
77 changes: 75 additions & 2 deletions README.md
@@ -1,4 +1,5 @@
# NestJS VK

[![npm](https://img.shields.io/npm/v/nestjs-vk.svg?style=flat-square)](https://www.npmjs.com/package/nestjs-vk)
[![NPM](https://img.shields.io/npm/dt/nestjs-vk.svg?style=flat-square)](https://www.npmjs.com/package/nestjs-vk)
[![GitHub last commit](https://img.shields.io/github/last-commit/xtcry/nestjs-vk)](https://github.com/xtcry/nestjs-vk)
Expand All @@ -15,7 +16,7 @@ This module provides a quick and easy way to interact with the VK API and create

- Simple. Easy to use.
- Ability to create custom decorators.
- ~~Scenes support.~~
- Scenes support.
- Ability to run multiple bots simultaneously.
- Full support of NestJS guards, interceptors, filters and pipes!

Expand All @@ -26,11 +27,16 @@ $ npm i nestjs-vk vk-io
```

## Usage

Once the installation process is complete, we can import the `VkModule` into the root `AppModule`:

```typescript
import { Module } from '@nestjs/common';
import { VkModule } from 'nestjs-vk';

import { AppUpdate } from './app.update';
import { SimpleScene } from './scene/simple.scene';

@Module({
imports: [
VkModule.forRoot({
Expand All @@ -39,17 +45,21 @@ import { VkModule } from 'nestjs-vk';
pollingGroupId: +process.env.VK_BOT_GROUP_ID,
apiMode: 'sequential',
},
})
}),
],
providers: [AppUpdate, SimpleScene],
})
export class AppModule {}
```

Then create `app.update.ts` file and add some decorators for handling VK bot API updates:

```typescript
import { InjectVkApi, Update, Ctx, Message, Hears, HearFallback } from 'nestjs-vk';
import { MessageContext, VK } from 'vk-io';

import { AppService } from './app.service';
import { SIMPLE_SCENE } from './vk.constants';

@Update()
export class AppUpdate {
Expand Down Expand Up @@ -80,6 +90,12 @@ export class AppUpdate {
return 'Hey there';
}

@Hears(/scene( ?(?<state>[0-9]+))?$/i)
async hearsScene(@Ctx() ctx: MessageContext) {
const stateNumber = ((e) => (isNaN(Number(e)) ? null : Number(e)))(ctx.$match?.groups?.state);
ctx.scene.enter(SIMPLE_SCENE, { state: { stateNumber } });
}

@Hears(['/sub', 'subscriber'])
async onSubscriberCommand(@Ctx() ctx: MessageContext) {
const isSib = await this.vk.api.groups.isMember({
Expand All @@ -102,3 +118,60 @@ export class AppUpdate {
}
}
```

For a simple scene, let's create an `app.update.ts` file and do a few steps for it:

```typescript
import { Scene, AddStep, Ctx, SceneEnter, SceneLeave } from 'nestjs-vk';
import { MessageContext } from 'vk-io';
import { IStepContext } from '@vk-io/scenes';

import { SIMPLE_SCENE } from './vk.constants';

@Scene(SIMPLE_SCENE)
export class SimpleScene {
@SceneEnter()
async onSceneEnter(@Ctx() ctx: IStepContext) {
const { stateNumber } = ctx.scene.state;
await ctx.reply(
`Hello! I am a simple scene. Your state number is ${stateNumber}.\n` +
` You can send me a number and I will multiply it by 2.`,
);
}

@SceneLeave()
async onSceneLeave(@Ctx() ctx: IStepContext) {
await ctx.reply('Bye!');
}

// @Hears('exit')
// async onHearsExit(@Ctx() ctx: IStepContext) {
// await ctx.scene.leave();
// }

@AddStep()
async onAddStep(@Ctx() ctx: IStepContext) {
let { stateNumber: number } = ctx.scene.state;

if (!ctx.scene.step.firstTime) {
number = Number(ctx.text);
}

if (ctx.scene.step.firstTime && number === null) {
await ctx.reply('Please send me a number.');
return;
}

if (isNaN(number)) {
await ctx.reply('Wrong. Please send me a number.');
return;
}

await ctx.reply(`Your number multiplied by 2 is ${number * 2}.`);

if (number > 20 && number % 2 === 0) {
await ctx.scene.leave();
}
}
}
```

0 comments on commit 000a11c

Please sign in to comment.