如何从中间件拦截“发送"钩子时访问"session.conversationData"? [英] How to access 'session.conversationData' while intercepting 'send' hook from middleware?
问题描述
我一直在关注官方准则,同时尝试拦截用户/机器人消息.
I have been following the official guidelines while trying to intercept user/bot messages.
bot.use({
botbuilder (session, next) {
logger.info("MESSAGE RECEIVED:" + session.message.text);
next();
},
send (event, next) {
logger.info("MESSAGE SENT:" + event.text);
next();
}
});
尽管botbuilder
钩子的行为符合预期,但从未调用send
.
While botbuilder
hook behaves as expected, send
is never called.
在我能够解决此问题的那一刻,我的目标是
从send
方法中间件访问session
对象,更精确地访问session.conversationData
存储容器.
At the moment when i will be able to solve this issue, my goal is to have
access to the session
object, more precisely to the session.conversationData
storage container from the send
method middleware.
我做了什么?
我一直遵循记录中间件示例和自述文件指出:
i have been following the Logging middleware example and the readme states:
中间件上的
botbuilder
钩子是ISessionMiddleware
的一个示例.使用此挂钩而不是receive
的主要优点是我们可以访问session
.
The
botbuilder
hook on the middleware is an example ofISessionMiddleware
. The main advantage of using this hook instead ofreceive
is the access we gain to thesession
.
和
send
和receive
挂钩使用IEventMiddleware
.第一个参数是事件本身.要查看事件是否为消息,请检查event.type
是否为'message'
.
send
andreceive
hooks useIEventMiddleware
. The first argument is the event itself. To see whether an event is a message, check to see ifevent.type
is'message'
.
当前,我已经将session.send
包装在一个自定义函数中,以便我可以记录由bot发送的消息,以及另一个函数来记录对话框中用户的响应.虽然此解决方案解决了我的问题,但感觉很糟糕(需要手动更新每个对话框),而且我无法创建可以从模块加载的通用中间件.
Currently i have wrapped the session.send
in a custom function so i can log the messages which are send by the bot, and another function which logs the user response from inside the dialog. While this solution solves my problem it feels bad ( need to manually update each dialog ) and i am not able to create a generic middleware which could be loaded from a module.
我要实现什么目标?
我的中间件的目的是拦截用户/机器人消息,并使用以下模式将它们记录到SQL表中:conversation-id, message-text, timestamp
.
The purpose of my middleware is to intercept user/bot messages and log them to SQL table with the following schema : conversation-id, message-text, timestamp
.
conversation-id
存储在session.conversationData
存储容器中,因此为什么我需要从send
挂钩访问session
对象.
conversation-id
is stored in session.conversationData
storage container, thus why i need to get access to session
object from the send
hook.
使用"botbuilder": "^3.13.1"
推荐答案
您可以通过调用loadSessionWithoutDispatching来在发送中加载会话:
You can load the session within the send by calling loadSessionWithoutDispatching:
send: function (message, next) {
bot.loadSessionWithoutDispatching(message.address,function (error,session){
console.log(session.userData);
});
next();
}
这篇关于如何从中间件拦截“发送"钩子时访问"session.conversationData"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!