首页 文章

通过DirectLine向WebChat发送消息时出错

提问于
浏览
0

我在Azure中部署了一个机器人 . 使用最新的> net bot框架,(v3) . 前端使用vanilla WebChat . 我正在尝试从BOT向客户端发送一个事件,以触发对网络聊天可见历史的擦除 . 当我的机器人试图发送事件消息时,我得到的不仅仅是无效的502错误 .

JS在我的前端设置网络聊天和直接线是:

var botConnection = new BotChat.DirectLine({
            secret: {secret removed..becuase secret},
            //token: params['t'],
            //domain: params['domain'],
            webSocket: "true" // defaults to true
        });

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

  //backchannel communication setup
  botConnection.activity$
    .filter(function (activity) {
      return activity.type === 'event' && activity.name === 'clearChatHistory';
    })
    .subscribe(function (activity) {
      console.log('"clearChatHistory" received');
      clearChatHistory();
    });

  function clearChatHistory() {
    $(".wc-message-wrapper").remove();
  }

这里的想法是我的机器人代码将创建一个类型为'activity'的消息,其值为''clearChatHistory' . 这会激活我客户端的代码 .

发送此消息的代码是:

internal static async Task SendClearChatHistoryEvent(Activity activity)
    {
        Trace.TraceInformation($"Utility::SendClearChatHistoryEvent");

        Activity clearMessage = activity.CreateReply();
        clearMessage.Type = "event";
        clearMessage.Value = "clearChatHistory";

        Trace.TraceInformation(JsonConvert.SerializeObject(clearMessage));
        Trace.TraceInformation($"Utility::SendClearChatHistoryEvent::ServiceURL:{activity.ServiceUrl}");
        var connector = new ConnectorClient(new Uri(activity.ServiceUrl));

        await connector.Conversations.SendToConversationAsync(clearMessage);
    }

机器人失败发生在'SendToConversationAsync'调用

我遇到错误最接近的是客户端“https://directline.botframework.com/v3/directline/conversations/5OSJJILizNqGG4H7SaV6fQ/activities 502(Bad Gateway)”

Visual Studio输出窗口显示“Microsoft.Rest.TransientFaultHandling.HttpRequestWithStatusException”和“Microsoft.Bot.Connector.ErrorResponseException异常”

任何关于我可能做错了什么的见解或者在这里发生的其他事情都会非常感激 .

2 回答

  • 1

    您在事件上设置了 value ,但检查了 name . 如果你使用它作为过滤器它应该工作:

    .filter(activity => activity.type === 'event' && activity.value === 'clearChatHistory')

    然而,关于坏网关,我不确定,因为我没有看到你的系统上的代码出现这种情况 .

  • 0

    我弄清楚问题是什么 . 首先,我的代码示例中有一个错误,Mark B指出 . 然而,这不是问题的根源,但它确实帮助我解决了真正的问题 . Bad Gateway问题也不是问题的良好指标 . 我不得不做很多Trace编写和逐步执行代码,最后注意到发生了什么 . 我会试着描述这个问题,这样你们都可以从我的愚蠢错误中吸取教训 .

    了解问题,您需要了解这行代码正在做什么

    Activity clearMessage = activity.CreateReply();
    

    CreateReply()从现有的Activiy创建一个新的Activiy . 当它这样做时,它会交换From和Recipient的值 . 这是因为您想要回复原始邮件来自的人 . 100%完全意义 .

    对我来说,问题是我的原始消息也是通过在前一个对话框中调用CreateReaply()来创建的 . 所以,我基本上创建了一个新的消息发送给自己(或在这种情况下的机器人) . 我需要向chatbot用户发送消息,而不是机器人 .

    当机器人框架试图向自己发送消息时,它会排除并失败 . 为什么它得到了错误的网关错误我不知道,并将继续研究 .

    为了解决这个问题,我改变了我的上游代码

    var newMessage = context.MakeMessage()
    

    而不是CreateReaply() . 这不会交换From和Recipient .

    现在一切都很顺利 . 我的代码现在将强制擦除我的WebChat客户端中的可见聊天历史记录 .

相关问题