title: Alexa ask a question and get response from external API
body: <p>I have set up a simple intent </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>{
    "interactionModel": {
      "languageModel": {
        "invocationName": "viva bank",
        "intents": [
          ...builtin intents...{
            "name": "ask",
            "slots": [{
              "name": "question",
              "type": "AMAZON.SearchQuery"
            }],
            "samples": [
              "when {question}",
              "how to {question}",
              "what {question}"
            ]
          }
        ],
        "types": []
      }
    }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>But when I ask a question it gives me a generic error response like this:</p>
  
  <p>Me: alexa ask viva bank when is the late fee charged</p>
  
  <p>Alexa: Sorry, I don't know that.</p>
  
  <p>Here is my lambda code, but I don't think it is getting that far.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>'use strict';
  
  const Alexa = require('ask-sdk-core');
  var https = require('https');
  var querystring = require('querystring');
  
  
  const APP_ID = 'amzn1.ask.skill.1234';
  
  const AskIntentHandler = {
    canHandle(handlerInput) {
      return !!handlerInput.requestEnvelope.request.intent.slots['question'].value;
    },
    handle(handlerInput) {
      var question = handlerInput.requestEnvelope.request.intent.slots['question'].value;
      console.log('mydata:', question);
      var responseString = '';
      const subscription_key = 'XXXX';
  
      var data = {
        simplequery: question,
        channel: 'Alexa'
      };
      var get_options = {
        headers: {
          'Subscription-Key': subscription_key
        }
      };
  
      https.get('https://fakeapi.com/' + querystring.stringify(data), get_options, (res) =&gt; {
        console.log('statusCode:', res.statusCode);
        console.log('headers:', res.headers);
  
        res.on('data', (d) =&gt; {
          responseString += d;
        });
  
        res.on('end', function(res) {
          var json_hash = JSON.parse(responseString);
          // grab the first answer returned as text and have Alexa read it
          const speechOutput = json_hash['results'][0]['content']['text'];
          console.log('==&gt; Answering: ', speechOutput);
          // speak the output
          return handlerInput.responseBuilder.speak(speechOutput).getResponse();
        });
      }).on('error', (e) =&gt; {
        console.error(e);
        return handlerInput.responseBuilder.speak("I'm sorry I ran into an error").getResponse();
      });
    }
  
  };
  
  exports.handler = (event, context) =&gt; {
    const alexa = Alexa.handler(event, context);
    alexa.APP_ID = APP_ID;
    alexa.registerHandlers(AskIntentHandler);
    alexa.execute();
  };</code></pre>
  </div>
  </div>
  </p>
  
  <p>I'm really just looking to create a very simple pass through, where a question is asked to Alexa, and then I pipe that to an external API and have Alexa read the response.</p>