简体   繁体   中英

How to send custom channel data when using web chat client with bot framework?

I'm trying to pass custom data along with every message that gets sent to my bot. There seems to be a way to achieve this but the example code is incomplete and because I'm a n00b at Javascript I have no clue how to get this right :-)

Here's the script I am using to set everything up:

const botConnection = new BotChat.DirectLine({
  secret: '@directLineSecret',
});

BotChat.App({
  bot: bot,
  botConnection: botConnection,
  user: user,
  resize: 'detect'
}, document.getElementById('bot'));

I am trying to apply what is shown here:

var dl = new BotChat.DirectLine({secret});

BotChat.App({botConnection: {
    … dl,
    postActivity: activity => dl.postActivity({
        … activity,
        channelData: // your data goes here 
    }),
        // other Chat props
});

The code above seems to intercept all calls to postActivity and adds custom channel data. But it also contains these unfortunate "...". How would I have to change my initialisation code so that it will intercept calls and add some keys/values to the channelData object?

Found the answer via the Github page of the web chat client. The "..." is not omitting any code but is Javascript spread syntax

The correct code:

BotChat.App({
  botConnection:
  {
    ...botConnection,
    postActivity: activity => {
      // Add whatever needs to be added.                  
      activity.channelData.MyKey = "MyValue";
      return botConnection.postActivity(activity)
    }
  },
  bot: bot,
  user: user,
  resize: 'detect',
}, document.getElementById('bot'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM