如何在Microsoft Bot框架中获取已部署的bot应用程序的服务URL?

时间:2017-07-12 06:34:22

标签: c# botframework luis direct-line-botframework

enter image description here

我正在使用DirectLine API向僵尸程序发送消息,我需要已发布的Bot的服务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;
        }

1 个答案:

答案 0 :(得分:0)

根据article

  

此处的serviceUrl属性至关重要,需要进行设置   到消息接收器/客户端的端点。

  

为了测试您的机器人,您需要创建自定义UI /消息   下沉以向您的机器人发送和接收消息。这个消息接收器会   有效地充当频道并接受HTTP POST消息   JSON序列化的机器人框架活动。

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