dialogflow-javascript-client
dialogflow-javascript-client copied to clipboard
Prompts not working
Hi!
I have a test page with both the iFrame based demo chatbot and an implementation using apiai-javascript-client, side by side.
I have a weather fulfillment example running like in the api.ai docs. This gives the weather based on city + date. I have the date set up to be required with a prompt "On what day?".
On the iFrame based one it works correctly:
what is the weather in sydney? On what day? sunday The weather forecast for the City of Sydney, Australia on 2017-10-01 is Sunny with a projected high of 22°C or 72°F and a low of 15°C or 58°F.
On the apiai-javascript-client one this happens:
what is the weather in sydney? On what day? sunday I missed what you said. Say it again?
So the context of the conversation does not appear to be maintained? Is this not supported, or am I doing it wrong?
I'll include an excerpt from my code below.
Thanks!
var token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
//handles call and response from chatbot API
function askQuestion(question) {
log('Calling API');
const client = new ApiAi.ApiAiClient({accessToken: token});
const promise = client.textRequest(question);
promise
.then(handleResponse)
.catch(handleError);
function handleResponse(serverResponse) {
log('Response OK');
var answer = serverResponse.result.fulfillment.speech;
$('#question-answer').text(answer);
$('#question-text').val('');
log('Done');
}
function handleError(serverError) {
log('ERROR ON API CALL');
log(serverError);
}
}
//logs messages with appending on by default
function log(message, clearLog = false) {
if(clearLog) {
$('#question-log').text(message)
}
else {
$('#question-log').append('<br />' + message);
}
}
//main
$(document).ready(
function() {
//handle button click
$('#question-button').click(
function() {
$('#question-answer').text('');
log('Started', true);
var question = $('#question-text').val().trim();
if(question==='') {
log('Validation failed');
}
else {
askQuestion(question);
}
}
);
}
);
Okay, after a coupe of weeks I came back to this and saw the problem immediately.
This line const client = new ApiAi.ApiAiClient({accessToken: token}); ...needed to be moved out of the askQuestion function, so that the client is only created once per session. Then the context works fine.