如何使用C#获取频道数据

How to pick up channel data using C#

我正在通过通道数据从网络聊天客户端向 Bot 发送数据,方式如下:

const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
  if (action.type === 'DIRECT_LINE/POST_ACTIVITY') {
   // The channelData submitted here is very similar to HTTP cookies and vulnerable to forgery attack.
   // Make sure you use signature to protect it and verify the signature on the bot side.
   // To minimize unexpected behaviors, we recommend to treat the "action" object as if it is immutable.
   // We use simple-update-in package to update "action" with partial deep cloning.
   action = window.simpleUpdateIn(
     action,
     ['payload', 'activity', 'channelData', 'myCustomProperty'],
     () => 'Custom value'
   );
 }

 return next(action);
}); 

https://github.com/microsoft/BotFramework-WebChat/blob/master/samples/04.api/b.piggy-back-on-outgoing-activities/index.html

如何使用 C# 在服务器端访问此数据?

您可以在上下文对象下的 channelData 下访问它,下面是完整路径

dialogContext.Context.Activity.ChannelData

我来自巴西,我找到了类似问题的解决方案。 我的最终 java 脚本存储对象:

var cliente = {'xxx': 'xx', 'xx':'xx'} 
      const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
         if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
           dispatch({
             type: 'WEB_CHAT/SEND_EVENT',
             payload: {
               name: 'webchat/join',
               value: cliente
             }
           });
         }
         return next(action);
       });

还有我的 C# 代码:

public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
        {
            await base.OnTurnAsync(turnContext, cancellationToken);
            if (turnContext.Activity.Name == "webchat/join")
            {
                await turnContext.SendActivityAsync(turnContext.Activity.Value?.ToString());
            }
            // Save any state changes that might have occurred during the turn.
            await ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
            await UserState.SaveChangesAsync(turnContext, false, cancellationToken);
        }