231 lines
8.1 KiB
JavaScript
231 lines
8.1 KiB
JavaScript
var Alexa = require ('alexa-sdk');
|
|
const config = require ('../config/config');
|
|
var databaseHelper = require ('../helpers/database');
|
|
var emailHelper = require ('../helpers/email');
|
|
const constants = require ('../config/constants');
|
|
|
|
var handlers = {};
|
|
var destinationEmail;
|
|
|
|
module.exports = {
|
|
run: function (req, res) {
|
|
// Build the context manually, because Amazon Lambda is missing
|
|
var context = {
|
|
succeed: function (result) {
|
|
res.json (result);
|
|
},
|
|
fail: function (error) {
|
|
console.log (error);
|
|
//We could send error json from here
|
|
},
|
|
};
|
|
|
|
var alexa = Alexa.handler (req.body, context);
|
|
alexa.appId = config.SKILL_ID;
|
|
alexa.registerHandlers (handlers);
|
|
alexa.execute ();
|
|
},
|
|
updateModel: function () {
|
|
//Get info from database, and store it for faster response on intent
|
|
databaseHelper
|
|
.getSkill (config.SKILL_DB_ID)
|
|
.then (activeSkill => {
|
|
handlers = {};
|
|
destinationEmail = activeSkill.contactEmail;
|
|
|
|
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"/>';
|
|
}
|
|
});
|
|
listOfPossibleQuestions += 'If you dont know what to do, just say help or stop';
|
|
|
|
//Handler for launch requestconsole.log()
|
|
handlers = {
|
|
LaunchRequest: function () {
|
|
this.response
|
|
.speak (
|
|
activeSkill.invocationAnswer +
|
|
'<break time="' +
|
|
constants.voiceResponseTimings.PAUSE_AFTER_WELCOME_MESSAGE +
|
|
'ms"/>' +
|
|
'Would you like to hear list of questions that you can ask me'
|
|
)
|
|
.listen (constants.voiceResponseStrings.GENERIC_CONTINUE); //Phrase from listen doesn't work !!!
|
|
this.attributes['LaunchRequestYesNo'] = true;
|
|
this.emit (':responseReady');
|
|
},
|
|
};
|
|
|
|
//Handlers for user defined questions
|
|
activeSkill.intents.map (intent => {
|
|
handlers[intent.intentName] = function () {
|
|
if (this.attributes['LaunchRequestYesNo']){
|
|
this.attributes['LaunchRequestYesNo'] = false;
|
|
}
|
|
this.response
|
|
.speak (intent.answer)
|
|
.listen (constants.voiceResponseStrings.GENERIC_CONTINUE); //Phrase from listen doesn't work !!!
|
|
this.emit (':responseReady');
|
|
};
|
|
});
|
|
|
|
//Handler for sending message
|
|
handlers.SendMessageIntent = function () {
|
|
if (this.attributes['LaunchRequestYesNo']){
|
|
this.attributes['LaunchRequestYesNo'] = false;
|
|
}
|
|
|
|
let intent = this.event.request.intent;
|
|
|
|
console.log ('Dialog state : ' + this.event.request.dialogState);
|
|
console.log (intent);
|
|
//STARTED, IN_PROGRESS
|
|
|
|
if (!intent.slots.Name.value) {
|
|
//Name not defined yet, ask user for name
|
|
const slotToElicit = 'Name';
|
|
const speechOutput = 'What is your name';
|
|
const repromptSpeech = speechOutput;
|
|
this.emit (
|
|
':elicitSlot',
|
|
slotToElicit,
|
|
speechOutput,
|
|
repromptSpeech
|
|
);
|
|
} else if (!intent.slots.Email.value) {
|
|
//Name not defined yet, ask user for email
|
|
const slotToElicit = 'Email';
|
|
const speechOutput =
|
|
'Ok ' + intent.slots.Name.value + '. What is your email';
|
|
const repromptSpeech = speechOutput;
|
|
this.emit (
|
|
':elicitSlot',
|
|
slotToElicit,
|
|
speechOutput,
|
|
repromptSpeech
|
|
);
|
|
} else if (!intent.slots.Message.value) {
|
|
intent.slots.Email.value = emailHelper.transformEmailFromAlexaResponse (
|
|
intent.slots.Email.value
|
|
);
|
|
if (!emailHelper.isEmailValid (intent.slots.Email.value)) {
|
|
//Email is not valid, ask again
|
|
const slotToElicit = 'Email';
|
|
const speechOutput =
|
|
'Sorry, that was not valid email. What is your email';
|
|
const repromptSpeech = speechOutput;
|
|
this.emit (
|
|
':elicitSlot',
|
|
slotToElicit,
|
|
speechOutput,
|
|
repromptSpeech
|
|
);
|
|
} else {
|
|
//Email is valid
|
|
const slotToElicit = 'Message';
|
|
const speechOutput = 'Great. What is your message';
|
|
const repromptSpeech = speechOutput;
|
|
this.emit (
|
|
':elicitSlot',
|
|
slotToElicit,
|
|
speechOutput,
|
|
repromptSpeech
|
|
);
|
|
}
|
|
} else {
|
|
//all slots are filled
|
|
console.log ('Name : ' + intent.slots.Name.value);
|
|
console.log ('Email : ' + intent.slots.Email.value);
|
|
console.log ('Message : ' + intent.slots.Message.value);
|
|
emailHelper
|
|
.sendEmail (
|
|
intent.slots.Name.value,
|
|
intent.slots.Email.value,
|
|
intent.slots.Message.value,
|
|
destinationEmail
|
|
)
|
|
.then (info => {
|
|
console.log (info);
|
|
this.response.speak (
|
|
'Ok. Message sent. Someone will contact you ASAP'
|
|
);
|
|
this.emit (':responseReady');
|
|
})
|
|
.catch (error => {
|
|
console.log (error);
|
|
this.response.speak (
|
|
'Sorry, there was a problem with sending message.'
|
|
);
|
|
this.emit (':responseReady');
|
|
});
|
|
}
|
|
};
|
|
|
|
//Built-In intents
|
|
|
|
handlers.CancelIntent = function () {
|
|
if (this.attributes['LaunchRequestYesNo']){
|
|
this.attributes['LaunchRequestYesNo'] = false;
|
|
}
|
|
this.response.speak ('Thank you for using Saburly');
|
|
this.emit (':responseReady');
|
|
};
|
|
|
|
handlers.HelpIntent = function () {
|
|
if (this.attributes['LaunchRequestYesNo']){
|
|
this.attributes['LaunchRequestYesNo'] = false;
|
|
}
|
|
this.response
|
|
.speak (listOfPossibleQuestions)
|
|
.listen (constants.voiceResponseStrings.GENERIC_CONTINUE); //Phrase from listen doesn't work !!!
|
|
this.emit (':responseReady');
|
|
};
|
|
|
|
handlers.YesIntent = function(){
|
|
if (this.attributes['LaunchRequestYesNo']){
|
|
this.attributes['LaunchRequestYesNo'] = false;
|
|
this.emit('HelpIntent');
|
|
}else{
|
|
this.response.speak(constants.voiceResponseStrings.DIDNT_ASK_ANYTHING).listen(constants.voiceResponseStrings.GENERIC_CONTINUE);
|
|
this.emit(':responseReady');
|
|
}
|
|
}
|
|
|
|
handlers.NoIntent = function (){
|
|
if (this.attributes['LaunchRequestYesNo']){
|
|
this.attributes['LaunchRequestYesNo'] = false;
|
|
this.response.listen(constants.voiceResponseStrings.GENERIC_CONTINUE);
|
|
}else{
|
|
this.response.speak(constants.voiceResponseStrings.DIDNT_ASK_ANYTHING).listen(constants.voiceResponseStrings.GENERIC_CONTINUE);
|
|
this.emit(':responseReady');
|
|
}
|
|
}
|
|
|
|
//Default handler for unknown question
|
|
|
|
handlers.Unhandled = function () {
|
|
this.response
|
|
.speak (constants.voiceResponseStrings.QUESTION_NOT_FOUND)
|
|
.listen (constants.voiceResponseStrings.GENERIC_CONTINUE);
|
|
};
|
|
|
|
//Session handlers
|
|
|
|
handlers.SessionEndedRequest = function () {
|
|
//We don't care for now
|
|
};
|
|
})
|
|
.catch (e => {
|
|
//Something is wrong, skill is not ready, use catch-all intent to inform user
|
|
console.log ('Error. Skill doesnt exist');
|
|
});
|
|
},
|
|
};
|