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'); let predefinedSourceHelper = require ('../helpers/externalSource'); var handlers = {}; var destinationEmail; let skillName; 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; skillName = activeSkill.invocationName; let listOfPossibleQuestions = ''; activeSkill.intents.forEach (intent => { if (intent.questions.length > 0 && intent.intentExplanation) { listOfPossibleQuestions += intent.intentExplanation + intent.questions[0] + ''; } }); 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 + '' + 'Would you like to hear list of questions that you can ask me' ) .listen (constants.voiceResponseStrings.GENERIC_CONTINUE); 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; } let answerPromiseProps = { resolve: null, reject: null, }; let answerPromise = new Promise ((resolve, reject) => { answerPromiseProps = { resolve: resolve, reject: reject, }; }); switch (intent.answerType) { case constants.answerType.PREDEFINED: answerPromiseProps.resolve (intent.answer); break; case constants.answerType.EXTERNAL_SOURCE_WP_TITLES: predefinedSourceHelper .getAnswerFromWP ( intent.externalAnswerSource, constants.contentType.TITLES ) .then (answer => { answerPromiseProps.resolve (answer); }) .catch (error => { answerPromiseProps.reject (error); }); break; case constants.answerType.EXTERNAL_SOURCE_WP_NEWS: predefinedSourceHelper .getAnswerFromWP ( intent.externalAnswerSource, constants.contentType.NEWS ) .then (answer => { answerPromiseProps.resolve (answer); }) .catch (error => { answerPromiseProps.reject (error); }); break; } answerPromise .then (answer => { this.response .speak (answer); this.emit (':responseReady'); }) .catch (error => { this.response .speak (error); 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 ${skillName}`); this.emit (':responseReady'); }; handlers.HelpIntent = function () { if (this.attributes['LaunchRequestYesNo']) { this.attributes['LaunchRequestYesNo'] = false; } this.response .speak (listOfPossibleQuestions) .listen (constants.voiceResponseStrings.GENERIC_CONTINUE); 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); this.emit (':responseReady'); } }; handlers.NoIntent = function () { if (this.attributes['LaunchRequestYesNo']) { this.attributes['LaunchRequestYesNo'] = false; this.response .speak (''); this.emit (':responseReady'); } else { this.response .speak (constants.voiceResponseStrings.DIDNT_ASK_ANYTHING); this.emit (':responseReady'); } }; //Default handler for unknown question handlers.Unhandled = function () { this.response .speak (constants.voiceResponseStrings.QUESTION_NOT_FOUND); this.emit (':responseReady'); }; //Session handlers handlers.SessionEndedRequest = function () { this.response.speak (`Thank you for using ${skillName}`); this.emit (':responseReady'); }; }) .catch (e => { //Something is wrong, skill is not ready, use catch-all intent to inform user console.log ('Error. Skill doesnt exist'); }); }, };