1.0.1 • Published 1 year ago

@vk-io/stateless-prompt v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

VK-IO Stateless Prompt

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

How it works

The basic concept is to send your message with a special text at the end. Then the user replies to the message, the bot checks message for a special text at the end of a reply message. If the message contains a special text, it calls a handler with a user message. If it doesn't contain a special text, then it skips. This is all!

Example

📦 Installation

Node.js 12.20.0 or newer is required

  • Using Yarn (recommended)
    yarn add @vk-io/stateless-prompt
  • Using npm
    npm i @vk-io/stateless-prompt
  • Using pnpm
    pnpm add @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);