diff --git a/backend/config/constants.js b/backend/config/constants.js index 6674956..c131195 100644 --- a/backend/config/constants.js +++ b/backend/config/constants.js @@ -27,7 +27,7 @@ constants.HTTPResultCodes = { constants.SKILL_ID_LENGTH = 24; constants.voiceResponseStrings = { - QUESTION_NOT_FOUND : 'Sorry, I didnt understan', + QUESTION_NOT_FOUND : 'Sorry, I didnt understand', GENERIC_CONTINUE : 'Would you like to continue' } diff --git a/backend/helpers/email.js b/backend/helpers/email.js new file mode 100644 index 0000000..575b75c --- /dev/null +++ b/backend/helpers/email.js @@ -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); + } + }); + }); + }, +}; diff --git a/backend/models/alexa.js b/backend/models/alexa.js index 4079e98..25c80ac 100644 --- a/backend/models/alexa.js +++ b/backend/models/alexa.js @@ -1,9 +1,11 @@ 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) { @@ -30,6 +32,7 @@ module.exports = { .getSkill (config.SKILL_DB_ID) .then (activeSkill => { handlers = {}; + destinationEmail = activeSkill.contactEmail; //Handler for launch request handlers = { @@ -58,36 +61,42 @@ module.exports = { let intent = this.event.request.intent; console.log ('Dialog state : ' + this.event.request.dialogState); - console.log ('Intent object :'); console.log (intent); + //STARTED, IN_PROGRESS - if (this.event.request.dialogState === 'STARTED') { - //Should this be in constants ? - 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 + 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) { + if ( + !emailHelper.validateEmailFromAlexaResponse ( + intent.slots.Email.value + ) + ) { + //Email is not valid, ask again const slotToElicit = 'Email'; - const speechOutput = 'What is your email'; - const repromptSpeech = speechOutput; - this.emit ( - ':elicitSlot', - slotToElicit, - speechOutput, - repromptSpeech - ); - } else if (!intent.slots.Message.value) { - const slotToElicit = 'Message'; - const speechOutput = 'What is your message'; + const speechOutput = + 'Sorry, that was not valid email. What is your email'; const repromptSpeech = speechOutput; this.emit ( ':elicitSlot', @@ -96,13 +105,37 @@ module.exports = { repromptSpeech ); } else { - //all slots are filled - console.log ('Name : ' + intent.slots.Name.value); - console.log ('Email : ' + intent.slots.Email.value); - console.log ('Message : ' + intents.slots.Message.value); - this.response.speak ('Ok. Someone will contact you ASAP'); - this.emit (':responseReady'); + //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=>{ + 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'); } }; diff --git a/backend/package.json b/backend/package.json index b0c0319..3b48add 100644 --- a/backend/package.json +++ b/backend/package.json @@ -10,6 +10,7 @@ "express": "^4.13.0", "isomorphic-fetch": "^2.2.1", "mongodb": "^2.2.33", + "nodemailer": "^4.4.1", "request": "^2.83.0" }, "author": "Matt Kruse (http://mattkruse.com/)",