@vk-io/stateless-prompt

Stateless prompts for the library vk-io

Usage no npm install needed!

<script type="module">
  import vkIoStatelessPrompt from 'https://cdn.skypack.dev/@vk-io/stateless-prompt';
</script>

README

VK-IO Scenes

NPM version Build Status NPM downloads Code quality

VK-IO Stateless Prompt - Simple implementation of middleware-based stateless prompt

Example

Installation

Node.js 12.0.0 or newer is required

Yarn

Recommended

yarn add @vk-io/stateless-prompt

NPM

npm i @vk-io/stateless-prompt

Example usage

import { VK } from 'vk-io';

import { StatelessPromptManager } from '@vk-io/stateless-prompt';

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

const namePrompt = new StatelessPromptManager({
    slug: 'name',
    handler: (context, next) => {
        if (!context.text) {
            return context.send('Please reply your name with text to previous message');
        }

        return context.send(`Your name is ${context.text}`);
    }
});

vk.updates.on('message_new', namePrompt.middlewareIntercept);

vk.updates.on('message_new', (context, next) => {
    if (context.text === '/signup') {
        return context.send('What\'s your name? Please reply to this message. ' + namePrompt.suffix);
    }

    return next();
});

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