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

alternative in a conditional #351

Closed enauman closed 3 years ago

enauman commented 4 years ago

I'm looking for an "or" feature to use in a conditional. It seems like the "alternative" feature should function this way but just results in the test failing. Is there a way to include an "or" structure in a conditional? The following example seems like it should work but gets passed over:

* <star> == (scary|horror) => I can't watch scary movies!

kirsle commented 3 years ago

That sort of syntax doesn't work unfortunately.

As a work-around you can write an object macro to do this:

> object in javascript
    if (args.length < 2) {
        return 'false';
    }
    let test = args[0];
    for (let i = 1; i < args.length; i++) {
        if (test === args[i]) {
            return 'true';
        }
    }
    return 'false';
< object

+ is the magic word *
* <call>in "<star>" "hocus pocus" "abra kadabra"</call> == true => That's one of them!
- That's not any of the magic words.

Live demo: https://play.rivescript.com/s/Cwsyak08VN

Note in the <call> I put quotes around the <star> and around the multi-word comparison checks; single word checks work too without the quotes. Quotes in a <call> tag act like quotes in a command-line environment, letting a multi-word (w/ spaces) to come in as a single element in the args array.