1
votes

I have an Azure bot service which runs perfectly when tested on webchat. Now I tried to open a websocket and test the bot.

  1. I first send a POST request (with all required headers) to https://directline.botframework.com/v3/directline/conversations

  2. I get a response with

    {

    "conversationId": "7FY18sO6FCT9OVi0pW7WBY",

    "token": "my_token_here",

    "expires_in": 1800,

    "streamUrl": "wss://directline.botframework.com/v3/directline/conversations/7FY18sO6FCT9OVi0pW7WBY/stream?watermark=-&t=token_value_here",

    "referenceGrammarId": "c1c290dd-f896-5857-b328-cdc10298e440"

    }

Now I try to use the streamUrl to send/receive data using web socket. I got an error: Undefined

I tried to test the streamUrl on different online websocket testing tool. I still got the undefined error.

How can I test whether there is a problem in the streamUrl? How can I test the web socket?

1
In the context of the Bot Framework, a WebSocket connection is used to receive messages, but not send them. You can find an example of receiving messages via WebSockets here: github.com/Microsoft/BotBuilder-Samples/blob/master/CSharp/…Eric Dahlvang

1 Answers

1
votes

Firstly, as Eric Dahlvang mentioned, it enables us to receive activities via WebSocket stream, but not send activities.

Besides, I do a test with the following steps and sample, the activities can be received as expected via WebSocket stream, you can refer to it.

Step1: make a request to start a conversation

enter image description here

Step2: start client app (a console application) to wait for receiving acitivities

class Program
{
    private static string botId = "fehanbasicbot";

    static void Main(string[] args)
    {
        var url = Console.ReadLine();
        StartReceivingActivities(url).Wait();
        Console.ReadLine();
    }

    private static async Task StartReceivingActivities(string url)
    {
        var webSocketClient = new WebSocket(url);
        webSocketClient.OnMessage += WebSocketClient_OnMessage;
        webSocketClient.Connect();
    }

    private static void WebSocketClient_OnMessage(object sender, MessageEventArgs e)
    {
        // Occasionally, the Direct Line service sends an empty message as a liveness ping. Ignore these messages.
        if (string.IsNullOrWhiteSpace(e.Data))
        {
            return;
        }

        var activitySet = JsonConvert.DeserializeObject<ActivitySet>(e.Data);
        var activities = from x in activitySet.Activities
                            where x.From.Id == botId
                            select x;

        foreach (Activity activity in activities)
        {
            Console.WriteLine(activity.Text);
        }
    }
}

packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.Bot.Connector.DirectLine" version="3.0.2" targetFramework="net461" />
  <package id="Microsoft.Rest.ClientRuntime" version="2.3.2" targetFramework="net461" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net461" />
  <package id="WebSocketSharp" version="1.0.3-rc11" targetFramework="net461" />
</packages>

Step3: make a request to send an activity to the bot

enter image description here

Step4: check the console app output, I can find the activities are received

enter image description here