Giter Site home page Giter Site logo

bennycode / wizardy Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 1.0 392 KB

Conversational wizard and prompts generator

Home Page: https://www.npmjs.com/package/wizardy

License: MIT License

JavaScript 5.05% TypeScript 94.95%
conversational-ui conversational-interface prompts questionnaire setup wizard ui

wizardy's Introduction

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 and easy to replace (if you ever want to).
  • Typed. Wizardy is written 100% in TypeScript, 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.
  • Documented. Modern documentation lives in code. That's why Wizardy's interface contains TSDoc comments.
  • Compatible. Node.js, Browsers? Run Wizardy on the platform of your choice!
  • Independent. Don't be afraid of big lock files. Wizardy uses zero dependencies.

โฏ Installation

npm install wizardy
yarn add wizardy

โฏ Usage

TypeScript

import {Prompt, Wizardy} from 'wizardy';

const questionnaire: Prompt<string | number>[] = [
  {
    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); // 0

console.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); // 1

console.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

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.

wizardy's People

Contributors

bennycode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

vikingdadmedic

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.