Multiple Conversations For Direct Line Client

做~自己de王妃 提交于 2020-06-29 06:30:52

问题


I'm trying to use the Microsoft.Bot.Connector.DirectLine .NET client to connect to my Direct Line Channel. My client application will have many conversations open at once (like 1000+).

What I'm trying to do is efficiently create a single Direct Line client object which can receive messages for all my conversations and NOT have a single client per conversation.

This below code is from: https://docs.microsoft.com/en-us/azure/bot-service/bot-service-channel-directline-extension-net-client?view=azure-bot-service-4.0

The problem is that to create a new conversation I need to create a new client which I think would eventually exhaust use up a lot of sockets. Does anyone know if I can create a single connection and then listen for multiple conversations?

Thanks

static async Task Main(string[] args)
{
    Console.WriteLine("What is your name:");
    var UserName = Console.ReadLine();

    var tokenClient = new DirectLineClient(
            new Uri(endpoint),
            new DirectLineClientCredentials(secret));

    var conversation = await tokenClient.Tokens.GenerateTokenForNewConversationAsync();

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

    await client.StreamingConversations.ConnectAsync(
        conversation.ConversationId,
        ReceiveActivities);

    var startConversation = await client.StreamingConversations.StartConversationAsync();
    var from = new ChannelAccount() { Id = startConversation.ConversationId, Name = UserName };
    var message = Console.ReadLine();

    while (message != "end")
    {
        try
        {
            var response = await client.StreamingConversations.PostActivityAsync(
                startConversation.ConversationId,
                new Activity()
                {
                    Type = "message",
                    Text = message,
                    From = from,
                    ChannelData = new Common.ChannelData() { FromNumber = "+17081234567"}
                });
        }
        catch (OperationException ex)
        {
            Console.WriteLine(
                $"OperationException when calling PostActivityAsync: ({ex.StatusCode})");
        }
        message = Console.ReadLine();
    }

    Console.ReadLine();
}

public static void ReceiveActivities(ActivitySet activitySet)
{
    if (activitySet != null)
    {
        foreach (var a in activitySet.Activities)
        {
            if (a.Type == ActivityTypes.Message && a.From.Id == "MyBotName")
            {
                Console.WriteLine($"<Bot>: {a.Text}");
            }
        }
    }
}

回答1:


I think using the Direct Line streaming extensions would be problematic for your purposes. I'm guessing your custom SMS channel would itself be an app service. Since an app service can (and probably should, in your case) be scaled so that multiple instances are running simultaneously, suppose two SMS messages from the same conversation go to two instances of your channel. In addition to having each instance of your channel using many web sockets to talk to many bots, multiple instances of your channel may use duplicated web sockets to talk to the same bot. There's also the problem of each bot itself needing to support streaming extensions.

Rather than using using Direct Line streaming extensions, you might consider using traditional Direct Line. This would involve receiving activities from the bots by polling a Direct Line endpoint.

Since Direct Line is a channel itself that you'd be using on top of your own channel, you might also consider cutting out Direct Line altogether. That way you wouldn't have two channels between the user and the bot. You could send HTTP requests to each bot's endpoint directly, and the activities the bots would receive would contain the service URL for your channel, allowing your channel to receive messages from the bots.



来源:https://stackoverflow.com/questions/57617863/multiple-conversations-for-direct-line-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!