tbeseda
6/28/2015 - 9:49 PM

Ask Alexa about Secrets in Hearthstone

Ask Alexa about Secrets in Hearthstone

{
  "intents": [
    {
     "intent": "PlayerClassSecrets",
     "slots": [
        {
          "name": "PlayerClass",
          "type": "LITERAL"
        }
      ]
    },
    {
      "intent": "Help",
      "slots": []
    }
  ]
}
PlayerClassSecrets secrets for {mage|PlayerClass}
PlayerClassSecrets secrets for {hunter|PlayerClass}
PlayerClassSecrets secrets for {paladin|PlayerClass}
PlayerClassSecrets {mage|PlayerClass} secrets
PlayerClassSecrets {hunter|PlayerClass} secrets
PlayerClassSecrets {paladin|PlayerClass} secrets

Help help
Help help me
Help what can I ask you
Help get help
Help what commands can I ask
Help what commands can I say
Help what can I do
Help what can I use this for
Help what questions can I ask
Help what can you do
Help what do you do
Help how do I use you
Help how can I use you
Help what can you tell me
var secretNames = {"paladin":["Eye for an Eye","Noble Sacrifice","Redemption","Repentance","Avenge"],"hunter":["Explosive Trap","Freezing Trap","Misdirection","Snake Trap","Snipe"],"mage":["Counterspell","Ice Barrier","Ice Block","Mirror Entity","Spellbender","Vaporize","Duplicate"]};

var Secrets = {
  LaunchRequest: function(event, context) {
    console.log('LAUNCH');

    var sessionAttributes = {};
    var cardTitle = 'Welcome';
    var speechOutput = 'Ask me about secrets played by a Hearthstone class. Say, secrets for mage';
    var repromptText = speechOutput;
    var shouldEndSession = false;

    var speechletResponse = buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession);
    var response = buildResponse(sessionAttributes, speechletResponse);

    context.succeed(response);
  },

  IntentRequest: function(event, context) {
    var intentName = event.request.intent.name;

    console.log('INTENT', intentName);

    if (this.intents[intentName]) {
      this.intents[intentName](event, context);
    } else {
      throw 'Invalid intent!'
    }
  },

  SessionEndedRequest: function(event, context) {
    console.log('END');
    context.succeed();
  },

  intents: {
    PlayerClassSecrets: function(event, context) {
      var intent = event.request.intent;
      var cardTitle = intent.name;
      var playerClassSlot = intent.slots.PlayerClass;
      var repromptText = '';
      var sessionAttributes = {};
      var shouldEndSession = false;
      var speechOutput = '';

      console.log('Class:', playerClassSlot);

      if (playerClassSlot) {
        var playerClass = playerClassSlot.value;
        var playerClassSecretNames = secretNames[playerClass];
        if (playerClassSecretNames) {
          speechOutput = 'I found these ' + playerClass + ' secrets, ' + playerClassSecretNames.join(', ');
          shouldEndSession = true;
        } else {
          speechOutput = 'I did not find any secrets played by the' + playerClass + 'class'
        }
      } else {
        speechOutput = 'I\'m not sure which class you want secrets for';
        repromptText = 'I\'m not sure which class you want secrets for,'
                + 'you can ask again by saying, secrets for hunter';
      }

      var speechletResponse = buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession);
      var response = buildResponse(sessionAttributes, speechletResponse);

      context.succeed(response);
    },

    Help: function(event, context) {
      var sessionAttributes = {};
      var cardTitle = 'Help';
      var speechOutput = 'Ask me about secrets played by a Hearthstone class. Say, secrets for mage, or hunter, or paladin';
      var repromptText = 'Ask me about secrets played by a Hearthstone class. Say, secrets for mage';
      var shouldEndSession = false;

      var speechletResponse = buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession);
      var response = buildResponse(sessionAttributes, speechletResponse);

      context.succeed(response);
    }
  }
}

// --------------- Helpers that build all of the responses -----------------------

function buildSpeechletResponse(title, output, repromptText, shouldEndSession) {
  return {
    outputSpeech: {
      type: 'PlainText',
      text: output
    },
    card: {
      type: 'Simple',
      title: 'SessionSpeechlet - ' + title,
      content: 'SessionSpeechlet - ' + output
    },
    reprompt: {
      outputSpeech: {
        type: 'PlainText',
        text: repromptText
      }
    },
    shouldEndSession: shouldEndSession
  }
}

function buildResponse(sessionAttributes, speechletResponse) {
  return {
    version: '1.0',
    sessionAttributes: sessionAttributes,
    response: speechletResponse
  }
}

exports.handler = function(event, context) {
  try {
    /*
    if (event.session.application.applicationId !== "amzn1.echo-sdk-ams.app.[unique-value-here]") {
         context.fail("Invalid Application ID");
     }
    */
    if (event.session.new) {
      console.log('NEW session. id:', event.session.sessionId);
    }

    Secrets[event.request.type](event, context);
  } catch (e) { context.fail('Exception: ' + e); }
};