promisify answer on user defined question

This commit is contained in:
GotPPay
2018-03-30 12:08:38 +02:00
parent f163dde5b2
commit 0ebadcd3f7
2 changed files with 25 additions and 9 deletions

View File

@@ -33,10 +33,10 @@ getDataFromWPJSON = function (sourceUrl, page = 1, maxPosts = 10) {
} }
module.exports = { module.exports = {
getAnswerFromWP : async function (sourceUrl){ getAnswerFromWP : function (sourceUrl){
//This function will extract needed data from JSON, which we got from getDataFromWPJSON //This function will extract needed data from JSON, which we got from getDataFromWPJSON
//At the moment, it's taking titles and creates answer //At the moment, it's taking titles and creates answer
return await new Promise((resolve,reject)=>{ return new Promise((resolve,reject)=>{
getDataFromWPJSON(sourceUrl).then(rawData=>{ getDataFromWPJSON(sourceUrl).then(rawData=>{
let result=''; let result='';
rawData.forEach(post=>{ rawData.forEach(post=>{

View File

@@ -71,22 +71,38 @@ module.exports = {
if (this.attributes['LaunchRequestYesNo']) { if (this.attributes['LaunchRequestYesNo']) {
this.attributes['LaunchRequestYesNo'] = false; this.attributes['LaunchRequestYesNo'] = false;
} }
let answer = ''; let answer;
switch (intent.answerType){ switch (intent.answerType){
case constants.answerType.PREDEFINED: case constants.answerType.PREDEFINED:
answer = intent.answer; answer = new Promise((resolve,reject)=>{
resolve(intent.answer);
});
break; break;
case constants.answerType.EXTERNAL_SOURCE_WP_JSON: case constants.answerType.EXTERNAL_SOURCE_WP_JSON:
answer = predefinedSourceHelper.getAnswerFromWP(intent.externalAnswerSource); predefinedSourceHelper.getAnswerFromWP(intent.externalAnswerSource).then(answer=>{
answer = new Promise((resolve,reject)=>{
resolve(answer);
});
}).catch(error=>{
reject(error);
});
break; break;
case constants.answerType.EXTERNAL_SOURCE_RSS: case constants.answerType.EXTERNAL_SOURCE_RSS:
answer = 'Not implemented yet' answer = 'Not implemented yet'
break; break;
} }
this.response
.speak (answer) answer.then(answer=>{
.listen (constants.voiceResponseStrings.GENERIC_CONTINUE); //Phrase from listen doesn't work !!! this.response
this.emit (':responseReady'); .speak (answer)
.listen (constants.voiceResponseStrings.GENERIC_CONTINUE); //Phrase from listen doesn't work !!!
this.emit (':responseReady');
}).catch(error=>{
this.response
.speak (error)
.listen (constants.voiceResponseStrings.GENERIC_CONTINUE); //Phrase from listen doesn't work !!!
this.emit (':responseReady');
});
}; };
}); });