21049

How to differentiate Bot to user AND user to Bot messages using middleware in bot builder sdk v4?

<h3>Question</h3>

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 code

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

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. });
<h3>Answer1:</h3>

So the function that you pass to .use represents a piece of middleware that can do pre and post processing of an <em>incoming</em> 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).

<em>Outgoing</em> 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.

This all comes together to look a little something like this:

<pre class="lang-js prettyprint-override">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. `); });

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.

来源:https://stackoverflow.com/questions/54227991/how-to-differentiate-bot-to-user-and-user-to-bot-messages-using-middleware-in-bo

Recommend

  • concatenate multiple rows to one single row in pandas
  • How to set the GOOGLE_APPLICATION_CREDENTIALS from Google Cloud Vision in C#?
  • Spring, Interceptor's excludePathPatterns function is not working properly
  • Which built in AudioUnit can resample audio?
  • JQuery Cycle and JSON with JQuery
  • Using a database API cursor with JDBC and SQLServer to select batch results
  • How to change the Woocommerce search result orderby into price low to high
  • Clearcase: How to control whether SUID programs work in a view or not?
  • Javascript Generated Image Gallery
  • Conflict. The container name “/gitlab-runner” is already in use by container
  • File structure for PHP-based website
  • node.js eventEmitter : Listen for events across files
  • How can I send data in text field using Selenium?
  • Facebook Comments on your website via Graph API instead of Comments plugin?
  • Fetch data from nested nodes in Firebase
  • Appending spaces with str_pad
  • Create a mobile app that just points to URL
  • Static analysis of header inclusion in C++
  • Timeseries Crossvalidation in R: using tsCV() with tslm()-Models
  • Multiple JSliders reacting to each other to always equal 100 percent
  • Re-order an array's items based on several criteria
  • Advertising Identifier for devices lower than iOS 6.0
  • Insert statement not working using execute(array()) of PDO Extension
  • Reader monad - how does it conform to Monad interface?
  • How to include associated objects using gon in Rails/jQuery
  • Terminal run dalvikvm with am.jar
  • Make checkout phone field optional for specific countries in WooCommerce
  • calling IO Operations from thread in ruby c extension will cause ruby to hang
  • Angular 4: Responsive Grid List
  • Jersey serializes character value to ASCII equivalent numeric string
  • How to use FirstOrDefault inside Include
  • PHP Permalinks.. how to change?
  • Write to .csv file with PHP (Commas in Data Error)
  • media foundation H264 decoder not working properly
  • Running R's aov() mixed effects model from Python using rpy2
  • Access to a Matlab gui from the web