dialogflow chatbot如何进入firestore数据库并返回支持相关问题?

问题描述 投票:-1回答:2

我正在为我的“dialogflow”聊天机器人使用“firestore”数据库,我已经为在线杂货店创建了这个数据库。问题是:我希望我的聊天机器人最初向用户提出问题,以便在我的数据库中找到正确的项目标题,然后通过询问3-4支持有关该项目的相关问题返回给用户。问题必须是项目属性(品牌,颜色,尺寸......),并且会因项目而异。所以聊天机器人会向下流下来找到最好的项目。你能帮我找到答案吗?我已经创建了代码,但它们不起作用,我不知道它有什么问题。如果你已经创建了这个并拥有index.js文件,我很感激在这里提出我的建议。

 index.js:

    'use strict';

const functions = require('firebase-functions');
// Import admin SDK
const admin = require('firebase-admin');
const {
  WebhookClient
} = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:*'; // enables lib debugging statements
admin.initializeApp(functions.config().firebase);
// here we get the database in a variable
const db = admin.firestore();
const data = {...};
// Add a new document in collection "dialogflow" with document ID 'agent'
  const dialogflowAgentRef = db.collection('dialogflow').doc('agent').set(data); 
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 doc
    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').where('title', '==', title);

    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/agent/rss/channel/item'); // .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);
});
firebase artificial-intelligence dialogflow chatbot
2个回答
1
投票

您显示的代码存在许多问题,可能会导致使用Firestore数据库进行读取和写入时出现问题。

看起来您正在尝试查找要使用该行写入的现有集合

const dialogflowAgentRef = db.collection('dialogflow').doc('agent').where('title', '==', title);

title没有在任何地方定义,我怀疑它会导致错误。此外,doc()返回DocumentReference,但DocumentReference中没有where()方法。

请记住,您需要使用交替的集合和文档来构建Firestore。因此,您的“firebase”集合可以包含名为“agent”的文档,该文档可能包含子集合。

当你试图阅读时

const dialogflowAgentDoc = db.collection('dialogflow/agent/rss/channel/item');

您正在收集一个集合,但后来尝试将其视为文档。评论表明你试图从这个集合中读取一个特定的文档(这是有道理的),但你是用硬编码的字符串“代理”加载该文档,而不是试图从传递的参数中获取代理来自Dialogflow。

最后 - 读取和写入部分中的路径不匹配。在测试时使用硬编码路径很好,但要确保使用匹配路径并且它们反映了collection / doc / collection / doc / ...路径要求。

因此,在这两种情况下,您可能都有一个类似的引用

const docTitle = agent.parameters.title;
const docRef = db.collection('dialogflow').doc(title);

如果您在Dialogflow中的Intents中定义了“title”参数,那么将使用它来引用您可以读取或写入的文档。


-1
投票
thanks for the answer I already changed my database to real time firebase instead of firestore. still having problem with support relevant questions. I want to go to my real time database to find the item by search using "oederByChild" and "equalTo" methods as I found these in people questions and answer in this website. still cannot find and item title through my database child. here is the codes are written:

'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.database();
// const ref = db.ref('server/saving-data/fireblog');
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));

    // Get the database collection 'dialogflow' and document 'agent' and store
    // the document  {entry: "<value of database entry>"} in the 'agent' document
    function writeToDb(agent) {
        const databaseEntry = agent.parameters.databaseEntry;
        const acc = db.ref('rss/channel/item/4/title'); //**This worked!  */
        acc.set({
            entry: databaseEntry
        });
        return Promise.resolve('write complete')
            .then(_acc => {
                agent.add(`Wrote ${databaseEntry} to the realtime database.`);
                return false;
            }).catch(err => {
                console.log(`Error writing to Firestore: ${err}`);
                agent.add(`Failed to write "${databaseEntry}" to the Firestore database.`);
            });
    }
    // and this is when we want to write to in the same child, keeping the old values:
    //const acc = db.ref('/rss/channel/item/5/color'); //**This worked!  */
    //const result = acc.child(databaseEntry).set({entry: databaseEntry});
    //agent.add(`Wrote ${databaseEntry} to the realtime database.`);
    //console.log(result.key);
    //});

    // to read data
    function readFromDb(agent) {
        const any = agent.parameters.any;
        agent.add(`Thank you...`);
        var rootRef = db.ref();
        var childref = rootRef.child("rss/channel/item");
        return childref.orderByChild("title").equalTo("Icebreaker").once("value").then(function(snapshot){ //has been taken from the bus example: https://stackoverflow.com/questions/51917390/dialogflow-how-do-i-pass-a-parameter-through-in-a-firebase-query
            var colored = snapshot.child("color/__text").val();
            var sized = snapshot.child("size/__text").val();
            agent.add(`Your search result for ` + any + ` Throughout the database is ` + colored +
                ` Color and ` + sized + ` Size`);
            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('IWannaBuy', readFromDb);
    intentMap.set('WriteToFirebase', writeToDb);
    agent.handleRequest(intentMap);
});



    enter code here

[this is how my database is][1]


  [1]: https://i.stack.imgur.com/QdFy5.png
© www.soinside.com 2019 - 2024. All rights reserved.