AI Zone Admin Forum Add your forum

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

reference to a JS function in an external JS-file?
 
 

In RS you can add JS-functions by writing

object myJsFunction javascript
    console
.log("hello world");
    return 
"return value";
object 

What if I got a bigger function or a function that calls other functions? Can I make a reference to a function outside of the RiveScript-file?

 

 
  [ # 1 ]

Not sure what you mean but here’s a couple ideas:

1) You can use setSubroutine() to set a JS function from your JS code

Example

var rs = new RiveScript();

var 
extraFunction = function() {
     
return "A function that you can call from the JS file scope";
};

rs.setSubroutine("myJsFunction", function(rsargs{
    console
.log("hello world!");

    
// you can access any variables in the scope of this function
    
var test extraFunction();

    return 
"return value";
}); 

The body of the function in `setSubroutine()` would be the same as in the `>object` in RiveScript, but your function would have access to any variables/functions in its parent scopes too (which you can’t get from the `>object` version)

2) Use the `scope` parameter in rs.reply()

Another way to handle scope of external variables/functions with the `>object` syntax, is to pass the scope variable `this` into the reply() function, like

var reply rs.reply(usernamemessagethis); 

And then from inside the JS object macro, the `this` variable would be the very same `this` as what got passed in (and it has access to other local functions/attributes).

There’s a full example of the scope parameter here: https://github.com/aichaos/rivescript-js/tree/master/eg/scope

 

 
  login or register to react