0
votes

I have a bot deployed in Azure. Uses the latest >net bot framework, (v3). The front-end uses the vanilla WebChat. I am trying to send an event from the BOT TO THE CLIENT, in order to trigger a wipe of the webchat visible history. I'm getting the more than useless 502 error when my bot tries to send the event message.

The JS to setup the web chat and directline on my front end is:

 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();
  }        

The idea here is that my bot code will create a message of type 'activity' with the value = 'clearChatHistory'. This fire the code on my client.

The code for sending this message is:

 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);
    }

The bot fail is happening at the 'SendToConversationAsync' call

The closest thing I get to an error is on the client side "https://directline.botframework.com/v3/directline/conversations/5OSJJILizNqGG4H7SaV6fQ/activities 502 (Bad Gateway)"

The Visual Studio output window displays 'Microsoft.Rest.TransientFaultHandling.HttpRequestWithStatusException' and 'Microsoft.Bot.Connector.ErrorResponseException exceptions

Any insights on what I might be doing wrong or whats happening otherwise here would be greatly appreciated.

2

2 Answers

1
votes

You're setting the value on the event but checking the name. If you use this as the filter it should work:

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

Regarding the bad gateway however, I am not sure, as I was not seeing this occur with your code on my system.

0
votes

I figured out what the problem is. First, there was a bug in my code example which Mark B pointed out. However, that was not the source of the problem but it did help me to get to the real issue. The Bad Gateway problem wasn't really a good indicator of the problem either. I had to do a lot of Trace writing and stepping through code to finally notice what was happening. I'll try to describe the problem so you all can learn from my boneheaded mistake.

The understand the problem, you need to understand what this line of code is doing

 Activity clearMessage = activity.CreateReply();

CreateReply() creates a new Activiy from an existing one. When it does that, it swaps the values of From and Recipient. This is because you want to reply to the person the original message comes from. Make 100% total sense.

The problem, for me, was my original message was ALSO created by calling CreateReaply() in a previous dialog. SO, I basically created a new message to be sent to myself (or the bot in this case). I need the message to to to the chatbot user, not the bot.

When the bot framework attempted to send itself a message it excepted and failed. Why it got the bad gateway error I dont exactly know and will continue to research that.

To fix this, I changed my upstream code to use

var newMessage = context.MakeMessage()

instead of the CreateReaply(). This does not swap From and Recipient.

Everything is now flowing along very nicely. My code will now force a wipe of the visible chat history in my WebChat client.