Permalink
Please sign in to comment.
Browse files
Updated to support standard Amazon Alexa syntax.
Include launch method.
- Loading branch information...
Showing
with
288 additions
and 21 deletions.
- +72 −0 guessinggame.js
- +37 −0 hello.js
- +98 −11 lib/chatskills.js
- +3 −3 package.json
- +78 −7 readme.md
| @@ -0,0 +1,72 @@ | ||
| +var chatskills = require('./lib/chatskills'); | ||
| +var readlineSync = require('readline-sync'); | ||
| + | ||
| +// Allow this module to be reloaded by hotswap when changed | ||
| +module.change_code = 1; | ||
| +chatskills.verbose = true; | ||
| + | ||
| +// Define an alexa-app | ||
| +var app = chatskills.app('guessinggame'); //new alexa.app('guessinggame'); | ||
| + | ||
| +app.launch(function(req,res) { | ||
| + var number = Math.floor(Math.random()*99)+1; | ||
| + res.session('number',number); | ||
| + res.session('guesses',0); | ||
| + var prompt = "Guess a number between 1 and 100!"; | ||
| + res.say(prompt).reprompt(prompt).shouldEndSession(false); | ||
| +}); | ||
| + | ||
| +app.intent('run', { | ||
| + "slots": {}, | ||
| + "utterances": ["{to|} {run|start|go|launch}"] | ||
| + }, function(req, res) { | ||
| + var number = Math.floor(Math.random()*99)+1; | ||
| + res.session('number',number); | ||
| + res.session('guesses',0); | ||
| + var prompt = "Guess a number between 1 and 100!"; | ||
| + res.say(prompt).reprompt(prompt).shouldEndSession(false); | ||
| + } | ||
| +); | ||
| + | ||
| +app.intent('guess',{ | ||
| + "slots":{"guess":"NUMBER"} | ||
| + ,"utterances":["{1-100|guess}"] | ||
| + }, | ||
| + function(req,res) { | ||
| + var guesses = (+req.session('guesses'))+1; | ||
| + var guess = req.slot('guess'); | ||
| + var number = +req.session('number'); | ||
| + if (!guess) { | ||
| + res.say("Sorry, I didn't hear a number. The number was "+number); | ||
| + } | ||
| + else if (guess==number) { | ||
| + res.say("Congratulations, you guessed the number in " + guesses + (guesses==1?" try":" tries")); | ||
| + } | ||
| + else { | ||
| + if (guess > number) { | ||
| + res.say("Guess lower"); | ||
| + } | ||
| + else if (guess < number) { | ||
| + res.say("Guess higher"); | ||
| + } | ||
| + res.reprompt("Sorry, I didn't hear a number. Try again."); | ||
| + res.session('guesses',guesses); | ||
| + res.shouldEndSession(false); | ||
| + } | ||
| + } | ||
| +); | ||
| +module.exports = app; | ||
| + | ||
| +// Start running our skill. You can also just create a "start" intent and ask, "chatskills, ask guessinggame start". | ||
| +chatskills.launch(app); | ||
| + | ||
| +// Console client. | ||
| +var text = ' '; | ||
| +while (text.length > 0 && text != 'quit') { | ||
| + text = readlineSync.question('> '); | ||
| + | ||
| + // Respond to input. | ||
| + chatskills.respond(text, function(response) { | ||
| + console.log(response); | ||
| + }); | ||
| +} |
| @@ -0,0 +1,37 @@ | ||
| +var chatskills = require('./lib/chatskills'); | ||
| +var readlineSync = require('readline-sync'); | ||
| + | ||
| +// Create a skill. | ||
| +var hello = chatskills.app('hello'); | ||
| + | ||
| +// Launch method to run at startup. | ||
| +hello.launch(function(req,res) { | ||
| + res.say("Ask me to say hi!"); | ||
| + | ||
| + // Keep session open. | ||
| + res.shouldEndSession(false); | ||
| +}); | ||
| + | ||
| +// Create an intent. | ||
| +hello.intent('helloWorld', { | ||
| + 'slots': {}, | ||
| + 'utterances': [ '{to |}{say|speak|tell me} {hi|hello|howdy|hi there|hiya|hi ya|hey|hay|heya}' ] | ||
| + }, | ||
| + function(req, res) { | ||
| + res.say('Hello, World!'); | ||
| + } | ||
| +); | ||
| + | ||
| +// Start running our skill. | ||
| +chatskills.launch(hello); | ||
| + | ||
| +// Console client. | ||
| +var text = ' '; | ||
| +while (text.length > 0 && text != 'quit') { | ||
| + text = readlineSync.question('> '); | ||
| + | ||
| + // Respond to input. | ||
| + chatskills.respond(text, function(response) { | ||
| + console.log(response); | ||
| + }); | ||
| +} |
Oops, something went wrong.
0 comments on commit
9d838c9