Alexa and mysql remote endpoint cannot be called or has invalid input

社会主义新天地 提交于 2019-12-12 21:20:30

问题


I'm trying to connect alexa to mysql. The database is stored in localhost,via xampp. I've got nodejs-mysql connection perfectly, but when i try to incorporate it with alexa, it says

"The remote endpoint could not be called, or the response it returned was invalid." My database file:-

var mysql=require('mysql');
var con=mysql.createConnection({
    host:'localhost',
    user: 'root',
    password:'',
    database:'ctp'
});
function answer(){}

answer.prototype.getdata= function(question,callback)
{
    sql="select answer from jarvis where question="+question;
    con.query(sql,function(err,rows,fields)
    {
        callback(rows);
    })
}
module.exports=answer;

My index.js file:-

'use strict';

var AlexaSkill = require('./AlexaSkill'),
    recipes = require('./recipes'),
    dbcon=require('./dbconfig');
var mys=new dbcon();
var APP_ID = "amzn1.ask.skill.1bfd2f06-1c46-464f-9f06-9195691e0aae"; //OPTIONAL: replace with 'amzn1.echo-sdk-ams.app.[your-unique-value-here]';

var HowTo = function () {
    AlexaSkill.call(this, APP_ID);
};

// Extend AlexaSkill
HowTo.prototype = Object.create(AlexaSkill.prototype);
HowTo.prototype.constructor = HowTo;

HowTo.prototype.eventHandlers.onLaunch = function (launchRequest, session, response) {
    var speechText = "Welcome to My Jarvis, version 1 point oh... How can I help you?";
    // If the user either does not reply to the welcome message or says something that is not
    // understood, they will be prompted again with this text.
    var repromptText = "For instructions on what you can say, please say help me.";
    response.ask(speechText, repromptText);
};
HowTo.prototype.intentHandlers = 
{
    "QuestionIntent": function (intent, session, response) {
        var itemSlot = intent.slots.Item,
            itemName,sp;
        if (itemSlot && itemSlot.value){
            itemName = itemSlot.value.toLowerCase();
        }
             sp=itemName;

             mys.getdata(sp,function(rows)
              {
                  sp=rows;
              });


        var cardTitle = "Details for " + itemName,
            //recipe=recipes[itemName],
            recipe = sp,
            speechOutput,
            repromptOutput;
        if (recipe) {
            //window.open("http://www.w3schools.com");
            speechOutput = {
                speech: recipe,
                type: AlexaSkill.speechOutputType.PLAIN_TEXT
            };
            response.tellWithCard(speechOutput, cardTitle, recipe);
        } else {
            var speech;
                speech = "There are no plans for you today";
            }
            speechOutput = {
                speech: speech,
                type: AlexaSkill.speechOutputType.PLAIN_TEXT
            };
            repromptOutput = {
                speech: "What else can I help with?",
                type: AlexaSkill.speechOutputType.PLAIN_TEXT
            };
            response.ask(speechOutput, repromptOutput);
        },

    "AMAZON.StopIntent": function (intent, session, response) 
    {
        var speechOutput = "Goodbye";
        response.tell(speechOutput);
    },

    "AMAZON.CancelIntent": function (intent, session, response) {
        var speechOutput = "Goodbye";
        response.tell(speechOutput);
    },

    "AMAZON.HelpIntent": function (intent, session, response) {
        var speechText = "You can ask questions such as, what's the plan for today, or, you can say exit... Now, what can I help you with?";
        var repromptText = "You can say things like, what's the plan for today, or you can say exit... Now, what can I help you with?";
        var speechOutput = {
            speech: speechText,
            type: AlexaSkill.speechOutputType.PLAIN_TEXT
        };
        var repromptOutput = {
            speech: repromptText,
            type: AlexaSkill.speechOutputType.PLAIN_TEXT
        };
        response.ask(speechOutput, repromptOutput);
    }
};
//what code do you want to see?
exports.handler = function (event, context) 
{
    var howTo = new HowTo();
    howTo.execute(event, context);
};

i have two sp assignments because i want to see if the function call works or not, well it doesnt and it's displaying only the identified itemName

So seeing previous forums, Some amazon helper told me to put lambda request as a test event in lambda console, I've tried it and got

{
  "errorMessage": "RequestId: 7a56e239-d4cc-11e6-9671-cfba622fbaaf Process exited before completing request"
}

I want to know why alexa does not wait for the callback to execute and query the result back to sp and into the speechOutput , Also, how can context.done()/succeed() help this? I've tried to pass context object to the dbconfig file but it just doesnt work. Can you tell me a solution for this? or atleast provide alexa-mysql documentation if it exists?

来源:https://stackoverflow.com/questions/41521097/alexa-and-mysql-remote-endpoint-cannot-be-called-or-has-invalid-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!