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

205 lines
7.1 KiB
JavaScript

var alexa = require ('alexa-app');
const config = require ('../config/config');
var databaseHelper = require ('../helpers/database');
//User data for sending message, this is skill-related and will be in some skill container
var Name = null;
var Email = null;
var Message = null;
var State = 0; // states should be defined in seperate file. (Not sending message, Waiting for name, Waiting for email, Waiting for message)
//For now (this is not long term solution)
// 0 : Not sending Message
// 1 : Waiting for name
// 2 : Waiting for email
// 3 : Waiting for message
var alexaApp = new alexa.app ('saburly'); // this means we still work with one skill
module.exports = {
init: function (express) {
alexaApp.express ({
expressApp: express,
// verifies requests come from amazon alexa. Must be enabled for production.
// You can disable this if you're running a dev environment and want to POST
// things to test behavior. enabled by default.
checkCert: false,
// sets up a GET route when set to true. This is handy for testing in
// development, but not recommended for production. disabled by default
debug: true,
});
},
updateIntentsJSON: function () {
databaseHelper
.getSkill (config.SKILL_DB_ID)
.then (skill => {
skill.intents.map (intent => {
alexaApp.intent (
intent.intentName,
{
slots: [],
utterances: intent.questions,
},
function (request, response) {
return response.say (intent.answer).shouldEndSession (false);
}
);
});
alexaApp.launch ((request, response) => {
return response.say (skill.invocationAnswer).shouldEndSession (false);
});
alexaApp.intent (
'EmailIntentLaunch',
{
slots: [],
utterances: [
'I want to send a message',
'I would like to send a message',
'I would like to leave a message',
'Leave a message',
],
},
function (request, response) {
Name = null;
Email = null;
Message = null;
State = 1;
return response
.say ('Ok. What is your name')
.shouldEndSession (false);
}
);
//TODO : Watch out for this intent. It will make trouble with other regular intents
//if other intents have utterance with just one slot like {Data}
//It should be taken care somwhere before this, to check if Email Intent is invoked
//This is problem only if we introduce slot options for regular intents for users
alexaApp.intent (
'EmailIntent',
{
slots: {
Name: 'AMAZON.US_FIRST_NAME',
Email: 'AMAZON.LITERAL',
Message: 'AMAZON.LITERAL',
Data: 'AMAZON.LITERAL',
},
utterances: [
'My name is {-|Name}',
'I am {-|Name}',
'{dawdw at dwd wdw|Data}',
'My email is {wadwwdw at wadwwd wdw|Email}',
'Send replay to {fkofkeofe at dppfam wd|Email}',
'My message is {Quick brown fox jumps over lazy dog|Message}',
],
},
function (request, response) {
let Data = undefined;
try {
if (!Name) Name = request.slot ('Name');
} catch (e) {
console.log ('Error. No name slot ');
Name = undefined;
}
try {
if (!Email) Email = request.slot ('Email');
} catch (e) {
console.log ('Error. No Email slot');
Email = undefined;
}
try {
if (!Message) Message = request.slot ('Message');
} catch (e) {
console.log ('Error. No Message slot');
Message = undefined;
}
try {
Data = request.slot ('Data');
} catch (e) {
console.log ('Error. No Data slot');
Data = undefined;
}
console.log ('State : ' + State);
//TODO : Responses could be configurable for each skill ?
if (State === 1) {
//Was waiting for name, so if Name is null, name is probably in Data
if ((!Name && Data) || Name) {
//got the name, let's continue for the email
if (!Name) Name = Data;
State = 2;
return response
.say ('Ok ' + Name + '. What is your email ?')
.shouldEndSession (false);
} else {
//Something is wrong, ask for name again
return response
.say (
'Sorry, I didnt understand your name. Can you say it again ?'
)
.shouldEndSession (false);
}
} else if (State === 2) {
//was waiting for email, so if Email is null, email is probably in Data
if ((!Email && Data) || Email) {
//Got the email, first verify email and than continue to message
if (!Email) Email = Data;
//TODO : verify email
State = 3;
return response
.say ('Great. Whats the message ?')
.shouldEndSession (false);
} else {
//Something is wrong, ask for the email again
return response
.say (
'Sorry, I didnt understan you email. Can you say it again ?'
)
.shouldEndSession (false);
}
} else if (State === 3) {
//Was waiting for message, so if Message is null, message is probably in Data
if ((!Message && Data) || Message) {
//Ok, we got all informations. Exit email intent
if (!Message) Message = Data;
State = 0;
//TODO : Send email
console.log (
'Name : ' +
Name +
' | Email : ' +
Email +
' | Message : ' +
Message
);
return response.say (
'Message sent. Someone will contact you ASAP'
);
} else {
//Something is wrong, ask for the message again
return response
.say (
'Sorry, I didnt understand your message. Can you say it again ?'
)
.shouldEndSession (false);
}
} else {
console.log ('State strange ! ' + State);
}
}
);
})
.catch (err => {
console.log (err);
alexaApp.launch ((request, response) => {
return response.say ('Sorry, there was no skill with that name');
});
});
},
};