Open kirsle opened 7 years ago
Wow. This is old but I overcame this issue five years ago -- sorry I didn't see this until now. My rivescript looks like this:
+ tell me what time it is
- LocalCommand "date"
+ run a custom script to get * from the database
- LocalCommand /var/tmp/bin/mycustomscript -u root -d Opetion1 -m <star1>
When the chatbot sees the first word of the response is a LocalCommand (You can change the name of this keyword in the settings file). It knows to exec everything after this. THis can be easily executed a better way than I did originally by using github.com/bitfield script: <determine if firstword is 'LocalCommand' then run:
cmd := strings.Split(returnMessage, " ")[1:] // get everything after LocalCommand
returnMessage ,err:= script.Exec(cmd).String()
This allows my chatbot users to create commands in ANY language they prefer, or use standard Linux commands. I also have a version of this for 'RemoteCommand' that executes them on other servers using SSH.
@rmasci I've done similar before, like this example to run Perl object macros for the Python version of RiveScript: https://github.com/aichaos/rivescript-python/tree/master/eg/perl-objects
The various RiveScript versions have a SetHandler() function to register custom programming language hooks for non-native languages (up to the programmer to implement how those hooks work). The Python module also has a JavaScript example for bots that run out of a web browser environment (so the JS macros are run using embedded <script>
tags in the user's own web browser): https://github.com/aichaos/rivescript-python/tree/master/eg/js-objects
Being that Go sits at a close level to C and can embed CPython (or Perl or other C-based languages) it may be possible to get a wide variety of languages "natively" supported without shell commands calling out to third party scripts to bridge the gap. Though on that latter idea, I was once playing with the idea of defining a 'standard' interface for all RiveScript libraries to be able to interact with any third party language (e.g., by a standard format for input/output similar to the CGI standard for web scripts) -- with the idea that hacks like that perl-objects example didn't need to be done in an ad-hoc basis by individual developers but that somebody could take a Perl wrapper (e.g.) and use it on any of the 5 RiveScript libraries with built-in support for the protocol.
In more recent years I have some further ideas that could be used:
> object * go
in-line macros because Go can't evaluate itself at runtime and Yaegi seems to be at least a 99% compatible Go interpreted language which could support in-line Go object macros.
It'd be nice if rivescript-go were able to parse and run Python object macros for RiveScript bots, by using the Python C API.
There are two projects I found so far: sbinet/go-python and qur/gopy. They both only support Python 2 so far, but that will work for now.
I did some experimenting and came up with the following Go code that demonstrates the key pieces of functionality needed: dynamically parse a Python function, call the function giving it an array of string arguments, and retrieve the result of the function as a Go string.
This code lets us:
*args
tuple, converting Go strings into Python strings for theargs
argument (which is alist(str)
type)The other huge TODO is the
rs
parameter to the Python function: the above code sends in aNoneType
, but IRL it would need to provide an object with at least a subset of the RiveScript API (most importantly, functions likers.current_user()
andrs.set_uservar()
& friends). I imagine to expose the full RiveScript API to Python I'd need to write a whole wrapper class that translates all the arguments to/from Python types, but I'll probably just focus on the aforementioned useful functions to start out with.