agent.add() 不工作,虽然 console.log() 是

agent.add() not working, though console.log() is

我正在尝试使用 Telegram、Dialogflow 和 Firebase 实现一个机器人。 我在使用此功能时遇到问题:

function findDoc(agent){
    const userId = request.body.originalDetectIntentRequest.payload.data.from.id.toString();
    const first_name = request.body.originalDetectIntentRequest.payload.data.from.first_name;
    console.log(`Telegram user ID: ${userId}, first_name: ${first_name}`);//THIS SHOWS
    agent.add(`voy a ver si existe el documento`);//THIS SHOWS
    agent.setContext({name: "firstTimer", lifespan:10});
    return db.collection('users').doc(''+userId).get()
      .then((doc) => {
        if (!doc.exists) {
          console.log(`New user created in database `);//THIS SHOWS
          agent.add(`New user created in database`);//THIS DOESN'T SHOW
          var data={
            'id':userId, 
            'name':first_name, 
            'contadorP': 0,
            'doneQuestions': [],
          };
          return db.runTransaction((dataDB)=>{
            dataDB.set(db.collection('users').doc(''+userId), data,{merge:true});
            return Promise.resolve();
          }).catch((err) => {
                console.error(`Error creating file: `+err);
            });
        } else {
          console.log('Found Telegram profile: ', JSON.stringify(doc.data()));//THIS SHOWS
          const data = doc.data();
          agent.add(`User ${data.id} has the name of ${data.nombre}`);//THIS DOESN'T SHOW
        }
      })
      .catch((err) => {
        console.error(err);
      });
  }

我确信该功能可以正常工作,因为 firebase 控制台中的 console.log() 可以正常工作,因为它们显示了它们应该做的事情;而且我也没有收到任何错误...

这是我的 package.json 的依赖项:

"dependencies": {
    "actions-on-google": "^2.5.0",
    "firebase-admin": "^8.2.0",
    "firebase-functions": "^2.0.2",
    "dialogflow": "^0.6.0",
    "dialogflow-fulfillment": "^0.6.1"
  }

这就是我处理意图的方式:

  intentMap.set('Default Welcome Intent', welcome);

这是调用 findDoc() 函数的函数:

function welcome(agent){
    console.log(`Estoy en la funcion de welcome`);
    agent.add(`Welcome`);
    findDoc(agent);
  }

欢迎功能的 console.log() 和 agent.add() 都显示。 从我在网上阅读的内容来看,依赖关系很好,应该 work.So 我不知道还能尝试什么,因为我(我认为)正确地完成了每个建议 found.Please 帮助...

问题在于,尽管 findDoc(agent) 处理异步并且 return 是一个 Promise,但您并未将此 Promise 用作 welcome() 处理程序的一部分。如果您正在执行任何异步操作,dialogflow-fulfillment 库要求您 return 来自处理程序的 Promise。

自从 Promise 完成后,似乎 可以工作,因此对 console.log() 的调用按预期工作。但是由于您没有 return 将此 Promise 发送给调度程序,它只会发回从 agent.add() 到异步操作点的所有内容。

对于您的情况,解决方案很简单。因为 findDoc() 是 return 一个 Promise,所以你需要做的就是同时拥有 welcome() return 这个 promise。这样的事情应该有效:

function welcome(agent){
  console.log(`Estoy en la funcion de welcome`);
  agent.add(`Welcome`);
  return findDoc(agent);
}