1
votes

I want to send extra custom data from webchat to microsoft bot v4. Let's say by send message event I want to append this data:

"mydata": "123"

to the data being posted and retrieve it in the bot (preferably in the turnContext) and do some logic based on it inside the bot logic. What is the best way to handle this. Is there any built-in functionality or I should use custom ways like middlewares? I'm using directline. Thanks in advance.

2

2 Answers

2
votes

It is possible to add custom channel data to outgoing activities in Web Chat v4. You can create a custom store middleware to modify activities sent by the user. Channel data is a channel-specific property bag that can be used to send non-standard in-band data. Refer to the Backchannel Piggyback on Outgoing Activities Web Chat Sample for more details.

For example,

Here, I will be making use of the package simple-update-in to update the immutable action objects. Then add the minified js file from unpkg.com to the < head> of the html:

…
<head>
  <title>Web Chat: Inject data on post activity</title>
  <script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
+ <script src="https://unpkg.com/simple-update-in/dist/simple-update-in.production.min.js"></script>
…

This helps to make use of the middleware to customise DIRECT_LINE/POST_ACTIVITY by updating the action with deep cloning.

…
const store = window.WebChat.createStore(
   {},
   ({ dispatch }) => next => action => {
   if (action.type === 'DIRECT_LINE/POST_ACTIVITY') {
+     action = window.simpleUpdateIn(action, ['payload', 'activity', 'channelData', 'email'], () => '[email protected]');
   }

   return next(action);
   }
);
…

Now all the DIRECT_LINE/POST_ACTIVITY sent on this bot will have an email attached to the channel data. Similarly, you can customize the data you want to send.

Furthermore, as linked in the above answer, you can implement channel-specific functionality by passing the metadata to the channel in the activity object's channel data property.

Hope this helps.

0
votes

Great question! I think what you're after is the ChannelData field on the Activity sent from WebChat through the channel to your bot.

Some channels have specific enhanced abilities that a bot can be instructed to invoke on the channel by including the appropriate triggers in ChannelData (see the examples in the link for more info on those), but this is also a safe place to send any special data you want to pass to your bot. In general this field exists to pass data from the client to the bot in order to instruct the bot to undertake some custom behavior when processing the message.

In your case it looks like you just need to have the client add some formatted JSON to the ChannelData of out going Activities and add logic to your bot to extract and process it.