Giter Site home page Giter Site logo

cloudxtreme / rivescript-js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aichaos/rivescript-js

0.0 1.0 0.0 1.03 MB

A RiveScript interpreter for JavaScript. RiveScript is a scripting language for chatterbots.

Home Page: https://www.rivescript.com/

License: MIT License

Makefile 0.34% Shell 0.10% JavaScript 94.49% CSS 0.45% HTML 2.94% Perl 0.12% Python 1.55%

rivescript-js's Introduction

RiveScript-JS

Build Status

INTRODUCTION

This is a RiveScript interpreter library for JavaScript. RiveScript is a scripting language for chatterbots, making it easy to write trigger/response pairs for building up a bot's intelligence.

This library can be used both in a web browser or as a Node module. See the eg/ folder for examples.

NOTICE: CHANGES IN v2.0.0 ALPHA

This branch is currently the alpha version of RiveScript v2.0.0. The biggest change was adding the use of async/await throughout the codebase, which necessarily had to break backwards compatibility with some of the RiveScript API.

  • reply() now returns a Promise instead of a string, like replyAsync() did before.
  • loadFile() and loadDirectory() now are Promise-based instead of callback-based.
  • replyAsync() is now deprecated and will be removed soon.

This means your use of reply() will need to be updated to use the Promise. If you are already using replyAsync(), just replace the function name with reply() and it works the same way.

  var bot = new RiveScript();
- bot.loadDirectory("./brain", onSuccess, onError);
+ bot.loadDirectory("./brain").then(onReady).catch(onError);

  function onReady() {
      bot.sortReplies();
      console.log("Bot is ready!");

-     var reply = bot.reply(username, message);
-     console.log("Bot>", reply);
+     bot.reply(username, message).then(function(reply) {
+         console.log("Bot> ", reply);
+     });
  }

DOCUMENTATION

There is generated Markdown and HTML documentation of the modules in the docs folder. The main module is at rivescript.

Also check out the RiveScript Community Wiki for common design patterns and tips & tricks for RiveScript.

INSTALLATION

For nodejs and other similar JavaScript engines, you can install this module through npm:

npm install rivescript

For the web you can use the unpkg:

<script src="https://unpkg.com/rivescript@latest/dist/rivescript.min.js"></script>

The git repository for this project includes ES2015+ source code. For ES5 builds targeting older browsers and Node versions, check the Releases tab. The compiled distribution includes a lib/ directory with ES5 sources to use with node <= 6, and a dist/ directory containing a "browserified" script that can be used on a web page.

To use on the web, just load dist/rivescript.min.js with a <script> tag like usual.

EXAMPLES

There are examples available in the eg/ directory of this project on GitHub that show how to interface with a RiveScript bot in a variety of ways--such as through a web browser or a telnet server--and other code snippets and useful tricks.

RIVESCRIPT PLAYGROUND

For testing and sharing RiveScript snippets that use the JavaScript implementation, check out the RiveScript Playground.

It's a JSFiddle style web app for playing with RiveScript in your web browser and sharing code with others.

https://play.rivescript.com/

USAGE

The distribution of RiveScript.js includes an interactive shell for testing your RiveScript bot, shell.js. Run it with Node and point it to a folder where you have your RiveScript documents. Example:

node shell.js eg/brain

Once inside the shell you can chat with the bot using the RiveScript files in that directory. For simple debugging you can type /eval to run single lines of JavaScript code. See /help for more.

Both shell scripts accept command line parameters:

  • --debug: enables verbose debug logging.
  • --watch: watch the reply folder for changes and automatically reload the bot when files are modified.
  • --utf8: enables UTF-8 mode.

When using RiveScript.js as a library, the synopsis is as follows:

var bot = new RiveScript();

// Load a directory full of RiveScript documents (.rive files). This is for
// Node.JS only: it doesn't work on the web!
bot.loadDirectory("brain").then(loading_done).catch(loading_error);

// Load an individual file.
bot.loadFile("brain/testsuite.rive").then(loading_done).catch(loading_error);

// Load a list of files all at once (the best alternative to loadDirectory
// for the web!)
bot.loadFile([
  "brain/begin.rive",
  "brain/admin.rive",
  "brain/clients.rive"
]).then(loading_done).catch(loading_error);

// All file loading operations are asynchronous, so you need handlers
// to catch when they've finished. If you use loadDirectory (or loadFile
// with multiple file names), the success function is called only when ALL
// the files have finished loading.
function loading_done() {
  console.log("Bot has finished loading!");

  // Now the replies must be sorted!
  bot.sortReplies();

  // And now we're free to get a reply from the brain!
  // NOTE: the API has changed in v2.0.0 and returns a Promise now.
  bot.reply("local-user", "Hello, bot!").then(function(reply) {
    console.log("The bot says: " + reply);
  });
}

// It's good to catch errors too!
function loading_error(error, filename, lineno) {
  console.log("Error when loading files: " + error);
}

UTF-8 SUPPORT

Version 1.0.5 adds experimental support for UTF-8 in RiveScript documents. It is disabled by default. Enable it by passing a true value for the utf8 option in the constructor.

By default (without UTF-8 mode on), triggers may only contain basic ASCII characters (no foreign characters), and the user's message is stripped of all characters except letters, numbers and spaces. This means that, for example, you can't capture a user's e-mail address in a RiveScript reply, because of the @ and . characters.

When UTF-8 mode is enabled, these restrictions are lifted. Triggers are only limited to not contain certain metacharacters like the backslash, and the user's message is only stripped of backslashes and HTML angled brackets (to protect from obvious XSS if you use RiveScript in a web application). Additionally, common punctuation characters are stripped out, with the default set being /[.,!?;:]/g. This can be overridden by providing a new RegExp object as the rs.unicodePunctuation attribute. Example:

// Make a new bot with UTF-8 mode enabled.
var bot = new RiveScript({utf8: true});

// Override the punctuation characters that get stripped from the
// user's message.
bot.unicodePunctuation = new RegExp(/[.,!?;:]/g);

The <star> tags in RiveScript will capture the user's "raw" input, so you can write replies to get the user's e-mail address or store foreign characters in their name.

This has so far only been tested when run under Node. When served through a web server, take extra care that your server sends the correct content encoding with the RiveScript source files (Content-Type: text/plain; charset=utf-8).

One caveat to watch out for in UTF-8 mode is that punctuation characters are not removed from a user's message, so if they include commas or exclamation marks it can impact the matching ability of your triggers (you should absolutely not write an explicit punctuation mark on your trigger's side. Triggers should NOT contain symbols like ? or , even with UTF-8 mode enabled, and while that may work right now, a future update will probably rigidly enforce this).

BUILDING

I use npm run scripts to handle various build tasks.

  • npm run build - Compiles the ES2015+ sources from src/ with Babel and outputs them into lib/
  • npm run test - Builds the source with Babel per above, builds the ES2015+ test scripts in test/ and outputs them into test.babel/ and then runs nodeunit on it.
  • npm run dist - Produces a full distributable build. Source is built with Babel and then handed off to webpack and uglify for browser builds.
  • npm run webpack - Creates dist/rivescript.js from the ES2015+ sources directly from src/ (using babel-loader). This command is independent from npm run build and could be run without leaving any ES5 code sitting around.
  • npm run uglify - Minifies dist/rivescript.js to dist/rivescript.min.js
  • npm run clean - Clean up all the build files.

If your local Node version is >= 7 (supports Async/Await), you can run the ES2015+ sources directly without needing to run any npm scripts. For that purpose, I have a Makefile.

  • make setup - sets up the dev environment, installs dependencies, etc.
  • make run - runs shell.js pointed at the example brain. This script runs natively on the ES2015+ sources, no build steps needed.
  • make test - runs nodeunit on the ES2015+ test sources directly without building them as npm run test would.

PUBLISHING

Steps for the npm maintainer of this module:

  1. Increment the version number in package.json and src/rivescript.js
  2. Add a change log notice to Changes.md
  3. Run npm run dist to build the ES5 sources and run unit tests.
  4. Test a local installation from a different directory (npm install ../rivescript-js)
  5. npm login if it's the first time on a new system, and npm publish to publish the module to NPM.
  6. Create compiled zip and tarballs for GitHub releases:
  • Copy git repo to a new folder.
  • rm -rf .git node_modules to remove cruft from the new folder.
  • zip -r rivescript-js-VERSION.zip rivescript-js
  • tar -czvf rivescript-js-VERSION.tar.gz rivescript-js

LICENSE

The MIT License (MIT)

Copyright (c) 2017 Noah Petherbridge

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

SEE ALSO

The official RiveScript website, http://www.rivescript.com/

rivescript-js's People

Contributors

callmephilip avatar christhebaron avatar dcsan avatar johnnymast avatar julien-c avatar kirsle avatar kjleitz avatar lily418 avatar mariohmol avatar markdowney avatar n1t0 avatar npmcdn-to-unpkg-bot avatar pirelaurent avatar pyrox777 avatar snowyu avatar tostegroo avatar

Watchers

 avatar

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.