Guest User

Untitled

a guest
Jan 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. 'use strict';
  2. const Alexa = require("alexa-sdk");
  3. const appId = 'amzn1.ask.skill.78e25d07-06c9-488b-afc1-9dabeae510bc'; //'amzn1.echo-sdk-ams.app.your-skill-id';
  4.  
  5. exports.handler = function(event, context, callback) {
  6. const alexa = Alexa.handler(event, context);
  7. alexa.appId = appId;
  8. alexa.registerHandlers(newSessionHandler, startGameHandlers, guessGameHandlers, guessAttemptHandlers);
  9. alexa.execute();
  10. };
  11.  
  12. const states = {
  13. "STARTMODE": "_STARTMODE",
  14. "GUESSMODE": "_GUESSMODE"
  15. }
  16.  
  17. const newSessionHandler = {
  18. 'NewSession': function() {
  19. const welcomeMessage = 'Welcome to high low. Would you like to play ?';
  20. this.response.speak(welcomeMessage).listen('You can say Yes or No');
  21. this.handler.state = states.STARTMODE;
  22. this.emit(':responseReady');
  23. },
  24. 'AMAZON.StopIntent': function(){
  25. this.response.speak('Goodbye!');
  26. this.emit(':responseReady');
  27. },
  28. 'AMAZON.CancelIntent': function(){
  29. this.response.speak('Goodbye!');
  30. this.emit(':responseReady');
  31. }
  32. };
  33.  
  34. const startGameHandlers = Alexa.CreateStateHandler(states.STARTMODE, {
  35.  
  36. 'AMAZON.StopIntent': function(){
  37. this.response.speak('Goodbye from startmode!');
  38. this.emit(':responseReady');
  39. },
  40. 'AMAZON.YesIntent': function() {
  41. this.attributes["guessNumber"] = Math.floor(Math.random() * 100);
  42. this.handler.state = states.GUESSMODE;
  43. this.response.speak('Great. Start by guessing a number').listen("guess a number");
  44. this.emit(':responseReady');
  45. },
  46. 'AMAZON.NoIntent': function(){
  47. this.response.speak('Goodbye!');
  48. this.emit(':responseReady');
  49. }
  50.  
  51.  
  52. });
  53.  
  54. const guessGameHandlers = Alexa.CreateStateHandler(states.GUESSMODE, {
  55. 'NumberGuessIntent': function(){
  56. const guessNum = parseInt(this.event.request.intent.slots.number.value);
  57. const targetNum = this.attributes["guessNumber"];
  58. console.log('user guessed: ' + guessNum);
  59.  
  60. if(guessNum > targetNum){
  61. this.emit('TooHigh', guessNum);
  62. } else if( guessNum < targetNum){
  63. this.emit('TooLow', guessNum);
  64. } else if (guessNum === targetNum){
  65. // With a callback, use the arrow function to preserve the correct 'this' context
  66. this.emit('JustRight', () => {
  67. this.response.speak(guessNum.toString() + 'is correct! Would you like to play a new game?')
  68. .listen('Say yes to start a new game, or no to end the game.');
  69. this.emit(':responseReady');
  70. })
  71. } else {
  72. this.emit('NotANum');
  73. }
  74. },
  75.  
  76. 'AMAZON.StopIntent': function(){
  77. this.response.speak('Goodbye from GuessMode!');
  78. this.emit(':responseReady');
  79. },
  80. 'AMAZON.CancelIntent': function(){
  81. this.response.speak('Goodbye from GuessMode!');
  82. this.emit(':responseReady');
  83. }
  84. });
  85.  
  86. // These handlers are not bound to a state
  87. const guessAttemptHandlers = {
  88. 'TooHigh': function(val) {
  89. this.response.speak(val.toString() + ' is too high.')
  90. .listen('Try saying a smaller number.');
  91. this.emit(':responseReady');
  92. },
  93. 'TooLow': function(val) {
  94. this.response.speak(val.toString() + ' is too low.')
  95. .listen('Try saying a larger number.');
  96. this.emit(':responseReady');
  97. },
  98. 'JustRight': function(callback) {
  99. this.handler.state = states.STARTMODE;
  100. callback();
  101. },
  102. 'NotANum': function() {
  103. this.response.speak('Sorry, I didn\'t get that. Try saying a number.')
  104. .listen('Try saying a number.');
  105. this.emit(':responseReady');
  106. }
  107. };
Add Comment
Please, Sign In to add comment