AI Zone Admin Forum Add your forum

NEWS: survey on 3000 US and UK consumers shows it is time for chatbot integration in customer service!read more..

javascript Object macros and access to bot fields

I’m trying to write a javascript plugin to return some properties of the bot.
for example if i had

<set mood=happy

how do i access that from javascript or a macro?

eg bit unclear what is passed into a JS function.

looking at the Rivescript source:


from within a macro

object coolfunc javascript



this seems to be a system level object (running inside node server side)
arguments 0 is the bot i think,
arguments 1 is an array of passed in args from rivescript.

but where would the variables be accessible from?
again check RS source:

value this._users[user][name]

i had a look in the


and also can’t see it there.


  [ # 1 ]

The JavaScript object macro runs in the same system as the RiveScript bot itself (if the bot is in the Node server, then the object macro runs there; if run in a browser, it runs in the browser).

The `scope` parameter can be used to make the object macro run the perspective of a different scope. How you’d use it is to pass a scope in to the reply() function, like,

var reply bot.reply(usernamemessagethis); 

The `this` would then be passed all the way through to the JS object macro, and the macro would then be running from the scope of your actual code (from where you called `bot.reply()`), so if your JS code is structured in an object-oriented way it would allow the JS object macro to access your other private variables/methods which it otherwise wouldn’t be able to if it was executing from the global/top-level scope (which is the default, iirc).

Anyway, `bot._users[username]` is indeed where variables get stored for the users (you can test it here: after you start a conversation you can print `bot._users[“local-user”]` in the JS console).

But don’t use `_users` that way, as it’s a “private” object (hence beginning with an underscore); there’s a function `getUservar()` which provides a public API interface to getting a user variable.

So, full example,

i am (happy|sad)
- <
set mood=<star>>I see.

what mood am i in
Your current mood is: <call>coolfunc</call>

object coolfunc javascript
var mood rs.getUservar(rs.currentUser(), "mood");
mood !== "undefined" mood "unknown";

You can copy/paste it in to test. smile

User: What mood am I in?
Bot: Your current mood is: unknown
User: I am happy
Bot: I see.
User: What mood am I in?
Bot: Your current mood is: happy
User: I am sad
Bot: I see.
User: What mood am I in?
Bot: Your current mood is: sad


  [ # 2 ]

OK great this makes sense, thanks!


  login or register to react