【问题标题】:Writing better code for Dialogflow Webhooks为 Dialogflow Webhooks 编写更好的代码
【发布时间】:2019-09-15 12:22:46
【问题描述】:

我目前在我的节点后端调用一个带有对话框流的实现 webhook,在 firestore 数据库上执行 crud 操作。有没有更好、更简洁的方法来编写这些?

我的代码看起来写得很糟糕,但它确实有效。我正在努力编写更清晰、更易读的代码,因此我正在寻找有人给我一些关于如何使用 webhook 编写更好的 API 调用的指导。

//DATABASE API CALLS HERE!// 

  case "FAV_COLOR":
    agent.handleRequest(agent => {
      return new Promise(() => {
        async function writeToDb() {
          // Get parameter from Dialogflow with the string to add to the database doc
          const databaseEntry = agent.parameters.color;
          // Get the database collection 'user' and document 'color' and store
          // the document  {entry: "<value of database entry>"} in the 'color' document
          const dialogflowAgentRef = db.collection("user").doc("color");

          try {
            await db.runTransaction(transaction => {
              transaction.set(dialogflowAgentRef, {
                entry: databaseEntry
              });
              return Promise.resolve("Write complete");
            });
            agent.add(
              `Wrote "${databaseEntry}" to the Firestore database.`
            );
          } catch (e) {
            agent.add(
              `Failed to write "${databaseEntry}" to the Firestore database.`
            );
          }
        }
        writeToDb();
      });
    });
    break;

  default:
    console.log("ITS BROKEN");

它目前在 switch 语句中,因为我想根据操作触发不同的实现。两个 agent.add 语句都不会触发。

另外,如果有人能提供一些关于调试这些的技巧,我将不胜感激。我刚刚部署了这些功能,添加了一个 console.log(JSON.stringify());然后检查 firebase 控制台功能部分是否有错误。看起来非常低效。

感谢您花时间回答:)

千斤顶

【问题讨论】:

  • 我投票结束这个问题,因为它要求同行评审以改进工作代码。它更适合专门为此目的创建的Code Review。该网站是针对涉及无法正常工作的代码的问题。
  • 非常感谢肯,我会把问题移到那里。

标签: node.js firebase google-cloud-firestore dialogflow-es chatbot


【解决方案1】:

你的index.js

const functions = require('firebase-functions');
const { WebhookClient } = require('dialogflow-fulfillment');
const  welcome  = require('./welcome')
const  fallback  = require('./fallback')

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));
    let intentMap = new Map();
    intentMap.set('Default Welcome Intent', welcome);
    intentMap.set('Default Fallback Intent', fallback);
    agent.handleRequest(intentMap);
});

您可以拆分欢迎、回退等文件

=> welcome.js

const welcome = (agent) => {
    agent.add(`Welcome to my agent!`);
}
module.exports = welcome

=> fallback.js

const fallback = (agent) => {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
}
module.exports  =  fallback

您可以使用与Example相同的方法

【讨论】:

  • 这很有趣,您的提示确实简化了一些事情!感谢您的答复。我发现我必须像这样在 intentMap 中传递代理:intentMap.set("favColor", updateDB(agent)) 才能使其正常工作。我现在已经拆分了我的文件,例如 index.js、app.js 和一个包含单独意图文件列表的文件夹。我现在的问题是 db 调用似乎只有 10% 的时间在工作。这绝对是一个单独的问题,也许我会做另一个线程解决。感谢您的时间:)
  • intentMap.set('NAME', updateDB);你可以直接这样分配函数,
猜你喜欢
  • 1970-01-01
  • 2013-01-14
  • 2017-08-19
  • 1970-01-01
  • 2011-01-22
  • 2013-12-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-07
相关资源
最近更新 更多