superscriptjs / superscript

A dialogue engine for creating chat bots
http://superscriptjs.com
MIT License
1.65k stars 209 forks source link

Filters cannot filter out replies as expect #348

Closed lijiarui closed 7 years ago

lijiarui commented 7 years ago

Using filter to filter out replies as wiki filter example to filter out replies but failed.

Expected Behavior

using the following script:

+ * 
- {^test(true)} how are you
- hello

test() as follows:

exports.test = async function test(cb) {
    const content = this.message.original
    console.log(content)
    if (content === 'test') {
        cb(null, true)
    } else {
        cb(null, false)
    }
}

when I type test I wish it will return how are you

Current Behavior

When I type test it occurs the following error

/Users/lijiarui/Dropbox/git/superscript/myBotName/node_modules/.1.0.2@ss-parser/lib/wordnet.js:28
  throw err;
  ^

TypeError: cb is not a function
    at Object.test (/Users/lijiarui/Dropbox/git/superscript/myBotName/plugins/index.plugins.js:46:9)
    at /Users/lijiarui/Dropbox/git/superscript/myBotName/node_modules/.1.1.0@superscript/lib/bot/utils.js:114:29
    at new Promise (/Users/lijiarui/Dropbox/git/superscript/myBotName/node_modules/.0.22.2@es6-shim/es6-shim.js:1452:9)
    at Object.<anonymous> (/Users/lijiarui/Dropbox/git/superscript/myBotName/node_modules/.1.1.0@superscript/lib/bot/utils.js:109:12)
    at Generator.next (<anonymous>)
    at step (/Users/lijiarui/Dropbox/git/superscript/myBotName/node_modules/.1.1.0@superscript/lib/bot/utils.js:25:191)
    at /Users/lijiarui/Dropbox/git/superscript/myBotName/node_modules/.1.1.0@superscript/lib/bot/utils.js:25:437
    at new Promise (/Users/lijiarui/Dropbox/git/superscript/myBotName/node_modules/.0.22.2@es6-shim/es6-shim.js:1452:9)
    at Object.<anonymous> (/Users/lijiarui/Dropbox/git/superscript/myBotName/node_modules/.1.1.0@superscript/lib/bot/utils.js:25:99)
    at Object.runPluginFunc (/Users/lijiarui/Dropbox/git/superscript/myBotName/node_modules/.1.1.0@superscript/lib/bot/utils.js:119:17)
    at /Users/lijiarui/Dropbox/git/superscript/myBotName/node_modules/.1.1.0@superscript/lib/bot/getReply/filterFunction.js:52:47
    at Generator.next (<anonymous>)
    at step (/Users/lijiarui/Dropbox/git/superscript/myBotName/node_modules/.1.1.0@superscript/lib/bot/getReply/filterFunction.js:27:191)
    at /Users/lijiarui/Dropbox/git/superscript/myBotName/node_modules/.1.1.0@superscript/lib/bot/getReply/filterFunction.js:27:361
    at /Users/lijiarui/Dropbox/git/superscript/myBotName/node_modules/.0.22.2@es6-shim/es6-shim.js:1409:18
    at Immediate.<anonymous> (/Users/lijiarui/Dropbox/git/superscript/myBotName/node_modules/.0.22.2@es6-shim/es6-shim.js:1382:26)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)

Possible Solution

When I try the following script: (change - {^test(true)} how are you to - {^test()} how are you)

+ * 
- {^test()} how are you
- hello

It works well.

So I feel confused about this, because wiki shows as follows, it use {^functionX(true)} instead of {^functionX()}

+ i am *1
- {^functionX(true)}  Yes, you are <cap>.
- {^functionX(false)} You’re lying, you’re not <cap>!

Need some help, Thanks!

bensalilijames commented 7 years ago

Could it be possible that true is misspelt as trues - as mentioned in stack trace? :)

lijiarui commented 7 years ago

sorry for the typos error, I pasted wrong error.... The error is TypeError: cb is not a function and I just modified the log in the issue.

lijiarui commented 7 years ago

Thanks for @silentrob

I think your function signature is not defining the bool you are passing in. It should be 'test=function (bool,cb){...}'

I use the following function and all works well.

exports.test = async function test(bool, cb) {
    const content = this.message.original
    console.log(content)
    if (content === 'test') {
        cb(null, true)
    } else {
        cb(null, false)
    }
}
lijiarui commented 7 years ago

Sorry here again... The same code as follows:

+ *
- {^test(true)} yes it is test.
- {^test(false)} no, you are wrong.
exports.test = async function test(bool, cb) {
    const content = this.message.original
    if (content === 'test') {
        cb(null, true)
    } else {
        cb(null, false)
    }
}

It doesn't occur error, and if I send test, all goes well, it return yes it is test

But when I send other words like hhhh, I expected to get no, you are wrong.

But I got null reply, json as follows:

{
  "message": "",
  "reply": {
    "replyId": null,
    "createdAt": 1496538340005,
    "string": "",
    "topicName": null,
    "subReplies": [],
    "debug": {
      "user_id": "user1",
      "raw_input": "test2",
      "normalized_input": "test2",
      "matched_gambit": [],
      "timestamp": "2017-06-04T01:05:38.998Z"
    }
  }
}
lijiarui commented 7 years ago

It seems duplicate with issue 316

I found this

+ *
- {^myName("Ben")} a
- {^myName("Rob")} b
- {^myName("Alice")} ^saveSomething(<cap1>)
- {^myName("David")} ^doSomething() c

So how could I write function myName() to post Ben?

duffrind commented 7 years ago

Changing the plugin to:

exports.test = function(bool, cb) {
    const content = this.message.original
    if (content === 'test') {
        cb(null, bool === 'true')
    } else {
        cb(null, bool === 'false')
    }
}

and the main.ss file to:

+ *
- {^test("true")} yes it is test.
- {^test("false")} no, you are wrong.

Makes filtering work again.

lijiarui commented 7 years ago

@duffrind Oh! Thanks, it works!