gpt4 book ai didi

node.js - 如何使用机器人框架中的每条消息创建和发送反向 channel 事件?

转载 作者:搜寻专家 更新时间:2023-10-31 23:40:02 24 4
gpt4 key购买 nike

我正在尝试从 send Hook 中的 botbuilder 中间件访问 session 变量:

bot.use({
botbuilder: function (session, next) {
session.send(); // it doesn't work without this..
session.sendTyping();
console.log('session.userData', session.userData['UI_Changes']);
var reply = createEvent("UI_Changes", session.userData['UI_Changes'], session.message.address);
session.send(reply);
// session.userData['UI_Changes'] = {};
next();
},
send: function (event, next) {
// console.log('session.userData', session.userData['UI_Changes']);
// var reply = createEvent("UI_Changes", session.userData['UI_Changes'], session.message.address);
// session.send(reply);
// session.userData['UI_Changes'] = {};
next();
}
});

但是由于 sessionsend 中不可用,我如何访问 userData

createEvent 只是创建一个 backchannel 事件:

//Creates a backchannel event
const createEvent = (eventName, value, address) => {
var msg = new builder.Message().address(address);
msg.data.type = "event";
msg.data.name = eventName;
msg.data.value = value;
return msg;
}

我在 stackoverflow 上找到了这个答案:

send: function (message, next) {
bot.loadSessionWithoutDispatching(message.address,function (error,session){
console.log(session.userData);
});

next();
}

但是,当我尝试创建一个事件并发送它时,我无法访问该地址

bot.loadSessionWithoutDispatching(event.address, function (error,session){
console.log('session not null?', session !== null ? "yes" : "no");
if(session !== null){
console.log('session.userData', session.userData['UI_Changes'], 'address:', session);
var reply = createEvent("UI_Changes", session.userData['UI_Changes'], event.address); //undefined
session.send(reply);
session.userData['UI_Changes'] = {};
}
});

session.messageevent.address 在回调函数中都是undefined。我该如何解决?

事件有以下内容:

event: { type: 'message',
text: 'You reached the Greeting intent. You said \'hi\'.',
locale: 'en-US',
localTimestamp: '2018-06-21T14:37:12.684Z',
from: { id: 'Steves@4MRN9VFFpAk', name: 'Steves' },
recipient: { id: 'pruthvi', name: 'pruthvi' },
inputHint: 'acceptingInput' }

而在 loadSessionWithoutDispatching 函数之外它有:

event outside { type: 'message',
agent: 'botbuilder',
source: 'directline',
textLocale: 'en-US',
address:
{ id: 'A7nrBS4yINt2607QtKpxOP|0000048',
channelId: 'directline',
user: { id: 'pruthvi', name: 'pruthvi' },
conversation: { id: 'A7nrBS4yINt2607QtKpxOP' },
bot: { id: 'Steves@4MRN9VFFpAk', name: 'Steves' },
serviceUrl: 'https://directline.botframework.com/' },
text: 'You reached the Greeting intent. You said \'hi\'.' }

我使用了 bot.loadSession 而不是 loadSessionWithoutDispatching,它工作正常。

最佳答案

我使用了 bot.loadSession 而不是 loadSessionWithoutDispatching,它工作正常。

关于node.js - 如何使用机器人框架中的每条消息创建和发送反向 channel 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50981545/

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