Socket
Socket
Sign inDemoInstall

@vk-io/scenes

Package Overview
Dependencies
16
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @vk-io/scenes

Scenes for the library vk-io


Version published
Maintainers
1
Install size
32.0 kB
Created

Readme

Source

VK-IO Scenes

NPM version Build Status NPM downloads

VK-IO Scenes - Simple implementation of middleware-based scene management 🎬

📦 Installation

Node.js 12.20.0 or newer is required

  • Using Yarn (recommended)
    yarn add @vk-io/scenes
    
  • Using npm
    npm i @vk-io/scenes
    
  • Using pnpm
    pnpm add @vk-io/scenes
    

Example usage

import { VK } from 'vk-io';

// Session implementation can be any
import { SessionManager } from '@vk-io/session';
import { SceneManager, StepScene } from '@vk-io/scenes';

const vk = new VK({
	token: process.env.TOKEN
});

const sessionManager = new SessionManager();
const sceneManager = new SceneManager();

vk.updates.on('message_new', sessionManager.middleware);

vk.updates.on('message_new', sceneManager.middleware);
vk.updates.on('message_new', sceneManager.middlewareIntercept); // Default scene entry handler

vk.updates.on('message_new', (context, next) => {
	if (context.text === '/signup') {
		return context.scene.enter('signup');
	}

	return next();
});

sceneManager.addScenes([
	new StepScene('signup', [
		(context) => {
			if (context.scene.step.firstTime || !context.text) {
				return context.send('What\'s your name?');
			}

			context.scene.state.firstName = context.text;

			return context.scene.step.next();
		},
		(context) => {
			if (context.scene.step.firstTime || !context.text) {
				return context.send('How old are you?');
			}

			context.scene.state.age = Number(context.text);

			return context.scene.step.next();
		},
		async (context) => {
			const { firstName, age } = context.scene.state;

			await context.send(`👤 ${firstName} ${age} ages`);

			return context.scene.step.next(); // Automatic exit, since this is the last scene
		}
	])
]);

vk.updates.start().catch(console.error);

Keywords

FAQs

Last updated on 31 Mar 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc