question

katomatey avatar image
katomatey asked ·

Connection between the Alexa and the API of TMDB, doesnt work. Any ideas?

"use strict";
var Alexa = require('alexa-sdk');
var JefNode = require('json-easy-filter').JefNode;
var http = require("https");
var APP_ID = undefined;  // TODO replace with your app ID (OPTIONAL).
var speechOutput = '';
var reprompt="";
var welcomeOutput = "Welcome to Film Oracle. Explore the best movies. Shall I search for genre,actor or release date?";
var welcomeReprompt = "Shall I search for genre, actor or release date?";
 // 2. Skill Code =======================================================================================================
var handlers = {
    'LaunchRequest': function () {
          this.emit(':ask', welcomeOutput, welcomeReprompt);
    },
 'AMAZON.HelpIntent': function () {
        speechOutput = '';
        reprompt = '';
        this.emit(':ask', speechOutput, reprompt);
    },
    'AMAZON.CancelIntent': function () {
        speechOutput = '';
        this.emit(':ask', speechOutput);
    },
    'AMAZON.StopIntent': function () {
        speechOutput = '';
        this.emit(':ask', speechOutput);
    },
    'SessionEndedRequest': function () {
        speechOutput = 'Good Bye';
        this.emit(':tell', speechOutput);
    },
   'GetFilmSuggestionIntent': function(){
       var input=this.event.request.intent.slots.choice.value;
       console.log(input);
       if (input === "actor"){
           speechOutput="Which actor should I look for?";
           this.emit(':ask', speechOutput);
       }
       if (input === "genre"){
           speechOutput="Which genre should I look for?";
           this.emit(':ask', speechOutput);
       }
       if (input === "release date"){
           speechOutput="Which release date should I look for?";
           this.emit(':ask', speechOutput);
       }
   },
    'GetActorIntent':function(){
        
 var actor=this.event.request.intent.slots.actor.value;
 console.log(actor);
 

     //here is the problem: either the request wasn't able to be sent or the response wasn't able to be read.


var actorChoice = 'will smith';
var options = {
  "method": "GET",
  "hostname": "api.themoviedb.org",
  "port": null,
  "path": "/3/search/person?include_adult=false&page=1&query="+encodeURI(actorChoice)+ "&language=en-US&api_key=3fa3fc2cf5a5c60b927e04849223fe18",
  "headers": {}
};
var req = http.request(options, function (res) {
  var chunks = [];
  res .on("data", function (chunk) {
    chunks.push(chunk);
  });
  res.on("end", function () {
    var body = Buffer.concat(chunks);
    var result = (body.toString());
    var json = JSON.stringify(eval("(" + result + ")"));
     var finalResult = JSON.parse(json);
     var res = new JefNode(finalResult).filter(function(node) {
    if (node.has('overview' && 'title')) {
     return  'Movie: ' + node.value.title ;
    }
   });
   console.log(res [2]);
    this.emit(':tell', res[2]);
  });
});
req.write("{}");
  
    
},
    'Unhandled': function () {
    speechOutput = "The skill didn't quite understand what you wanted.  Do you want to try something else?";
    this.emit(':ask', speechOutput, speechOutput);
}
};
exports.handler = (event, context) => {
var alexa = Alexa.handler(event, context);
alexa.APP_ID = APP_ID;
// To enable string internationalization (i18n) features, set a resources object.
//alexa.resources = languageStrings;
alexa.registerHandlers(handlers);
alexa.execute();
};

alexa skills kitapijavanodejs
1 comment
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hi there

a quick question can we see some error logs from cloudwatch or anything that might lead us into the good way? Because it could be the request takes to long and the lambda function just fails (set the function to 20sec and see if the problem persists). But it could also be a malformed request but there i can't help you with as of yet.

Does Alexa speak back to you or does she just stop?

0 Likes 0 ·
MuseumDev avatar image
MuseumDev answered ·

Simple try is to change it to https.request, otherwise see this:

https://stackoverflow.com/questions/28449363/why-is-this-http-request-not-working-on-aws-lambda

1 comment
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Ignore. I see you are using https.

0 Likes 0 ·
Jenn@amazon avatar image
Jenn@amazon answered ·

What's the error message you're seeing? Do you know where it's failing? Are you getting a Response back from TMDB web service?

10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.