Files
old-tellall/backend/models/alexa.js

296 lines
10 KiB
JavaScript
Raw Normal View History

2018-01-18 21:33:44 +01:00
var Alexa = require ('alexa-sdk');
const config = require ('../config/config');
var databaseHelper = require ('../helpers/database');
2018-01-19 16:29:47 +01:00
var emailHelper = require ('../helpers/email');
2018-01-18 22:07:23 +01:00
const constants = require ('../config/constants');
let predefinedSourceHelper = require ('../helpers/externalSource');
2018-01-18 21:33:44 +01:00
var handlers = {};
2018-01-19 16:29:47 +01:00
var destinationEmail;
2018-04-03 14:19:07 +02:00
let skillName;
2018-01-18 21:33:44 +01:00
module.exports = {
run: function (req, res) {
2018-01-18 21:43:25 +01:00
// 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
},
};
2018-01-18 21:33:44 +01:00
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 = {};
2018-01-19 16:29:47 +01:00
destinationEmail = activeSkill.contactEmail;
2018-04-03 14:19:07 +02:00
skillName = activeSkill.invocationName;
2018-01-18 22:07:23 +01:00
2018-01-23 00:08:39 +01:00
let listOfPossibleQuestions = '';
2018-01-23 13:24:56 +01:00
activeSkill.intents.forEach (intent => {
2018-01-23 01:46:02 +01:00
if (intent.questions.length > 0 && intent.intentExplanation) {
2018-01-23 00:08:39 +01:00
listOfPossibleQuestions +=
intent.intentExplanation +
intent.questions[0] +
2018-01-23 01:56:07 +01:00
'<break time="' +
constants.voiceResponseTimings.PAUSE_BETWEEN_QUESTIONS +
'ms"/>';
2018-01-23 00:08:39 +01:00
}
});
listOfPossibleQuestions +=
'If you dont know what to do, just say help or stop';
2018-01-23 00:08:39 +01:00
2018-01-23 02:15:44 +01:00
//Handler for launch requestconsole.log()
2018-01-18 21:33:44 +01:00
handlers = {
LaunchRequest: function () {
2018-01-18 21:56:56 +01:00
this.response
.speak (
activeSkill.invocationAnswer +
2018-01-23 01:56:07 +01:00
'<break time="' +
constants.voiceResponseTimings.PAUSE_AFTER_WELCOME_MESSAGE +
'ms"/>' +
2018-01-23 14:40:01 +01:00
'Would you like to hear list of questions that you can ask me'
)
2018-01-18 22:07:23 +01:00
.listen (constants.voiceResponseStrings.GENERIC_CONTINUE); //Phrase from listen doesn't work !!!
this.attributes['LaunchRequestYesNo'] = true;
2018-01-18 21:33:44 +01:00
this.emit (':responseReady');
},
};
2018-01-18 22:07:23 +01:00
//Handlers for user defined questions
2018-01-18 21:33:44 +01:00
activeSkill.intents.map (intent => {
handlers[intent.intentName] = function () {
if (this.attributes['LaunchRequestYesNo']) {
2018-01-23 14:40:01 +01:00
this.attributes['LaunchRequestYesNo'] = false;
}
2018-03-30 14:30:30 +02:00
let answerPromiseProps = {
resolve: null,
reject: null,
};
2018-03-30 14:30:30 +02:00
2018-04-03 14:19:07 +02:00
let answerPromise = new Promise ((resolve, reject) => {
2018-03-30 14:30:30 +02:00
answerPromiseProps = {
resolve: resolve,
reject: reject,
};
2018-03-30 14:30:30 +02:00
});
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;
2018-03-28 15:51:10 +02:00
}
2018-04-06 22:33:53 +02:00
answerPromise
.then (answer => {
this.response
.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');
});
2018-01-18 21:33:44 +01:00
};
});
2018-01-18 22:07:23 +01:00
2018-01-19 07:43:10 +01:00
//Handler for sending message
handlers.SendMessageIntent = function () {
if (this.attributes['LaunchRequestYesNo']) {
2018-01-23 14:40:01 +01:00
this.attributes['LaunchRequestYesNo'] = false;
}
2018-01-24 13:02:46 +01:00
2018-01-19 08:00:51 +01:00
let intent = this.event.request.intent;
console.log ('Dialog state : ' + this.event.request.dialogState);
console.log (intent);
2018-01-19 16:29:47 +01:00
//STARTED, IN_PROGRESS
2018-01-19 08:00:51 +01:00
2018-01-19 16:29:47 +01:00
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) {
2018-01-19 20:23:50 +01:00
intent.slots.Email.value = emailHelper.transformEmailFromAlexaResponse (
intent.slots.Email.value
);
if (!emailHelper.isEmailValid (intent.slots.Email.value)) {
2018-01-19 16:29:47 +01:00
//Email is not valid, ask again
2018-01-19 08:00:51 +01:00
const slotToElicit = 'Email';
2018-01-19 16:29:47 +01:00
const speechOutput =
'Sorry, that was not valid email. What is your email';
2018-01-19 08:00:51 +01:00
const repromptSpeech = speechOutput;
this.emit (
':elicitSlot',
slotToElicit,
speechOutput,
2018-01-19 20:28:26 +01:00
repromptSpeech
2018-01-19 08:00:51 +01:00
);
2018-01-19 16:29:47 +01:00
} else {
//Email is valid
2018-01-19 08:00:51 +01:00
const slotToElicit = 'Message';
2018-01-19 16:29:47 +01:00
const speechOutput = 'Great. What is your message';
2018-01-19 08:00:51 +01:00
const repromptSpeech = speechOutput;
this.emit (
':elicitSlot',
slotToElicit,
speechOutput,
repromptSpeech
);
}
2018-01-19 16:29:47 +01:00
} 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);
2018-01-19 20:23:50 +01:00
emailHelper
2018-01-23 12:18:20 +01:00
.sendEmail (
2018-01-19 20:23:50 +01:00
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');
});
2018-01-19 08:00:51 +01:00
}
2018-01-19 07:43:10 +01:00
};
2018-01-23 13:24:56 +01:00
//Built-In intents
2018-01-23 13:46:02 +01:00
handlers.CancelIntent = function () {
if (this.attributes['LaunchRequestYesNo']) {
2018-01-23 14:40:01 +01:00
this.attributes['LaunchRequestYesNo'] = false;
}
2018-04-03 14:19:07 +02:00
this.response.speak (`Thank you for using ${skillName}`);
2018-01-23 13:24:56 +01:00
this.emit (':responseReady');
};
2018-01-23 13:46:02 +01:00
handlers.HelpIntent = function () {
if (this.attributes['LaunchRequestYesNo']) {
2018-01-23 14:40:01 +01:00
this.attributes['LaunchRequestYesNo'] = false;
}
2018-01-23 13:46:02 +01:00
this.response
.speak (listOfPossibleQuestions)
.listen (constants.voiceResponseStrings.GENERIC_CONTINUE); //Phrase from listen doesn't work !!!
this.emit (':responseReady');
2018-01-23 13:24:56 +01:00
};
handlers.YesIntent = function () {
if (this.attributes['LaunchRequestYesNo']) {
2018-01-23 14:40:01 +01:00
this.attributes['LaunchRequestYesNo'] = false;
this.emit ('HelpIntent');
} else {
this.response
.speak (constants.voiceResponseStrings.DIDNT_ASK_ANYTHING)
.listen (constants.voiceResponseStrings.GENERIC_CONTINUE);
this.emit (':responseReady');
2018-01-23 14:40:01 +01:00
}
};
2018-01-23 14:40:01 +01:00
handlers.NoIntent = function () {
if (this.attributes['LaunchRequestYesNo']) {
2018-01-23 14:40:01 +01:00
this.attributes['LaunchRequestYesNo'] = false;
this.response
.speak ('')
.listen (constants.voiceResponseStrings.GENERIC_CONTINUE);
this.emit (':responseReady');
} else {
this.response
.speak (constants.voiceResponseStrings.DIDNT_ASK_ANYTHING)
.listen (constants.voiceResponseStrings.GENERIC_CONTINUE);
this.emit (':responseReady');
2018-01-23 14:40:01 +01:00
}
};
2018-01-23 14:40:01 +01:00
//Default handler for unknown question
2018-01-18 22:07:23 +01:00
handlers.Unhandled = function () {
this.response
.speak (constants.voiceResponseStrings.QUESTION_NOT_FOUND)
.listen (constants.voiceResponseStrings.GENERIC_CONTINUE);
2018-01-19 07:43:10 +01:00
};
2018-01-18 22:07:23 +01:00
2018-01-23 14:40:01 +01:00
//Session handlers
2018-01-19 07:43:10 +01:00
handlers.SessionEndedRequest = function () {
2018-01-18 22:07:23 +01:00
//We don't care for now
2018-01-19 07:43:10 +01:00
};
2018-01-18 21:33:44 +01:00
})
.catch (e => {
//Something is wrong, skill is not ready, use catch-all intent to inform user
console.log ('Error. Skill doesnt exist');
});
},
};