I am using DialogFlow V1 node.js webhook and I have a problem. I have an intent that will connect to the Spotify API to get tracks for my chatbot:
const {DialogflowApp} = require('actions-on-google');
function spotifyIntent(app) {
//Get search parameters
[...]
return asyncFunction(query).then(function (message) {
this.app.ask(message);
return Promise.resolve();
}).catch(function (err) {
this.app.ask("Une erreur est apparue: "+err);
return Promise.resolve();
})
}
with my async function being:
function asyncFunction(query) {
spotifyApi.clientCredentialsGrant()
.then(function(data) {
// spotifyApi.setAccessToken(data.body['access_token']);
return spotifyApi.searchTracks(query);
}).then(function(data) {
const link = data.body.tracks.items[0].preview_url;
let speech = '<speak>Here is the first result found.<audio src="'+ link +'">I didn\'t found it</audio></speak>';
return Promise.resolve(speech);
}).catch(function(err) {
return Promise.reject(err);
});
}
A Promise is hidden in the call of clientCredentialsGrant() and searchTracks();
My intent is called by the classic map with action: intentFunction. I read here that the ask method should work but it doesn't for me. I tried the version with the Promise and with a callback but when I simulate my code I always get the answer:
"message": "Failed to parse Dialogflow response into AppResponse, exception thrown with message: Empty speech response
I don't understand what I'm doing wrong here? I know the problem come from the request being async but it should work fine with the Promise or the callback because right now it returns instantly?