如何从中间件拦截“发送"钩子时访问"session.conversationData"? [英] How to access 'session.conversationData' while intercepting 'send' hook from middleware?

查看:70
本文介绍了如何从中间件拦截“发送"钩子时访问"session.conversationData"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注官方准则,同时尝试拦截用户/机器人消息.

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 of ISessionMiddleware. The main advantage of using this hook instead of receive is the access we gain to the session.

sendreceive挂钩使用IEventMiddleware.第一个参数是事件本身.要查看事件是否为消息,请检查event.type是否为'message'.

send and receive hooks use IEventMiddleware. The first argument is the event itself. To see whether an event is a message, check to see if event.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆