简体   繁体   中英

Connect to Postgresql with Alexa Skill

I am trying to develop a simple Alexa Skill that connects to a postgresql database using node.js and returns the results.

I can connect to the database using node.js from my local machine but cannot when uploading to a lambda function on aws.

I am still very new and learning. I have done a good amount of research before posting but no luck.

 'query_one': function () {

        const { Pool, Client } = require('pg')

        const pool = new Pool({
            user: 'username',
            host: 'host information',
            database: 'database name',
            password: 'password',
            port: 5432,
        })

        pool.query('SELECT * FROM responses', (err, res) => {
            console.log(res.rows[1].answer)
            pool.end()
        })

        this.emit(':tellWithCard', 'test', this.t('SKILL_NAME'), 'test');

    }

full code including

'use strict';


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

const APP_ID = 'app id is hidden from here';  // TODO replace with your app ID (OPTIONAL).


const languageStrings = {
    'en': {
        translation: {
            FACTS: [
                'SELECT answer FROM responses WHERE query_id=1',
                'SELECT answer FROM responses WHERE query_id=2',
            ],
            SKILL_NAME: 'Space Facts',
            GET_FACT_MESSAGE: "Here's your fact: ",
            HELP_MESSAGE: 'You can ask me questions about the health of the system, or, you can say exit... What can I help you with?',
            HELP_REPROMPT: 'What can I help you with?',
            STOP_MESSAGE: 'Goodbye!',
            WELCOME: 'Welcome to the health check monitor',
        },
    },
};


const handlers = {

    'LaunchRequest': function () {
        const welcome = this.t('WELCOME');
        this.emit(':tellWithCard', welcome, welcome, welcome);
    },
    'query_one': function () {

        const { Pool, Client } = require('pg')

        const pool = new Pool({
            user: 'username',
            host: 'host information',
            database: 'database name',
            password: 'password',
            port: 5432,
        })

        pool.query('SELECT * FROM responses', (err, res) => {
            console.log(res.rows[1].answer)
            pool.end()
        })

        this.emit(':tellWithCard', 'test', this.t('SKILL_NAME'), 'test');

    },
    'query_two': function () {
        const factArr = this.t('FACTS');
        const randomFact = factArr[1];
        const speechOutput = this.t('GET_FACT_MESSAGE');
        this.emit(':tellWithCard', speechOutput, this.t('SKILL_NAME'), randomFact);
    },
    'AMAZON.HelpIntent': function () {
        const speechOutput = this.t('HELP_MESSAGE');
        const reprompt = this.t('HELP_MESSAGE');
        this.emit(':ask', speechOutput, reprompt);
    },
    'AMAZON.CancelIntent': function () {
        this.emit(':tell', this.t('STOP_MESSAGE'));
    },
    'AMAZON.StopIntent': function () {
        this.emit(':tell', this.t('STOP_MESSAGE'));
    },
};

exports.handler = function (event, context) {
    const 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();
};

Well first off all of your emits will fail, here is the proper format. this.emit(':tellWithCard', speechOutput, cardTitle, cardContent, imageObj); For information in connecting to your database see this post How to make connection to Postgres via Node.js

Enjoy, glad to help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM