166 lines
5.6 KiB
JavaScript
166 lines
5.6 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) {
|
|
console.log (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;
|
|
|
|
//Handler for launch request
|
|
handlers = {
|
|
LaunchRequest: function () {
|
|
this.response
|
|
.speak (activeSkill.invocationAnswer)
|
|
.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');
|
|
},
|
|
};
|
|
|
|
//Handlers for user defined questions
|
|
activeSkill.intents.map (intent => {
|
|
handlers[intent.intentName] = function () {
|
|
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 () {
|
|
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,
|
|
intent
|
|
);
|
|
} 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
|
|
.sendEmal (
|
|
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');
|
|
});
|
|
}
|
|
};
|
|
|
|
//Default handlers for unknown questions and session close
|
|
|
|
handlers.Unhandled = function () {
|
|
this.response
|
|
.speak (constants.voiceResponseStrings.QUESTION_NOT_FOUND)
|
|
.listen (constants.voiceResponseStrings.GENERIC_CONTINUE);
|
|
};
|
|
|
|
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');
|
|
});
|
|
},
|
|
};
|