簡體   English   中英

使用node.js和AWS Lambda將Alexa技能連接到mysql數據庫

[英]Connecting Alexa skill to mysql database using node.js and aws lambda

我正在嘗試使用AWS Lambda中的node.js將Alexa技能連接到Amazon RDS mySQL數據庫。 我在將連接上載到lambda之前對其進行了測試,但該連接有效,但是當我上載該連接時,出現“在完成請求之前退出進程”或“技能響應出現問題”錯誤。

'use strict';
const Alexa = require('alexa-sdk');

const APP_ID = 'amzn1.ask.skill.11069fc0-53bc-4cd0-8961-dd41e2d812f8';

var testSQL = 'SELECT weight, height from users where pin=1100';
//=========================================================================================================================================
//Database connection settings
//=========================================================================================================================================
var mysql = require('mysql');
var config = require('./config.json');
// Add connection details for dB
var pool  = mysql.createPool({
    host     : config.dbhost,
    user     : config.dbuser,
    password : config.dbpassword,
    database : config.dbname
  });

// var dbHeight, dbWeight, dbMuscle, dbExerciseOne, dbExerciseTwo, dbExerciseThree, dbExerciseFour; 
var dbResult;

function searchDB(quest) {
    pool.getConnection(function(err, connection) {
      // Use the connection
        console.log(quest);
        connection.query(quest, function (error, results, fields) {
            // And done with the connection.
            connection.release();
            // Handle error after the release.
            if (!!error) {
                console.log('error')
            }
            else {
                console.log(results[0]);
                dbResult = results[0];
                return dbResult;
                console.log(dbResult.height);
            }
            process.exit();
        });
    });
};

//searchDB(testSQL);

//=========================================================================================================================================
//TODO: The items below this comment need your attention.
//=========================================================================================================================================



const SKILL_NAME = 'My Application';
const GET_FACT_MESSAGE = "Here's your fact: ";
const HELP_MESSAGE = 'You can say tell me a space fact, or, you can say exit... What can I help you with?';
const HELP_REPROMPT = 'What can I help you with?';
const STOP_MESSAGE = 'Goodbye!';

var name, pinNumber;


//=========================================================================================================================================
//Editing anything below this line might break your skill.
//=========================================================================================================================================

const handlers = {
    'LaunchRequest': function () {
        if(Object.keys(this.attributes).length === 0){
            this.attributes.userInfo = {
                'userName': '',
                'pinNo': 0
                }
            this.emit('GetPinIntent');
        }
        else{

            name = this.attributes.userInfo.userName;
            pinNumber = this.attributes.userInfo.pinNo;
            var sql = "";
            //var result = searchDB(sql);
            //var uWeight = result.weight;
            //var uHeight = result.height;
            var speechOutput = 'Welcome ' + name + 'Please select an option: Check My BMI, Create Exercise Plan, Create Meal Plan, Update Height and Weight, Update workout status?';
            this.emit(':ask', speechOutput);
        }
    },

    'GetPinIntent': function (){
      this.emit(':ask','Welcome to my Application, as this is your first time please say your name followed by your pin. For example, my name is Jason and my pin is zero one zero one');
      //this.emit(':responseReady');
   },

    'RememberNameID': function (){
        var filledSlots = delegateSlotCollection.call(this);
        this.attributes.userInfo.userName = this.event.request.intent.slots.name.value;
        this.attributes.userInfo.pinNo = this.event.request.intent.slots.pin.value;

        var speechOutput = 'Welcome ' + this.attributes.userInfo.userName + ' we have stored your Pin Number and we will call you by name next time. Please select an option: BMI or exercise';

        this.response.speak(speechOutput);
        this.emit(':responseReady');

    },

    'CheckBMI': function(){

        var sql = 'SELECT height, weight FROM users WHERE pin=' + this.attributes.userInfo.pinNo;
        var heightWeight = searchDB(sql);
        dbHeight = parseInt(heightWeight.height);
        dbWeight = parseInt(heightWeight.weight);
        var speechOutput = bmiCalculator(dbHeight, dbWeight);
        this.emit(':ask', speechOutput);  

    },

    'AMAZON.HelpIntent': function () {
        const speechOutput = HELP_MESSAGE;
        const reprompt = HELP_REPROMPT;

        this.response.speak(speechOutput).listen(reprompt);
        this.emit(':responseReady');
    },
    'AMAZON.CancelIntent': function () {
        this.response.speak(STOP_MESSAGE);
        this.emit(':responseReady');
    },
    'AMAZON.StopIntent': function () {
        this.response.speak(STOP_MESSAGE);
        this.emit(':responseReady');
    },
    'SessionEndedRequest': function() {
        console.log('session ended!');
        this.emit(':saveState', true);
    }
};

exports.handler = function (event, context, callback) {
    var alexa = Alexa.handler(event, context, callback);
    alexa.APP_ID = APP_ID;
    alexa.dynamoDBTableName = 'fitnessDB';
    alexa.registerHandlers(handlers);
    alexa.execute();


};

function delegateSlotCollection(){
  console.log("in delegateSlotCollection");
  console.log("current dialogState: "+this.event.request.dialogState);
    if (this.event.request.dialogState === "STARTED") {
      console.log("in Beginning");
      var updatedIntent=this.event.request.intent;
      //optionally pre-fill slots: update the intent object with slot values for which
      //you have defaults, then return Dialog.Delegate with this updated intent
      // in the updatedIntent property
      this.emit(":delegate", updatedIntent);
    } else if (this.event.request.dialogState !== "COMPLETED") {
      console.log("in not completed");
      // return a Dialog.Delegate directive with no updatedIntent property.
      this.emit(":delegate");
    } else {
      console.log("in completed");
      console.log("returning: "+ JSON.stringify(this.event.request.intent));
      // Dialog is now complete and all required slots should be filled,
      // so call your normal intent handler.
      return this.event.request.intent;
    }
};
function bmiCalculator (userHeight, userWeight ) {


        var speechOutput = " "; 

        var h = userHeight/100;

        var calcBMI = 0;
        calcBMI = userWeight / (h*h);
        calcBMI = calcBMI.toFixed(2);


        if (calcBMI < 18.5) {
            speechOutput += "Based on your weight of " +weight+ " kilograms and your height of " + height + " metres, your BMI is " +calcBMI+ ". Meaning you are currently underweight.";
            speechOutput += " I would advise you to increase your calorie intake, whilst remaining active.";
            return speechOutput;

        }
        else if (calcBMI >=18.5 && calcBMI < 25){

            speechOutput += "Based on your weight of " +weight+ " kilograms and your height of" + height + " metres, your BMI is " +calcBMI+ ". Meaning you are currently at a normal weight.";
            speechOutput += " I would advise you to stay as you are but ensure you keep a healthy diet and lifestyle to avoid falling above or below this.";
            this.response.speak(speechOutput);
            return speechOutput;
        }
        else if (calcBMI >=25 && calcBMI < 29.9){

            speechOutput += "Based on your weight of " +weight+ " kilograms and your height of" + height + " metres, your BMI is " +calcBMI+ ". Meaning you are currently overweight.";
            speechOutput += " I would advise you to exercise more to fall below this range. A healthy BMI is ranged between 18.5 and 24.9";
            this.response.speak(speechOutput);
            return speechOutput;
        }
        else{

            speechOutput += "Based on your weight of " +weight+ " kilograms and your height of" + height + " metres, your BMI is " +calcBMI+ ". Meaning you are currently obese.";
            speechOutput += " I would advise you to reduce your calorie intake, eat more healthy and exercise more. A healthy BMI is ranged between 18.5 and 24.9";
            return speechOutput;

        }


 };

該代碼概述了我的數據庫連接。 我創建連接查詢是一個功能,因為我需要根據上下文對數據庫進行各種查詢。 有沒有辦法在exports.handler函數內創建一個僅在需要時才調用查詢的函數? 或是否有其他有關以這種方式連接數據庫的解決方案。

您遇到了多個問題,沒有使用Promise或等待,您的呼叫正在異步運行,並且您將永遠不會立即從RDS獲得lambda呼叫的答案。 您需要創建一個在繼續其邏輯之前將等待答案的函數。 您將遇到的另一個問題是MySQL RDS實例,如果它持續運行,可能會出現冷啟動問題。 最后一點是,在AWS Lambda控制台中,請確保在計算和時間上分配足夠的資源以運行此功能,默認為128 mb的內存,並且可以調整運行該功能的時間以提高性能

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM