Created
September 5, 2017 10:55
-
-
Save muttoni/49e70dc80720bd696f29bf45c3fbb912 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Alexa = require('alexa-sdk'); | |
const moment = require('moment'); | |
const languageStrings = { | |
'en-GB': { | |
'translation': { | |
'WELCOME_MESSAGE' : 'Hello World!', | |
'LAST_HAIRCUT' : 'Your last haircut was %s', | |
'HAIRCUT_SAVED_SUCCESSFULLY' : "Ok, I'll remember this haircut", | |
'UNHANDLED' : "I'm sorry, I didn't catch that. What would you like to do?", | |
'GOODBYE' : "Goodbye", | |
'TODAY' : "Today", | |
'ERROR_SAVING_HAIRCUT' : 'Error saving haircut' | |
} | |
}, | |
'en-US': { | |
'translation': { | |
'WELCOME_MESSAGE' : 'Hello World!', | |
'LAST_HAIRCUT' : 'Your last haircut was %s', | |
'HAIRCUT_SAVED_SUCCESSFULLY' : "Ok, I'll remember this haircut", | |
'UNHANDLED' : "I'm sorry, I didn't catch that. What would you like to do?", | |
'GOODBYE' : "Goodbye", | |
'TODAY' : "Today", | |
'ERROR_SAVING_HAIRCUT' : 'Error saving haircut' | |
} | |
}, | |
'de-DE': { | |
'translation': { | |
'WELCOME_MESSAGE' : 'Hallo Welt!', | |
'CONFIRMATION' : 'Genau' | |
} | |
} | |
}; | |
const handlers = { | |
'LaunchRequest': function() { | |
if(this.attributes && !this.attributes.haircuts ) { | |
this.attributes.haircuts = []; | |
} | |
this.emit(':ask', this.t('WELCOME_MESSAGE')); | |
}, | |
'SaveHaircutIntent' : function() { | |
var date = moment(this.event.request.intent.slots.NEW_HAIRCUT_DATE.value); | |
if(date.isValid()) { | |
// date is ok | |
var dateString = date.toJSON(); | |
this.attributes.last_haircut = dateString; | |
if(!Array.isArray(this.attributes.haircuts)) { | |
this.attributes.haircuts = []; | |
} | |
this.attributes.haircuts.push(dateString); | |
this.emit(':tell', this.t('HAIRCUT_SAVED_SUCCESSFULLY')); | |
} else { | |
//invalid date | |
this.emit(':ask', this.t('UNHANDLED')); | |
} | |
}, | |
'GetLastHaircutIntent' : function() { | |
moment.locale(this.event.request.locale); | |
var last_haircut = this.attributes.last_haircut; | |
var last_haircut_date = moment(last_haircut); | |
if(last_haircut && last_haircut_date.isValid()) { | |
var diff_string = ''; | |
if (last_haircut_date.diff(moment().startOf('day')) === 0) { | |
diff_string = this.t('TODAY'); | |
} else if(last_haircut_date.diff(moment().startOf('day'),'days') >= 2){ | |
diff_string = last_haircut_date.from(moment().startOf('day')); | |
} else { | |
diff_string = last_haircut_date.from(moment()); | |
} | |
this.emit(':tell', this.t('LAST_HAIRCUT', diff_string)); | |
} | |
}, | |
'AMAZON.CancelIntent' : function() { | |
this.emit(':tell', this.t('GOODBYE')); | |
}, | |
'AMAZON.HelpIntent' : function() { | |
this.emit(':tell', this.t('UNHANDLED')); | |
}, | |
'AMAZON.StopIntent' : function() { | |
this.emit(':tell', this.t('GOODBYE')); | |
}, | |
'Unhandled': function() { | |
this.emit(':ask', this.t('UNHANDLED')); | |
}, | |
'SessionEndedRequest': function () { | |
this.emit(':saveState', true); | |
}, | |
}; | |
exports.handler = (event, context, callback) => { | |
const alexa = Alexa.handler(event, context, callback); | |
alexa.appId = 'amzn1.ask.skill.4364040b-4b88-4dd9-ae42-560141632776'; | |
alexa.resources = languageStrings; | |
alexa.dynamoDBTableName = "barber"; | |
alexa.registerHandlers(handlers); | |
alexa.execute(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment