Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajax call in javascript object macro #299

Open
hark444 opened this issue Jan 21, 2019 · 1 comment
Open

Ajax call in javascript object macro #299

hark444 opened this issue Jan 21, 2019 · 1 comment

Comments

@hark444
Copy link

hark444 commented Jan 21, 2019

Can anybody share a code snipped of how to make an ajax POST request in
Javascript object macro in a RiveScript...
Kirsle has talked about it here.
But this example doesn't work.
Or maybe there's some other way of making this work.

This is what I am trying to do:
image

This is the bot Response: "hello.[ERR: Object Not Found]"

@kirsle
Copy link
Member

kirsle commented Jan 22, 2019

Check your browser console for any errors. The "Object Not Found" probably meant your object macro failed to parse correctly in the first place (syntax error for example).

rs.getUservar() nowadays returns a Promise and not a string, so you'll need to await it or .then() it to use its value. In RiveScript.js 2.0.0, the inline object macros defined using > object are parsed in as async functions by default so you may use the await keyword in them, provided your calling code is also coming from an async function.

(example if unfamiliar with async/await)

// if you had a global bot in an `rs` variable like on a web page
var rs = new RiveScript();

// your function to get a reply needs to be an async function (well, your whole
// call stack has to be but usually you'll have one entry function of your own)
// rs.reply() is an `async function` and RiveScript takes care of the rest, but
// if your call stack isn't all async you can't use `await` -- check your console for
// that error
async function sendMessage(username, message) {
    // you can use the await keyword to wait synchronously on any Promise
    var reply = await rs.reply(username, message);
    return reply;
}

In that sort of setup the async function in the object macro is allowed to use the await keyword.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants