Last active
December 14, 2016 15:35
-
-
Save busbyjon/ac5367149e0123e101ab62b397da5728 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
var alexa = require('alexa-app'); | |
// Allow this module to be reloaded by hotswap when changed | |
module.change_code = 1; | |
// Define an alexa-app | |
var app = new alexa.app('catcheckup'); | |
app.launch(function(req,res) { | |
// lets see if the user has any pets setup? | |
var cats = new Array(); | |
res.session('cats', cats) | |
if (cats.length < 1) { | |
var prompt = "You don't appear to have a cat setup, would you like to set a cat up now?"; | |
var reprompt = "I didn't understand, would you like to setup a cat now?" | |
res.session('promptData', { yesAction: 'setupCat'}); | |
} else if (cats.length == 1) { | |
var prompt = "Would you like to know your cats health now?"; | |
res.session('promptData', { yesAction: 'catHealth'}); | |
} else { | |
var prompt = "Which cat would you like to know the health of?"; | |
} | |
res.say(prompt).reprompt(reprompt).shouldEndSession(false); | |
}); | |
app.intent('ListPetsIntent', { | |
"utterances":["{ List my cats | what cats do I have configured }"] | |
},function(req,res) { | |
var cats = req.session('cats'); | |
if (cats.length < 1) { | |
res.say('You have no cats setup.').shouldEndSession(false); | |
} else if (cats.length == 1) { | |
res.say('You have one cat, '+cats[0]+ ' setup').shouldEndSession(false); | |
} else { | |
var s = cats.slice(0, cats.length - 1).join(', ') + ", and " + cats.slice(-1); | |
res.say('You have a few cats setup,' + s).shouldEndSession(false); | |
} | |
} | |
); | |
app.intent('NamePetIntent', { | |
"slots":{"NAME":"LITERAL"} | |
,"utterances":["{My cats name is|my pets name} {bob|missy|waffles|NAME}"] | |
},function(req,res) { | |
var promptData = req.session('promptData'); | |
if (!promptData) { | |
console.log('Got a AMAZON.YesIntent but no promptData. Ending session.'); | |
res.shouldEndSession(true).send(); | |
} else if (promptData.yesAction = "setupCat") { | |
res.say('Great, your cats name is '+req.slot('NAME') + ". Would you like to tell us more about him now?"); | |
var cat = req.slot('NAME'); | |
var cats = req.session('cats'); | |
cats.push(cat); | |
req.session('cats', cats); | |
// clear the prompt - and set it for the next action | |
req.session('promptData', { yesAction: 'setupMedication' }); | |
} | |
} | |
); | |
app.intent("LogPetMedication", { | |
"slots":{"NAME":"LITERAL", "MEDICATION":"LITERAL"}, | |
"utterances":["{NAME} has just had his {MEDICATION} {medication|pill|injection|food|drop}"] | |
}, function(req,res) { | |
res.say('Understood, '+req.slot("NAME") + " has just had his " + req.slot("MEDICATION")); | |
} | |
); | |
app.intent('AMAZON.NoIntent', function(req,res){ | |
var promptData = req.session('promptData'); | |
if (!promptData) { | |
console.log('Got a AMAZON.YesIntent but no promptData. Ending session.'); | |
res.shouldEndSession(true).send(); | |
} else if (promptData.yesAction = "setupCat") { | |
res.say("Ok, you can setup a cat later just by asking").shouldEndSession(true); | |
} else if (promptData.yesAction = "catsHealth") { | |
res.say("Ok, I'll be here if you need me"); | |
} else if (promptData.yesAction = "setupMedication") { | |
res.say("Ok, I'll be here if you need me"); | |
} else { | |
console.log('Got a AMAZON.NoIntent but invalid promptData. Ending session.'); | |
res.shouldEndSession(true).send(); | |
} | |
}); | |
app.intent('AMAZON.YesIntent', function(req,res){ | |
var promptData = req.session('promptData'); | |
if (!promptData) { | |
console.log('Got a AMAZON.YesIntent but no promptData. Ending session.'); | |
res.shouldEndSession(true).send(); | |
} else if (promptData.yesAction = "setupCat") { | |
res.say("Ok, what is your pets name?").shouldEndSession(false); | |
} else if (promptData.yesAction = "catsHealth") { | |
res.say("Your current cats health is.."); | |
} else if (promptData.yesAction = "setupMedication") { | |
res.say("Great, what have you just given your cat?"); | |
} else { | |
console.log('Got a AMAZON.YesIntent but invalid promptData. Ending session.'); | |
res.shouldEndSession(true).send(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment