@vk-io/stateless-prompt
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

VK-IO Stateless Prompt

NPM version Build Status NPM downloads

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);

Readme

Keywords

Package Sidebar

Install

npm i @vk-io/stateless-prompt

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

8.13 kB

Total Files

9

Last publish

Collaborators

  • negezor