aichaos / rivescript-js

A RiveScript interpreter for JavaScript. RiveScript is a scripting language for chatterbots.
https://www.rivescript.com/
MIT License
377 stars 144 forks source link

User Variable Session Manager Interface #272

Closed kirsle closed 6 years ago

kirsle commented 6 years ago

This implements the User Variable Session Manager Interface, catching rivescript-js up to its Go, Python and Java cousins.

This will make it possible to replace the in-memory user variable store with one backed by MongoDB or Redis or anything else, with async support. This is made possible with the async/await feature in modern JavaScript, so that the bot can "pause" anywhere in the middle of a reply() to set and retrieve user variables from storage asynchronously. This means your bot can actively remember variables without you needing to use getUservars() to export them periodically.

The default session manager continues to keep variables in an in-memory object.

Implementations for session managers (e.g. Redis) will come as separate packages on npm, e.g., rivescript-contrib-redis or so.

The usage for you will be like:

const RedisStore = require("rivescript-contrib-redis");
const RiveScript = require("rivescript");

var bot = new RiveScript({
    // RedisStore constructor arguments TBD
    sessionManager: new RedisStore("localhost:6379"),
});
bot.setUservar("kirsle", "topic", "random") // goes straight to Redis

Fixes #268