microsoft / BotFramework-DirectLine-DotNet

C# client library for Microsoft Bot Framework's Direct Line protocol
MIT License
27 stars 37 forks source link

Add StreamingConversations sample #7

Closed krisrok closed 4 years ago

krisrok commented 4 years ago

The samples use polling and an external library (websocket-sharp) but should also cover the Microsoft.Bot.Streaming.Transport.WebSockets.WebSocketClient the StreamingConversations APIs use.

An example is outlined here: https://docs.microsoft.com/en-us/azure/bot-service/bot-service-channel-directline-extension-net-client?view=azure-bot-service-4.0

EricDahlvang commented 4 years ago

Hi @krisrok

This repository should definitely have a sample demonstrating usage of this library with App Service Extension. One of the reasons we've decided to open source this library is to enable community contributions. Would you like to make a PR for this sample?

krisrok commented 4 years ago

Hey @EricDahlvang, sadly I could not get it to work with our bot using the code on the linked site. But I am using it in Unity3D and have no access to the bot configuration on azure so it's not an ideal testbed.

EricDahlvang commented 4 years ago

Unfortunately, that document mistakenly points to 'nuget' as the source to find Microsoft.Bot.Connector.DirectLine 3.0.3-Preview1 when in fact, it is currently only published here: https://botbuilder.myget.org/feed/experimental/package/nuget/Microsoft.Bot.Connector.DirectLine

We are in process of releasing this to nuget, but will update that doc with the correct link in the interim. Apologies for the confusion.

krisrok commented 4 years ago

I used the source directly. Are the concepts outlined in the linked document not applicable to the version maintained „here“? (Quotation marks because the issue got moved for whatever reason)

krisrok commented 4 years ago

Oh and the doc mentions you have to subscribe to the myget feed.

EricDahlvang commented 4 years ago

You used the source directly? Please elaborate on the issues you encountered.

EricDahlvang commented 4 years ago

I tried the latest codebase from https://github.com/microsoft/BotFramework-DirectLine-DotNet with:

class Program
{
    static void Main(string[] args)
    {
        Task.WaitAll(Run());
    }

    private const string BotHandle = "mydirectlineextension";
    private const string endpoint = "https://mydirectlineextension.azurewebsites.net/.bot/";
    private const string secret = "";

    public static async Task Run()
    {

        var userId = Guid.NewGuid().ToString();
        var userName = "Fred";

        Console.WriteLine("Connecting...");

        var tokenClient = new DirectLineClient(
            new Uri(endpoint),
            new DirectLineClientCredentials(secret));
        var tokenConversation = await tokenClient.Tokens.GenerateTokenForNewConversationAsync().ConfigureAwait(false);

        var client = new DirectLineClient(
            new Uri(endpoint),
            new DirectLineClientCredentials(tokenConversation.Token));

        await client.StreamingConversations.ConnectAsync(
            tokenConversation.ConversationId,
            ReceiveActivities).ConfigureAwait(false);

        var conversation = await client.StreamingConversations.StartConversationAsync().ConfigureAwait(false);

        Console.WriteLine($"Connected to conversation {conversation.ConversationId}");

        Console.Write("> ");
        var message = Console.ReadLine();

        while (message != "end")
        {
            try
            {
                var response = await client.StreamingConversations.PostActivityAsync(conversation.ConversationId,
                    new Activity()
                    {
                        Type = "message",
                        Text = message,
                        From = new ChannelAccount()
                        {
                            Id = userId,
                            Name = userName
                        }
                    }).ConfigureAwait(false);
            }
            catch (OperationException ex)
            {
                Console.WriteLine($"OperationException when calling PostActivityAsync: ({ex.StatusCode})");
            }
            Console.Write("> ");
            message = Console.ReadLine();
        }

        client.StreamingConversations.Disconnect();
    }

    public static void ReceiveActivities(ActivitySet activitySet)
    {
        if (activitySet != null)
        {
            foreach (var a in activitySet.Activities)
            {
                if (a.Type == ActivityTypes.Message && a.From.Id.Contains(BotHandle,StringComparison.InvariantCultureIgnoreCase))
                {
                    Console.WriteLine($"<Bot>: {a.Text}");
                    if (a.Attachments.Any())
                    {
                        foreach (var attachment in a.Attachments)
                        {
                            var stream = attachment.Content as Stream;
                            Task.Run(async () => {
                                int count;
                                int total = 0;
                                byte[] buffer = new byte[4096];
                                do
                                {
                                    count = await stream.ReadAsync(buffer, 0, 4096);
                                    total += count;
                                } while (count > 0);
                                Console.WriteLine($"  Read stream of length: {total}");
                            });
                        }
                    }
                }
            }
        }
    }
}

Using this bot: DLASE_Bot.zip

This works as expected.

This could be moved back to https://github.com/microsoft/BotFramework-DirectLine-DotNet and labeled as a feature request.

arturl commented 4 years ago

Moved as per Eric's comment.

lzc850612 commented 4 years ago

@krisrok was Eric's last comment sufficient for a sample for StreamingConversations? Feel free to close this issue if it was helpful. Thanks.

krisrok commented 4 years ago

unfortunately, i had no time to revisit this part of the project so far, as i have a working implementation without StreamingConversations -- you know how it is sometimes.

anyway, thanks for the replies @EricDahlvang, maybe someone else profits from this in the meantime...