gpt4 book ai didi

postgresql - 将 DialogflowChatbot 与 PostgreSQL 数据库连接

转载 作者:行者123 更新时间:2023-11-29 12:01:59 25 4
gpt4 key购买 nike

我想将 Dialogflow 中的自定义聊天机器人与 PostgreSQL 数据库连接起来。场景是用户向机器人提交他/她的查询,然后机器人将 SQL 查询提交给数据库。我知道应该使用 Webhook/Fulfillment and Integration,但问题是如何

直到现在,我都尝试在 Fulfillment/Inside Editor 中编写代码,方法是将...

const pg = require('pg');
const connectionString = process.env.DATABASE_URL || 'postgres://un:pass@localhost:5432/postgres';
const client = new pg.Client(connectionString);
client.connect();
const query = client.query('CREATE TABLE items(id SERIAL PRIMARY KEY, text VARCHAR(40) not null, complete BOOLEAN)');
query.on('end', () => { client.end(); });

...进入 index.js。此外,在尝试使用 Google Assistant 时,我总是会收到一个没有任何指示性解释的 Webhook 错误:

textPayload: "MalformedResponse: Webhook error (206)"  

我不想将机器人连接到其他中间网站;聊天机器人应该能够自行查询和检查数据库。

有人对我有什么建议吗?谢谢!

日志摘录如下:

7:10:03.013 PM
dialogflowFirebaseFulfillment
Ignoring exception from a finished function
7:10:02.995 PM
dialogflowFirebaseFulfillment
Function execution took 10 ms, finished with status code: 200
7:10:02.986 PM
dialogflowFirebaseFulfillment
Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions
7:10:02.986 PM
dialogflowFirebaseFulfillment
Function execution started
7:09:49.540 PM
dialogflowFirebaseFulfillment
Ignoring exception from a finished function
7:09:48.543 PM
dialogflowFirebaseFulfillment
Function execution took 865 ms, finished with status code: 200
7:09:47.678 PM
dialogflowFirebaseFulfillment
Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions
7:09:47.678 PM
dialogflowFirebaseFulfillment
Function execution started
7:09:12.659 PM
dialogflowFirebaseFulfillment
Warning, estimating Firebase Config based on GCLOUD_PROJECT. Initializing firebase-admin may fail
7:08:41.442 PM
dialogflowFirebaseFulfillment
Warning, estimating Firebase Config based on GCLOUD_PROJECT. Initializing firebase-admin may fail
7:04:51.279 PM
dialogflowFirebaseFulfillment
Ignoring exception from a finished function
7:04:51.238 PM
dialogflowFirebaseFulfillment
Function execution took 896 ms, finished with status code: 200
7:04:50.343 PM
dialogflowFirebaseFulfillment
Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions
7:04:50.343 PM
dialogflowFirebaseFulfillment
Function execution started
7:04:33.195 PM
dialogflowFirebaseFulfillment
Warning, estimating Firebase Config based on GCLOUD_PROJECT. Initializing firebase-admin may fail

最佳答案

我最近写了一篇关于连接 MongoDB 和 Dialogflow here 的文章。

如我之前所述,您将无法连接到运行在 localhost:5432 上的 Postgres 本地实例(也无法连接到 localhost:8080 上的 MySQL)。您必须使用托管 Postgres 服务,例如 ElephantSQL(对于除 Firebase/Firestore 之外的其他数据库。)

现在转向你的答案,首先,请务必认真对待此日志消息:

Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions

要解决此问题,极其重要您使用计费帐户,例如,firebase 的 Blaze 计划来访问外部网络(在您的案例中是作为服务托管的数据库)引用 this

代码

'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');

const pg = require('pg');
const connectionString = process.env.DATABASE_URL || 'postgres://fsdgubow:K4R2HEcfFeYHPY1iLYvwums3oWezZFJy@stampy.db.elephantsql.com:5432/fsdgubow';
const client = new pg.Client(connectionString);
client.connect();
//const query = client.query('CREATE TABLE items(id SERIAL PRIMARY KEY, text VARCHAR(40) not null, complete BOOLEAN)');

});

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

function welcome(agent) {

const text = 'INSERT INTO items(text, complete) VALUES($1, $2) RETURNING *'
const values = ['some item text', '1' ]

// insert demo in your items database
client.query(text, values)
.then(res => {
console.log(res.rows[0])
//see log for output
})
.catch(e => console.error(e.stack))

// sample query returning current time...
return client.query('SELECT NOW() as now')
.then((res) => {
console.log(res.rows[0]);
agent.add(`Welcome to my agent! Time is ${res.rows[0].now}!`);
})
.catch(e => console.error(e.stack))

}

function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}

// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('welcome', welcome);
intentMap.set('Default Fallback Intent', fallback);
// intentMap.set('your intent name here', yourFunctionHandler);
// intentMap.set('your intent name here', googleAssistantHandler);
agent.handleRequest(intentMap);

});

ElephantSQL 控制台

console

Firebase 日志

logs

Google 助理

assistant

我在这里使用 ElephantSQL 进行演示。寻找更多 queries 。确保在它们之前放置 return 以成功执行并避免 "MalformedResponse: Webhook error (206)"!

希望这能回答您的所有疑问。祝你好运!

关于postgresql - 将 DialogflowChatbot 与 PostgreSQL 数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53654308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com