rivescript-python icon indicating copy to clipboard operation
rivescript-python copied to clipboard

Event callbacks and example bot

Open flogiston opened this issue 9 years ago • 2 comments

Event callbacks

Enabling event-based handling of session state changes, instead of getting them and comparing after each reply.

def topic_cb(user, topic, redirect=None)
    ...

rive.on('topic', topic_cb)

def uservar_cb(user, name, value)
    ...

rive.on('uservar', uservar_cb)

Example bot - managing user session data using event callbacks

More info in eg/sessions/example.py.

NOTE: Maybe this could be added as a feature to RiveScript, but I've kept it separate for now as I'm not sure what's the status/roadmap for current multi-user functionality. I'm not using it in my bots which operate in multithreaded environment. I'm just keeping one RS instance per user and managing sessions outside. I think session management should be abstracted away and independent of scripting.

Additional public functions

get_topic(user), set_topic(user, topic), redirect(user, target)

Other changes

  • added readline config to rivescript.py so that all interactive prompts (including intaractive.py) are more terminal-friendly
  • cleaner history implementation based on collections.deque
  • minor cleanups

flogiston avatar May 22 '16 00:05 flogiston

What's the use case for the topic callback? Under the hood, topics are just user variables (e.g. <set topic=random> would do the same as {topic=random}, except the {} syntax has priority in begin blocks), or why is it useful to store the redirected trigger?

kirsle avatar May 31 '16 17:05 kirsle

It's handy for tracking engine state (e.g. for persistence) - I attached an example bot. We might want to persist each topic change and in-topic redirects to be able to fully restore the session later. Of course you can get and store all engine state (vars, topic, etc.) after each reply, but I think the callback-based approach is much cleaner -- topic changes don't occur on every reply.

My first implementation of this behavior was via function calls, but it made the script a total mess and it was only time consuming and error-prone (adding a <call> in every topic and using an 'entry' redirect to trigger the call). With the optional callback it's easy and keeps the script clean.

flogiston avatar May 31 '16 18:05 flogiston