Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var buildSpeechletResponse = (outputText, shouldEndSession) => {
  2.     return {
  3.         outputSpeech: {
  4.             type: "PlainText",
  5.             text: outputText
  6.         },
  7.         shouldEndSession: shouldEndSession
  8.     }
  9. }
  10.  
  11. var generateResponse = (speechletResponse, sessionAttributes) => {
  12.     return {
  13.         version: "1.0",
  14.         sessionAttributes: sessionAttributes,
  15.         response: speechletResponse
  16.     }
  17. }
  18.  
  19. function getRandomInt(max) { 'intialising random number generation'
  20.     return Math.floor(Math.random() * Math.floor(max));
  21. }
  22.  
  23. exports.handler = (event, context, callback) => {
  24.     var attributes = event.session.attributes;
  25.     if ( typeof attributes === 'undefined' ) {
  26.         attributes = {};
  27.     }
  28.     switch (event.request.type) {
  29.         case "LaunchRequest":
  30.             attributes.c0 = (getRandomInt(52));
  31.             attributes.c1 = (getRandomInt(52));
  32.             context.succeed(generateResponse(buildSpeechletResponse(`Your first number is ${attributes.c0}. Higher or Lower?`, false), attributes))
  33.             break;
  34.         case "IntentRequest":
  35.             var result;
  36.             switch (event.request.intent.name) {
  37.                 case "higher":
  38.                     if (attributes.c1 < attributes.c0) {
  39.                         result = `Take a drink! It was ${attributes.c1}. `;
  40.                     } else {
  41.                         result = `Correct! It was ${attributes.c1}. `;
  42.                     }
  43.                     attributes.c0 = attributes.c1;
  44.                     attributes.c1 = (getRandomInt(52));
  45.                     context.succeed(generateResponse(buildSpeechletResponse(result+`Higher or Lower?`, false), attributes))
  46.                     break;
  47.                 case "lower":
  48.                     if (attributes.c1 > attributes.c0) {
  49.                         result = `Take a drink! It was ${attributes.c1}. `
  50.                     } else {
  51.                         result = `Correct! It was ${attributes.c1}. `
  52.                     }
  53.                     attributes.c0 = attributes.c1;
  54.                     attributes.c1 = (getRandomInt(52));
  55.                     context.succeed(generateResponse(buildSpeechletResponse(result+`Higher or Lower?`, false), attributes))
  56.                     break;
  57.                 case "lastnumber":
  58.                     context.succeed(generateResponse(buildSpeechletResponse(`The number is ${attributes.c0}. Higher or Lower?`, false), attributes))
  59.                     break;
  60.             }
  61.             break;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement