我正在实现一个聊天机器人,该聊天机器人会根据用户输入提供一些信息。我正在通过node调用python脚本并获取数据,而在尝试将此数据发送回Slack时遇到了此错误。我不确定如何将数据从一个异步函数发送到另一个异步函数,或者如何将结果回调到processTextAndRespond。这是我遇到的错误-
An error occurred ReferenceError: result is not defined
at postMessage (C:\Users\)
at processTextAndRespond
这是代码:
rtm.on('message', (event) => {
// console.log(event);
if(!event.subtype){
processTextAndRespond(event.text,event.user,event);
}else{
console.log('nothing to do !!!');
}
});
const processTextAndRespond = async function(userText,userName,event){
processData(userText);
postMessage(answer);
};
const postMessage = async function(answer,event){
try {
console.log("Working");
processData(result);
const answer = await rtm.sendMessage(result, event.channel);
return answer;
} catch (error) {
console.log('An error occurred', error);
}
};
const processData = async function(userText,result) {
const childProcess = require("child_process").spawn('python', ['./search.py', userText], {stdio: 'inherit'})
childProcess.on('data', function (data) {
process.stdout.on("python script output", data)
result = data.toString()