gpt4 book ai didi

javascript - agent.add() 不起作用,但 console.log() 是

转载 作者:行者123 更新时间:2023-12-04 10:12:30 25 4
gpt4 key购买 nike

我正在尝试使用 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() 都显示出来了。
从我在网上阅读的内容来看,依赖关系很好并且应该可以工作。所以我不知道还能尝试什么,因为我(我认为)正确地完成了找到的每个建议。请帮助...

最佳答案

问题是,虽然 findDoc(agent)异步处理事情并返回一个 Promise,您没有将此 Promise 用作 welcome() 的一部分。处理程序。如果您正在执行任何异步操作,dialogflow-fulfillment 库要求您从处理程序返回一个 Promise。

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

在您的情况下,解决方案很简单。自 findDoc()正在返回一个 Promise,您需要做的就是拥有 welcome()也返回这个 promise 。这样的事情应该工作:

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

关于javascript - agent.add() 不起作用,但 console.log() 是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61262516/

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