Assistant response not returned by VirtualGoogleAssistant object
Closed this issue · 13 comments
We followed the example given in the docs (slightly modified):
const vga = require("virtual-google-assistant");
const assistant = vga.VirtualGoogleAssistant.Builder()
.handler("index.handler") // Google Cloud Function file and name
.directory("./dialogFlowFolder") // Path to the Dialog Flow exported model
.create();
assistant.utter("KEYWORD").then((payload) => {
console.log("OutputSpeech: " + result.speech);
console.log("OutputSpeech: " + payload);
});
When we run our example test, we are able to see the proper response being logged in the respective files down the stack, but the log statements above in the tests are never triggered, thus making us unable to assert anything in our test.
We also tried the following
let response = virtualAssistant.utter('KEYWORD').then((payload) => {
return payload;
});
console.log('response = ' + JSON.stringify(response));
response is printed as '{}'
It seems that we are close, but not sure if this is a bug or error in the documentation (or we are doing something wrong) - any ideas?
what version of virtual-google-assistant are you using?
Can you try the async/await approach?
try {
const result = await assistant.utter("KEYWORD");
// asserts
} catch(e) {
console.log(e);
}
Hi @ecruzado, we are on version 0.2.4 version of virtual-google-assistant.
I tried this approach
const response = await assistant.utter('KEYWORD').then((result) => {
console.log("Result = " + result);
}).catch((ex) => {
console.log("Exception = " + ex);
});
console.log("Response = " + response);
I do not get any of the console log outputs but get an error which says Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
If I remove the 'await' from the approach and print out the response, I get an empty body for Response console log statement. But no console logs for Result or Exception.
const response = assistant.utter('KEYWORD').then((result) => {
console.log("Result = " + result);
}).catch((ex) => {
console.log("Exception = " + ex);
});
console.log("Response = " + JSON.stringify(response));
Output from the mocha test run is Response = {}
Are we missing anything here?
Can you share your project or at least a minimal version to try to reproduce the error?
@elizabetht did you solve your issue?
ok, but in the example you shared:
const response = assistant.utter('KEYWORD').then((result) => {
console.log("Result = " + result);
}).catch((ex) => {
console.log("Exception = " + ex);
});
console.log("Response = " + JSON.stringify(response));
The response that you are looking for is what you are seeing in the console.log result, if you are following that approach, you should assert inside the then condition, and then throw any error on the catch.
right now what your are outputing in the console.log for response is the returned value from the then.
Promises run asynchronously, and when they are finished the then executes with the result.
@jperata But we are not seeing anything getting printed for the line 'console.log("Result = " + result);
We only see the output of Response = {}
that probably means that your promise is finishing after your test ends, or that your internal code is not sending a response.
could you try a test with just this:
const vga = require("virtual-google-assistant");
const assistant = vga.VirtualGoogleAssistant.Builder()
.handler("index.handler") // Google Cloud Function file and name
.directory("./dialogFlowFolder") // Path to the Dialog Flow exported model
.create();
return assistant.utter("KEYWORD").then((payload) => {
console.log("payload: " + payload);
});
Returning the promise at the end will make the mocha test await for the end of the promise before ending the test.
We tried the above code you pasted. But we are getting the following error:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
ok, then it's most likely that the response is not being returned. If you can share me your google cloud function file, privately at juan@bespoken.io, i can give more input.
Hi @jperata, Just sent you the minimal version of our code. Let us know if you find anything wrong with it.
@elizabetht Our google function handler was based on an older implementation, we released a patch that works with the version (0.2.5) you are using. Please try updating your version of virtual-google-assistant and it should work as expected.
Thanks @jperata. We can now see the result come back!