aichaos / rivescript-js

A RiveScript interpreter for JavaScript. RiveScript is a scripting language for chatterbots.
https://www.rivescript.com/
MIT License
377 stars 145 forks source link

getUservars() always empty #380

Closed j2l closed 2 years ago

j2l commented 2 years ago

Hello there, Reading the doc and the JS example, I'm starting to understand the JS API and build a SPA (only browser, no server) bot. But getUservars() is always throwing empty object in the browser console:

The bot says: Hi. What seems to be your problem?
User: Hi
{}
Aiden: How do you do. Please state your problem.
User: my name is Zerezrez
{}
Aiden: Zerezrez, nice to meet you.

See getUservars() in:

function userentry (message) {
    bot.reply(username, message).then(function(reply) {
            console.log(JSON.stringify(bot.getUservars()))
      console.log("Aiden: " + reply);
      usermessage="";
    })
  }

Before the bot reply I want to see Uservars getting populated. And I can't use currentUser() since it's for macro, right?

I don't get why getUservars() returns an empty object while the bot keeps record of the user's name. Not "undefined" or "Promise" but empty.

I'm using local rivescript.min.js from https://unpkg.com/rivescript@2.2.0/dist/rivescript.min.js Node v14.18.2 (only to bundle)

What did I miss? Thanks!

j2l commented 2 years ago

My bad, {} is still a promise (but fulfilled)

async function userentry (message) {
    const usersdetails = await bot.getUservars()
...
j2l commented 2 years ago

Still not getting the user's name in the result :(

{
    "topic": "random",
    "__initialmatch__": "(hello|hi|hey|howdy|hola|hai|yo) [*]",
    "__history__": {
        "input": ["hello bot", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined"],
        "reply": ["Hi. What seems to be your problem?", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined"]
    },
    "__lastmatch__": "(hello|hi|hey|howdy|hola|hai|yo) [*]",
    "__last_triggers__": [{
        "trigger": "(hello|hi|hey|howdy|hola|hai|yo) [*]",
        "reply": ["How do you do. Please state your problem.", "Hi. What seems to be your problem?"],
        "condition": [],
        "redirect": null,
        "previous": null
    }]
}
j2l commented 2 years ago

Oh, the following interaction writes it to the uservars. I don't get how it replies using the user's name while not writing it to uservars.

j2l commented 2 years ago

OK, reply is the memory writing process. Must have async func following it.