直线客户端的多个对话

Multiple Conversations For Direct Line Client

我正在尝试使用 Microsoft.Bot.Connector.DirectLine .NET 客户端连接到我的直线频道。我的客户端应用程序将同时打开许多对话(例如 1000 多个)。

我想做的是有效地创建一个 Direct Line 客户端对象,它可以接收我所有对话的消息,而不是每个对话只有一个客户端。

以下代码来自: https://docs.microsoft.com/en-us/azure/bot-service/bot-service-channel-directline-extension-net-client?view=azure-bot-service-4.0

问题是要创建一个新的对话,我需要创建一个新的客户端,我认为这最终会耗尽大量的套接字。有谁知道我是否可以创建一个连接然后监听多个对话?

谢谢

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

我认为使用 Direct Line 流媒体扩展对您的目的来说会有问题。我猜您的自定义 SMS 频道本身就是一项应用服务。由于可以(在您的情况下可能应该)扩展应用程序服务,以便多个实例同时 运行,假设来自同一对话的两条 SMS 消息发送到您频道的两个实例。除了让您频道的每个实例使用许多网络套接字与许多机器人对话外,您频道的多个实例可能使用重复的网络套接字与同一个机器人对话。还有每个机器人本身需要支持流扩展的问题。

您可以考虑使用 traditional Direct Line,而不是使用 Direct Line 流式扩展。这将涉及通过轮询 Direct Line 端点来接收来自机器人的活动。

由于 Direct Line 本身就是一个频道,您将在自己的频道之上使用它,因此您也可以考虑完全切断 Direct Line。这样你就不会在用户和机器人之间有两个通道。您可以直接向每个机器人的端点发送 HTTP 请求,机器人将收到的活动将包含您频道的服务 URL,从而允许您的频道接收来自机器人的消息。