Skip to content

A small tool to make it easy to create an interactive chatbot.

License

Notifications You must be signed in to change notification settings

riozee/promptees

Repository files navigation

promptees

npm (scoped) npm bundle size (minified)

A small tool to make it easy to create an interactive chatbot.

Install

$ npm install promptees

Changelog

2.0.0

  • returnPrompt() is now an async function, to support the loop feature.
  • Added new feature: loop
  • Removed all dependencies.

1.0.1

  • Added PrompteesOpts type to exported member.
  • Typescript fixes.

1.0.0

  • isPrompting() is now splitted into two, isPrompting() and returnPrompt(). Now it's possible to check first and then do some processing before resolving to waitForResponse(). returnPrompt() is behaving the same as isPrompting() in the previous version. So you should rename isPrompting() to returnPrompt() if you have a working code using the previous version.
  • Typescript intellisense is now possible using generic type parameter.
  • Added new feature: timeout.

Usage

// test.ts
import Prompt from 'promptees';

const promptees = new Prompt({
	timeout: 3000,
	onTimeout: () => 'timed out',
	loopWhen: (input) => input === 'roll',
	onLoop: (input) => console.log('<BOT>', `What's ${input}?`),
});

async function onMessage(user: string, message: string) {
	console.log(`<${user}>`, message);
	if (promptees.isPrompting(user)) {
		return await promptees.returnPrompt(user, message);
	}
	console.log('<BOT>', 'Hi, tell me your age.');
	const age = await promptees.waitForResponse(user);
	if (age === 'timed out') {
		console.log('<BOT>', 'Are you still there?');
	} else if (+age < 18) {
		console.log('<BOT>', 'You are not allowed');
	} else {
		console.log('<BOT>', 'You are allowed.');
	}
}

(async () => {
	onMessage('Lina', 'Hi bot');
	await pause(500);
	onMessage('Lina', '14');
	await pause(500);
	console.log('============');
	onMessage('Jeff', 'Hello');
	await pause(5000);
	console.log('============');
	onMessage('Rick', 'roll');
	await pause(500);
	onMessage('Rick', 'roll');
	await pause(500);
	onMessage('Rick', 'roll');
	await pause(500);
	onMessage('Rick', 'roll');
	await pause(500);
	onMessage('Rick', '35');
})();

Output:

<Lina> Hi bot
<BOT> Hi, tell me your age.
<Lina> 14
<BOT> You are not allowed
============
<Jeff> Hello
<BOT> Hi, tell me your age.
<BOT> Are you still there?
============
<Rick> roll
<BOT> Hi, tell me your age.
<Rick> roll
<BOT> What's roll?
<Rick> roll
<BOT> What's roll?
<Rick> roll
<BOT> What's roll?
<Rick> 35
<BOT> You are allowed.
See my bot project: Miki Bot.

About

A small tool to make it easy to create an interactive chatbot.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published