[英]Connect to Postgresql with Alexa Skill
我正在尝试开发一个简单的Alexa技能,该技能使用node.js连接到Postgresql数据库并返回结果。
我可以从本地计算机使用node.js连接到数据库,但是在aws上上传到lambda函数时无法连接。
我仍然很新,正在学习。 发布前,我做了很多研究,但没有运气。
'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');
}
完整的代码包括
'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();
};
首先,所有发射都将失败,这是正确的格式。 this.emit(':tellWithCard',speechOutput,cardTitle,cardContent,imageObj); 有关连接到数据库的信息,请参见这篇文章如何通过Node.js与Postgres建立连接
享受,很高兴为您提供帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.