node.js - 如何使用机器人框架中的每条消息创建和发送反向通道事件?
问题描述
我正在尝试从send
钩子中的 botbuilder 中间件访问会话变量:
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();
}
});
但由于session
在 中不可用send
,我该如何访问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.message
都event.address
在undefined
回调函数中。我怎么可能做一个解决方法?
event
有以下内容:
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
它工作正常。
推荐阅读
- ubuntu - 当 ctrl-alt-F 停止工作时,如何让 x2go 退出全屏模式?
- ubuntu - 无法将域指向 Ubuntu 18.04 服务器
- asp.net-core - Google 登录和 ASP.NET Core Web API 出错
- javascript - 如何为具有未知结构的大型 json 构建地图
- google-sheets - 如何使用数组公式按行计算部分匹配?
- visual-studio-code - 在 Visual Studio Code 中将打开的选项卡栏移到底部
- c++ - 是否可以在 C++ 编译时计算函数长度?
- python - 编写一个函数,将另一个函数应用于python中的数字列表
- mqtt - ESP32 - MQTT 到 AWS IoT 使用 MicroPython
- arrays - Python用for循环排列列表