Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bennycode/wizardy
Conversational wizard and prompts generator
https://github.com/bennycode/wizardy
conversational-interface conversational-ui prompts questionnaire setup ui wizard
Last synced: 4 months ago
JSON representation
Conversational wizard and prompts generator
- Host: GitHub
- URL: https://github.com/bennycode/wizardy
- Owner: bennycode
- License: mit
- Created: 2019-09-12T20:26:28.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-07-12T15:28:55.000Z (over 1 year ago)
- Last Synced: 2024-03-15T15:51:14.610Z (11 months ago)
- Topics: conversational-interface, conversational-ui, prompts, questionnaire, setup, ui, wizard
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/wizardy
- Size: 383 KB
- Stars: 7
- Watchers: 3
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Wizardy 🧙📜
Wizardy is a conversational wizard which helps you to easily build prompts and questionnaires. It has been designed to be used with messenger applications to generate bot interactions but it is generic and can be used in many ways.
## ❯ Features
- **Easy-to-use.** Wizardy's API is dead simple, [easy to use](#-usage) and easy to replace (if you ever want to).
- **Typed.** Wizardy is written [100% in TypeScript](./src/Wizardy.ts), so there is no need to install typings from an external source.
- **Tested.** Do not be surprised when using Wizardy, it ships with [100% code coverage](./src/Wizardy.test.ts).
- **Documented.** Modern documentation lives in code. That's why [Wizardy's interface](./src/Prompt.ts) contains TSDoc comments.
- **Compatible.** Node.js, Browsers? [Run Wizardy](https://runkit.com/npm/wizardy) on the platform of your choice!
- **Independent.** Don't be afraid of big lock files. Wizardy uses [zero dependencies](https://www.npmjs.com/package/wizardy?activeTab=dependencies).## ❯ Installation
```bash
npm install wizardy
``````bash
yarn add wizardy
```## ❯ Usage
**TypeScript**
```ts
import {Prompt, Wizardy} from 'wizardy';const questionnaire: Prompt[] = [
{
answerKey: 'item',
answerValue: input => input.trim(),
question: `What would you like to order?`,
response: () => `Lucky you! We have '${wizard.answers.item}' in stock.`,
},
{
answerKey: 'quantity',
answerValue: input => parseInt(input, 10),
question: () => `How many '${wizard.answers.item}' do you want to buy?`,
response: () =>
`Great, we will prepare your order and deliver ${wizard.answers.quantity}x ${wizard.answers.item} as soon as possible.`,
},
];const wizard = new Wizardy();
wizard.addQuestions(questionnaire);
console.log(wizard.step); // 0console.log(wizard.ask()); // "What would you like to order?"
console.log(wizard.answer('iPhones')); // "Lucky you! We have 'iPhones' in stock."
console.log(wizard.step); // 1console.log(wizard.ask()); // "How many 'iPhones' do you want to buy?"
console.log(wizard.answer('13')); // "Great, we will prepare your order and deliver 13 iPhones as soon as possible."
console.log(wizard.step); // 2
```**Node.js**
```js
const {Prompt, Wizardy} = require('wizardy');const wizard = new Wizardy();
wizard.on(Wizardy.TOPIC.START, () => console.log('Wizard started...'));
wizard.on(Wizardy.TOPIC.END, answers => {
console.log('Wizard ended', answers); // Wizard ended { category: 'pizza', quantity: 7 }
});const questionnaire = [
{
answerKey: 'category',
answerValue: answer => {
const menu = ['burger', 'pizza', 'veggie burger'];
answer = answer.toLowerCase();
if (menu.includes(answer)) {
return answer;
} else {
throw Error(
`Sorry, we only offer: ${menu
.map(category => category.charAt(0).toUpperCase() + category.substr(1))
.join(', ')}.`
);
}
},
question: 'What kind of food do you want to order?',
response: 'Good choice!',
},
{
answerKey: 'quantity',
answerValue: answer => {
try {
return parseInt(answer.match(/(\d+)/)[0], 10);
} catch (error) {
throw Error('Please write down the amount as number.');
}
},
question: `How much do you want?`,
response: 'Thanks. We will prepare your order.',
},
];wizard.addQuestions(questionnaire);
console.log(wizard.ask()); // What kind of food do you want to order?
console.log(wizard.answer('Fajitas')); // Sorry, we only offer: Burger, Pizza, Veggie burger.console.log(wizard.ask()); // What kind of food do you want to order?
console.log(wizard.answer('Pizza')); // Good choice!console.log(wizard.ask()); // How much do you want?
console.log(wizard.answer('Seven')); // Please write down the amount as number.console.log(wizard.ask()); // How much do you want?
console.log(wizard.answer('I would like to have 7.')); // Thanks. We will prepare your order.
```