0

在此处输入图像描述

我正在使用 DirectLine API 向机器人发送消息,我需要已发布机器人的服务 URL 来执行负载测试的发布请求,如此处步骤中所述https://blog.botframework.com/2017/06/19/Load-Testing-A-Bot/

这是代码,任何人都可以指出我哪里出错了

private static async Task<Chat> TalkToTheBot(string Message)
        {
            Chat objChat = null;
            // Connect to the DirectLine service
            try
            {
                DirectLineClient client = new DirectLineClient(directLineSecret);

                Conversation conversation = await client.Conversations.StartConversationAsync();

                string watermark = null;

                Activity reply = new Activity
                {
                    From = new ChannelAccount("User1", "User Name"),
                    Text = "Hello",
                    Type = ActivityTypes.Message,
                };


                //await client.Conversations.PostActivityAsync(conversation.ConversationId, reply.CreateReply(text: Message, locale: "en-US"), CancellationToken.None);
                await client.Conversations.PostActivityAsync(conversation.ConversationId,reply , CancellationToken.None);

                // Get the response as a Chat object
                objChat = await ReadBotMessagesAsync(client, conversation.ConversationId, watermark);
            }
            catch (Exception e)
            {

                throw;
            }
            // Return the response as a Chat object
            return objChat;
        }

        private static async Task<Chat> ReadBotMessagesAsync(DirectLineClient client, string conversationId, string watermark)
        {
            // Create an Instance of the Chat object
            Chat objChat = new Chat();

            // We want to keep waiting until a message is received
            bool messageReceived = false;

            while (!messageReceived)
            {
                // Get any messages related to the conversation since the last watermark 
                ActivitySet messages = await client.Conversations.GetActivitiesAsync(conversationId, watermark, CancellationToken.None);

                // Set the watermark to the message received
                watermark = messages?.Watermark;
                // Get all the messages 
                var messagesFromBotText = from message in messages.Activities
                                          where message.From.Id == botId
                                          select message;
                // Loop through each message
                foreach (var message in messagesFromBotText)
                {
                    // We have Text
                    if (message.Text != null)
                    {
                        // Set the text response
                        // to the message text
                        objChat.ChatResponse
                            += " "
                            + message.Text.Replace("\n\n", "<br />");
                    }
                }
                // Mark messageReceived so we can break 
                // out of the loop
                messageReceived = true;
            }
            // Set watermark on the Chat object that will be 
            // returned
            objChat.watermark = watermark;
            // Return a response as a Chat object
            return objChat;
        }
4

1 回答 1

0

根据文章

此处的 serviceUrl 属性很重要,需要将其设置为消息接收器/客户端的端点。

和:

为了测试您的机器人,您需要创建一个自定义 UI/消息接收器来向您的机器人发送和接收消息。此消息接收器将有效地充当通道并接受带有 JSON 序列化机器人框架活动的 HTTP POST 消息。

这基本上意味着您必须构建一个“消息客户端”,并且该客户端的 URL 是您必须在请求的 serviceUrl 中提供的 URL。

于 2017-07-12T20:09:32.417 回答