首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Alexa Skill连接到Postgresql

使用Alexa Skill连接到Postgresql
EN

Stack Overflow用户
提问于 2017-10-28 08:23:30
回答 1查看 399关注 0票数 0

我正在尝试开发一个简单的Alexa技巧,它使用node.js连接到postgresql数据库并返回结果。

我可以从我的本地机器上使用node.js连接到数据库,但在上传到亚马逊网络服务上的lambda函数时无法连接到数据库。

我还是个新手,还在学习。我在发帖前已经做了大量的研究,但没有运气。

代码语言:javascript
复制
 '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');

    }

完整代码,包括

代码语言:javascript
复制
'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();
};
EN

回答 1

Stack Overflow用户

发布于 2017-10-28 08:40:20

首先,你所有的发射都会失败,下面是正确的格式。this.emit(':tellWithCard',speechOutput,cardTitle,cardContent,imageObj);有关连接到数据库的信息,请参阅此帖子How to make connection to Postgres via Node.js

祝您愉快,很高兴为您效劳!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46985079

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档