gpt4 book ai didi

node.js - 如何通过 Dialogflow 中的内联编辑器将 Dialogflow 连接到 Cloud Firestore?

转载 作者:搜寻专家 更新时间:2023-10-31 23:03:06 26 4
gpt4 key购买 nike

我有一个 Cloud Firestore 数据库,存储了 2017 年英国所有城市的居民人数。

然后我有一个 Dialogflow。每当我将城市名称告诉 Dialogflow 时,我希望它从 Firestore 获取该城市的居民人数并将其返回给 Dialogflow。

具体来说,我想通过内联编辑器实现它。

问题:我需要在下面的代码中添加哪些代码行才能实现这一点?

下面是我在 Dialogflow > Fulfillment > index.js 的内联编辑器中编写的代码:

'use strict';

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

const App = require('actions-on-google').DialogflowApp;

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) {
agent.add(`Hello and welcome!`);
}

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


let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);

agent.handleRequest(intentMap);
});

最佳答案

下面是一些示例代码,展示了如何将 Firebase 的 Firestore 数据库连接到 Firebase 函数上托管的 Dialogflow fulfillment:

'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {WebhookClient} = require('dialogflow-fulfillment');

process.env.DEBUG = 'dialogflow:*'; // enables lib debugging statements
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });

function writeToDb (agent) {
// Get parameter from Dialogflow with the string to add to the database
const databaseEntry = agent.parameters.databaseEntry;

// Get the database collection 'dialogflow' and document 'agent' and store
// the document {entry: "<value of database entry>"} in the 'agent' document
const dialogflowAgentRef = db.collection('dialogflow').doc('agent');
return db.runTransaction(t => {
t.set(dialogflowAgentRef, {entry: databaseEntry});
return Promise.resolve('Write complete');
}).then(doc => {
agent.add(`Wrote "${databaseEntry}" to the Firestore database.`);
}).catch(err => {
console.log(`Error writing to Firestore: ${err}`);
agent.add(`Failed to write "${databaseEntry}" to the Firestore database.`);
});
}

function readFromDb (agent) {
// Get the database collection 'dialogflow' and document 'agent'
const dialogflowAgentDoc = db.collection('dialogflow').doc('agent');

// Get the value of 'entry' in the document and send it to the user
return dialogflowAgentDoc.get()
.then(doc => {
if (!doc.exists) {
agent.add('No data found in the database!');
} else {
agent.add(doc.data().entry);
}
return Promise.resolve('Read complete');
}).catch(() => {
agent.add('Error reading entry from the Firestore database.');
agent.add('Please add a entry to the database first by saying, "Write <your phrase> to the database"');
});
}

// Map from Dialogflow intent names to functions to be run when the intent is matched
let intentMap = new Map();
intentMap.set('ReadFromFirestore', readFromDb);
intentMap.set('WriteToFirestore', writeToDb);
agent.handleRequest(intentMap);
});

这来自位于此处的 Dialogflow 的 Firestore 示例:https://github.com/dialogflow/fulfillment-firestore-nodejs

关于node.js - 如何通过 Dialogflow 中的内联编辑器将 Dialogflow 连接到 Cloud Firestore?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51692357/

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