如何使用Bot Builder SDK V4中的中间件区分Bot与用户以及用户与Bot消息? [英] How to differentiate Bot to user AND user to Bot messages using middleware in bot builder sdk v4?

查看:105
本文介绍了如何使用Bot Builder SDK V4中的中间件区分Bot与用户以及用户与Bot消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在sdk V4 Bot中实现了一个中间件,以拦截bot&用户并记录该自定义mongo Db.我正在尝试为使用SDK v4构建的Bot实施类似的概念.看起来我可以使用以下代码来添加中间件,但是不确定如何区分bot和用户之间的消息.用户成为机器人.

I have implemented a middleware in sdk V4 Bot to intercept each message between bot & user and log that custom mongo Db. I am trying to implement similar concept for Bot built using SDK v4. Looks like I can use following piece of code to add a middleware but, not sure how to differentiate message between bot to user & user to bot.

V3机器人代码

bot.use({
    botbuilder: function (session, next) {
        logUserConversation(session)
        next()
    },
    send: function (event, next) {
        logBotsConversation(event)
        next()
    }
})

中间件的V4机器人代码

V4 bot code for Middleware

botAdapter.use(async (turnContext, next) => {
    // How to find which messages/activity object is from user to bot

    await next();
    // How to find which messages/activity object is from bot to user.
});

推荐答案

因此,传递给.use的函数表示可以对传入活动进行预处理的中间件. .您可以通过turnContext.Activity属性从转弯上下文中访问当前"活动.这些活动既可以从用户发送,也可以从通过DirectLine API将其发送到bot的其他系统发送(假设您使用的是Bot Framework Service).

So the function that you pass to .use represents a piece of middleware that can do pre and post processing of an incoming activity. You access the "current" activity from the turn context via the turnContext.Activity property. Those activities can either be sent from the user or from some other system that sends them via the DirectLine API to the bot (assuming you're using the Bot Framework Service).

去向活动(即机器人为响应进入的活动而发送的活动)也可以被中间件拦截,但是中间件需要更多地参与到这些活动的发送中明确地.它通过使用onSendActivities API在转弯上下文中注册处理程序来实现此目的.

Outgoing activities, that is activities that are sent from the bot in response to the incoming activities, can also be intercepted by middleware, but the middleware needs to involve itself in the sending of those activities more explicitly. It does this by registering a handler with the turn context using the onSendActivities API.

这一切看起来像这样:

botAdapter.use(async (turnContext, next) => {
    // pre-processing of the current incoming activity
    console.log(`Processing activity ${turnContext.activity.id} starting... `);

    // hook up a handler to process any outgoing activities sent during this turn
    turnContext.onSendActivities(async (sendContext, activities, nextSend) => {
       // pre-processing of outgoing activities

       await nextSend();       

       // post-processing outgoing activities
    });

    await next();

    // post-processing of the current incoming activity 
    console.log(`Processing activity ${turnContext.activity.id} finishing. `);    

});

要注意的一件事是,传出活动处理程序可以被调用0..*次,因为它们基本上是由下游逻辑调用turnContext.sendActivit[y|ies]触发的.因此,如果在转弯期间发送了多个活动,则将为每个批次调用您的处理程序.

The one thing to note is that outgoing activity handlers can be invoked 0..* times as they are basically triggered by downstream logic calling turnContext.sendActivit[y|ies]. So if, during the turn, there are multiple activities sent, your handler will be invoked for each batch.

这篇关于如何使用Bot Builder SDK V4中的中间件区分Bot与用户以及用户与Bot消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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