Merge pull request #12 from GotPPay/question-explanation

Question explanation
This commit is contained in:
MirnaM
2018-01-23 12:19:14 +01:00
committed by GitHub
8 changed files with 347 additions and 161 deletions

View File

@@ -17,7 +17,7 @@ constants.apiResultCodes = {
AMAZON_FAIL:2, //amazon api doesn't work
DATABASE_ERROR:3,
NO_SKILL:4,
INCONSISTEN_STATE:5,
INCONSISTENT_STATE:5,
}
constants.HTTPResultCodes = {
@@ -31,6 +31,12 @@ constants.voiceResponseStrings = {
GENERIC_CONTINUE : 'Would you like to continue'
}
//Timing is given in [ms]
constants.voiceResponseTimings = {
PAUSE_BETWEEN_QUESTIONS : 650,
PAUSE_AFTER_WELCOME_MESSAGE : 650,
}
module.exports = constants;

View File

@@ -41,6 +41,7 @@ router.put ('/:id', bodyParser.json (), async (req, res, next) => {
.then (() => {
//Ok, done, now update skill on Amazon (if needed)
if (updateOnAmazon) {
//We need to update skill on Amazon
amazonHelper
.updateSkill (skill)
.then (amazonResult => {
@@ -51,45 +52,78 @@ router.put ('/:id', bodyParser.json (), async (req, res, next) => {
res.json ({result: constants.apiResultCodes.OK, message: ''});
alexa.updateModel ();
} else {
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({
result: constants.apiResultCodes.AMAZON_ERROR,
message: amazonResult,
});
//Update on amazon failed, revert changes in database and send error to user
databaseHelper
.updateSkill (id, currentSkillState)
.then (() => {
res
.status (
constants.HTTPResultCodes.INTERNAL_SERVER_ERROR
)
.json ({
result: constants.apiResultCodes.AMAZON_ERROR,
message: amazonResult,
});
})
.catch (() => {
//This should never happen, something is seriously wrong, like no database connection
res
.status (
constants.HTTPResultCodes.INTERNAL_SERVER_ERROR
)
.json ({
result: constants.apiResultCodes.INCONSISTENT_STATE,
message: '',
});
});
}
})
.catch (e => {
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({
result: constants.apiResultCodes.AMAZON_FAIL,
message: e,
});
//Update on amazon failed, revert changes in database and send error to user
databaseHelper
.updateSkill (id, currentSkillState)
.then (() => {
res
.status (constants.HTTPResultCodes.INTERNAL_SERVER_ERROR)
.json ({
result: constants.apiResultCodes.AMAZON_FAIL,
message: e,
});
})
.catch (() => {
//This should never happen, something is seriously wrong, like no database connection
res
.status (constants.HTTPResultCodes.INTERNAL_SERVER_ERROR)
.json ({
result: constants.apiResultCodes.INCONSISTENT_STATE,
message: '',
});
});
});
}else{
} else {
//No need to update on Amazon, tell to user it's ok
res.json ({result: constants.apiResultCodes.OK, message: ''});
alexa.updateModel ();
}
})
.catch (() => {
//Update in database didn't go well, revert changes
databaseHelper
.updateSkill (id, currentSkillState)
.then (() => {
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({
result: constants.apiResultCodes.DATABASE_ERROR,
message: '',
});
})
.catch (() => {
//This should never happen, something is seriously wrong, like no database connection
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({
result: constants.apiResultCodes.INCONSISTEN_STATE,
message: '',
});
});
//Update in database didn't go well, no need to revert since it failed to write in the first place
//just send error to user
res
.status (
constants.HTTPResultCodes.INTERNAL_SERVER_ERROR
)
.json ({
result: constants.apiResultCodes.DATABASE_ERROR,
message: '',
});
});
})
.catch (e => {
//I don't know why, but something went wrong, possibly ID of skill is wrong, doesn't exist in DB
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({result: constants.apiResultCodes.NO_SKILL, message: ''});
res
.status (constants.HTTPResultCodes.INTERNAL_SERVER_ERROR)
.json ({result: constants.apiResultCodes.NO_SKILL, message: ''});
});
});

View File

@@ -226,24 +226,23 @@ var generateInteractionModel = function (skill) {
},
];
let dialog = {
intents: dialogIntents,
};
result.interactionModel = {};
result.interactionModel.languageModel = {
invocationName: skill.invocationName,
types: customSlotTypes,
intents: allIntents,
prompts: dialogPrompts,
dialog: dialog,
};
result.interactionModel.prompts = dialogPrompts;
result.interactionModel.dialog = {};
result.interactionModel.dialog.intents = dialogIntents;
return JSON.stringify (result);
};
var uploadSkill = function (skill) {
let generatedInteractionModel = generateInteractionModel (skill);
return fetch (
`https://api.amazonalexa.com/v0/skills/${skill.skillID}/interactionModel/locales/en-US`,
{
@@ -251,7 +250,7 @@ var uploadSkill = function (skill) {
headers: {
Authorization: config.TOKEN,
},
body: generateInteractionModel (skill),
body: generatedInteractionModel,
}
);
};

View File

@@ -12,7 +12,6 @@ module.exports = {
// Build the context manually, because Amazon Lambda is missing
var context = {
succeed: function (result) {
console.log (result);
res.json (result);
},
fail: function (error) {
@@ -34,14 +33,32 @@ module.exports = {
handlers = {};
destinationEmail = activeSkill.contactEmail;
//Handler for launch request
let listOfPossibleQuestions = '';
activeSkill.intents.forEach(intent => {
if (intent.questions.length > 0 && intent.intentExplanation) {
listOfPossibleQuestions +=
intent.intentExplanation +
intent.questions[0] +
'<break time="' +
constants.voiceResponseTimings.PAUSE_BETWEEN_QUESTIONS +
'ms"/>';
}
});
console.log(listOfPossibleQuestions);
//Handler for launch requestconsole.log()
handlers = {
LaunchRequest: function () {
this.response
.speak (activeSkill.invocationAnswer)
.speak (
activeSkill.invocationAnswer +
'<break time="' +
constants.voiceResponseTimings.PAUSE_AFTER_WELCOME_MESSAGE +
'ms"/>' +
listOfPossibleQuestions
)
.listen (constants.voiceResponseStrings.GENERIC_CONTINUE); //Phrase from listen doesn't work !!!
//TODO : Maybe to ask user does he want to hear possible intents
//For this functionality, we need explanation text for each intent (Question)
this.emit (':responseReady');
},
};