improve dialog ; send email
This commit is contained in:
@@ -27,7 +27,7 @@ constants.HTTPResultCodes = {
|
|||||||
constants.SKILL_ID_LENGTH = 24;
|
constants.SKILL_ID_LENGTH = 24;
|
||||||
|
|
||||||
constants.voiceResponseStrings = {
|
constants.voiceResponseStrings = {
|
||||||
QUESTION_NOT_FOUND : 'Sorry, I didnt understan',
|
QUESTION_NOT_FOUND : 'Sorry, I didnt understand',
|
||||||
GENERIC_CONTINUE : 'Would you like to continue'
|
GENERIC_CONTINUE : 'Would you like to continue'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
58
backend/helpers/email.js
Normal file
58
backend/helpers/email.js
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
const nodemailer = require ('nodemailer');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
validateEmailFromAlexaResponse: function (email) {
|
||||||
|
//email from alexa response will contain words instead of symbols, like :
|
||||||
|
//at = @
|
||||||
|
//underscore = _
|
||||||
|
//dash = -
|
||||||
|
//dot = .
|
||||||
|
//TODO: This list should be longer
|
||||||
|
let transformedEmail = email
|
||||||
|
.replace (/at/gi, '@')
|
||||||
|
.replace (/underscore/gi, '_')
|
||||||
|
.replace (/dash/gi, '-')
|
||||||
|
.replace (/dot/gi, '.');
|
||||||
|
|
||||||
|
//Validate here with some email validation regex
|
||||||
|
|
||||||
|
//return true if email is valid
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
|
||||||
|
sendEmal: function (name, fromEmail, message, toEmail) {
|
||||||
|
return new Promise ((resolve, reject) => {
|
||||||
|
let messageBody =
|
||||||
|
'Hello. User ' +
|
||||||
|
name +
|
||||||
|
' left you a message on Saburly service using Alexa. Content of the message : ' +
|
||||||
|
message;
|
||||||
|
|
||||||
|
let transporter = nodemailer.createTransport ({
|
||||||
|
host: 'smtp.yandex.com',
|
||||||
|
port: 465,
|
||||||
|
secure: true,
|
||||||
|
auth: {
|
||||||
|
user: 'saburly@yandex.com',
|
||||||
|
pass: 'WeAreSaburlyTeam',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
var mailOptions = {
|
||||||
|
from: fromEmail,
|
||||||
|
replyTo: fromEmail,
|
||||||
|
to: toEmail,
|
||||||
|
subject: 'Message from Saburly service',
|
||||||
|
text: messageBody,
|
||||||
|
};
|
||||||
|
|
||||||
|
transporter.sendMail (mailOptions, (error, info) => {
|
||||||
|
if (error) {
|
||||||
|
reject (error);
|
||||||
|
} else {
|
||||||
|
resolve (info);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
var Alexa = require ('alexa-sdk');
|
var Alexa = require ('alexa-sdk');
|
||||||
const config = require ('../config/config');
|
const config = require ('../config/config');
|
||||||
var databaseHelper = require ('../helpers/database');
|
var databaseHelper = require ('../helpers/database');
|
||||||
|
var emailHelper = require ('../helpers/email');
|
||||||
const constants = require ('../config/constants');
|
const constants = require ('../config/constants');
|
||||||
|
|
||||||
var handlers = {};
|
var handlers = {};
|
||||||
|
var destinationEmail;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
run: function (req, res) {
|
run: function (req, res) {
|
||||||
@@ -30,6 +32,7 @@ module.exports = {
|
|||||||
.getSkill (config.SKILL_DB_ID)
|
.getSkill (config.SKILL_DB_ID)
|
||||||
.then (activeSkill => {
|
.then (activeSkill => {
|
||||||
handlers = {};
|
handlers = {};
|
||||||
|
destinationEmail = activeSkill.contactEmail;
|
||||||
|
|
||||||
//Handler for launch request
|
//Handler for launch request
|
||||||
handlers = {
|
handlers = {
|
||||||
@@ -58,36 +61,42 @@ module.exports = {
|
|||||||
let intent = this.event.request.intent;
|
let intent = this.event.request.intent;
|
||||||
|
|
||||||
console.log ('Dialog state : ' + this.event.request.dialogState);
|
console.log ('Dialog state : ' + this.event.request.dialogState);
|
||||||
console.log ('Intent object :');
|
|
||||||
console.log (intent);
|
console.log (intent);
|
||||||
|
//STARTED, IN_PROGRESS
|
||||||
|
|
||||||
if (this.event.request.dialogState === 'STARTED') {
|
if (!intent.slots.Name.value) {
|
||||||
//Should this be in constants ?
|
//Name not defined yet, ask user for name
|
||||||
if (!intent.slots.Name.value) {
|
const slotToElicit = 'Name';
|
||||||
//Name not defined yet, ask user for name
|
const speechOutput = 'What is your name';
|
||||||
const slotToElicit = 'Name';
|
const repromptSpeech = speechOutput;
|
||||||
const speechOutput = 'What is your name';
|
this.emit (
|
||||||
const repromptSpeech = speechOutput;
|
':elicitSlot',
|
||||||
this.emit (
|
slotToElicit,
|
||||||
':elicitSlot',
|
speechOutput,
|
||||||
slotToElicit,
|
repromptSpeech
|
||||||
speechOutput,
|
);
|
||||||
repromptSpeech
|
} else if (!intent.slots.Email.value) {
|
||||||
);
|
//Name not defined yet, ask user for email
|
||||||
} else if (!intent.slots.Email.value) {
|
const slotToElicit = 'Email';
|
||||||
//Name not defined yet, ask user for 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) {
|
||||||
|
if (
|
||||||
|
!emailHelper.validateEmailFromAlexaResponse (
|
||||||
|
intent.slots.Email.value
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
//Email is not valid, ask again
|
||||||
const slotToElicit = 'Email';
|
const slotToElicit = 'Email';
|
||||||
const speechOutput = 'What is your email';
|
const speechOutput =
|
||||||
const repromptSpeech = speechOutput;
|
'Sorry, that was not valid email. What is your email';
|
||||||
this.emit (
|
|
||||||
':elicitSlot',
|
|
||||||
slotToElicit,
|
|
||||||
speechOutput,
|
|
||||||
repromptSpeech
|
|
||||||
);
|
|
||||||
} else if (!intent.slots.Message.value) {
|
|
||||||
const slotToElicit = 'Message';
|
|
||||||
const speechOutput = 'What is your message';
|
|
||||||
const repromptSpeech = speechOutput;
|
const repromptSpeech = speechOutput;
|
||||||
this.emit (
|
this.emit (
|
||||||
':elicitSlot',
|
':elicitSlot',
|
||||||
@@ -96,13 +105,37 @@ module.exports = {
|
|||||||
repromptSpeech
|
repromptSpeech
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
//all slots are filled
|
//Email is valid
|
||||||
console.log ('Name : ' + intent.slots.Name.value);
|
const slotToElicit = 'Message';
|
||||||
console.log ('Email : ' + intent.slots.Email.value);
|
const speechOutput = 'Great. What is your message';
|
||||||
console.log ('Message : ' + intents.slots.Message.value);
|
const repromptSpeech = speechOutput;
|
||||||
this.response.speak ('Ok. Someone will contact you ASAP');
|
this.emit (
|
||||||
this.emit (':responseReady');
|
':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=>{
|
||||||
|
this.response.speak (
|
||||||
|
'Ok. Message sent. Someone will contact you ASAP'
|
||||||
|
);
|
||||||
|
}).catch(error=>{
|
||||||
|
this.response.speak (
|
||||||
|
'Sorry, there was a problem with sending message.'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
this.emit (':responseReady');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
"express": "^4.13.0",
|
"express": "^4.13.0",
|
||||||
"isomorphic-fetch": "^2.2.1",
|
"isomorphic-fetch": "^2.2.1",
|
||||||
"mongodb": "^2.2.33",
|
"mongodb": "^2.2.33",
|
||||||
|
"nodemailer": "^4.4.1",
|
||||||
"request": "^2.83.0"
|
"request": "^2.83.0"
|
||||||
},
|
},
|
||||||
"author": "Matt Kruse <github@mattkruse.com> (http://mattkruse.com/)",
|
"author": "Matt Kruse <github@mattkruse.com> (http://mattkruse.com/)",
|
||||||
|
|||||||
Reference in New Issue
Block a user