【问题标题】:Test and use chat bot without emulator在没有模拟器的情况下测试和使用聊天机器人
【发布时间】:2017-06-01 06:21:01
【问题描述】:

这似乎是一个非常基本的问题,但感谢任何指导。我可以从哪里开始学习? 我使用了 bot 框架并制作了一些代码,给出了特定国家/地区的城市总数。我集成并使用了 LUIS。一切顺利,但现在进行测试,我使用微软的 bot 模拟器。 实时,我想在我的 asp.net 应用程序上使用这个聊天应用程序。我如何在 Asp.net 中使用这个代码并在没有机器人模拟器的情况下进行测试。

工作代码(以防它可能对某人有所帮助)-

using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using Microsoft.Bot.Connector;
using Newtonsoft.Json;


namespace YahooBot
{
    [BotAuthentication]
    public class MessagesController : ApiController
    {
        /// <summary>
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// </summary>
        public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {
                ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
                string StockRateString;

                LUIS StLUIS = await GetEntityFromLUIS(activity.Text);
                if (StLUIS.intents.Count() > 0)
                           {
                              switch (StLUIS.intents[0].intent)
                                        {
                                                                  case "StockPrice":  
                            StockRateString = await GetStock(StLUIS.entities[0].entity);
                                                                        break;
                                                                    case "StockPrice2":  
                            StockRateString = await GetStock(StLUIS.entities[0].entity);
                                                                        break;
                                                                    case "Getcity":
                            StockRateString = await GetCityDetails(StLUIS.entities[0].entity);
                            break;
                        default:  
                            StockRateString = "Sorry, I am not getting you...";
                                                                       break;
                                                                }
                             }
                else  
                     {
                      StockRateString = "Sorry, I am not getting you...";
                     }

                Activity reply = activity.CreateReply(StockRateString);
                await connector.Conversations.ReplyToActivityAsync(reply);
            }
            else
            {
                HandleSystemMessage(activity);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);
            return response;
        }




        private async Task<string> GetStock(string StockSymbol)
                {  
                   double? dblStockValue = await YahooBot.GetStockRateAsync(StockSymbol);  
                   if(dblStockValue==null)  
                   {  
                        return string.Format("This \"{0}\" is not an valid stock symbol", StockSymbol);  
                   }  
                   else  
                   {  
                       return string.Format("Stock Price of {0} is {1}", StockSymbol, dblStockValue);  
                    }  
                }
        private async Task<string> GetCityDetails(string citychar)
        {
           string  dblcityValue = await YahooBot.GetCityAsync(citychar);
            if (dblcityValue == "")
            {
                return string.Format("This \"{0}\" is not an valid city ", citychar);
            }
            else
            {
                return string.Format("number of cities beginning with  {0} is {1}", citychar, dblcityValue);
            }
        }
        private static async Task<LUIS> GetEntityFromLUIS(string Query)
            {  
               Query = Uri.EscapeDataString(Query);
            LUIS Data = new LUIS();  
               using (HttpClient client = new HttpClient())  
               {  
                    string RequestURI = "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/f2a85791-c9fe-44f8-b23c-2580581dc383?subscription-key=8b4566ea897c4c87960995755aa8881d&verbose=true&timezoneOffset=0&q=" + Query;  
                   HttpResponseMessage msg = await client.GetAsync(RequestURI);  

                   if (msg.IsSuccessStatusCode)  
                    {  
                        var JsonDataResponse = await msg.Content.ReadAsStringAsync();  
                       Data = JsonConvert.DeserializeObject<LUIS>(JsonDataResponse);  
                   }  
               }  
                return Data;  
            }  


private Activity HandleSystemMessage(Activity message)
        {
            if (message.Type == ActivityTypes.DeleteUserData)
            {
                // Implement user deletion here
                // If we handle user deletion, return a real message
            }
            else if (message.Type == ActivityTypes.ConversationUpdate)
            {
                // Handle conversation state changes, like members being added and removed
                // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
                // Not available in all channels
            }
            else if (message.Type == ActivityTypes.ContactRelationUpdate)
            {
                // Handle add/remove from contact lists
                // Activity.From + Activity.Action represent what happened
            }
            else if (message.Type == ActivityTypes.Typing)
            {
                // Handle knowing tha the user is typing
            }
            else if (message.Type == ActivityTypes.Ping)
            {
            }

            return null;
        }
    }
}

【问题讨论】:

    标签: asp.net botframework azure-language-understanding


    【解决方案1】:

    必须遵循几个步骤:

    1. 在 Azure 上发布您的机器人(可以直接从 Visual Studio 完成),请参阅 documentation
    2. 在 dev.botframework.com 上注册您的机器人(请参阅 documentation
    3. 在您的 Asp.net 站点中集成聊天,例如使用 iFrame:请参阅documentation 上的教程。 here 还描述了其他可能性。一个重要的注意事项是您应该在 iframe src 中使用令牌而不是秘密,请参阅之前提供的文档中的详细信息(选项 1 与选项 2)

    简而言之:您需要的一切都在官方文档中,在How-to guides

    【讨论】:

    • 非常感谢。再问一个问题 - 如果我可以使用 azure 并在 Asp.net 中使用,那么 Bot Builder 中的对话框有什么用?
    • 机器人是一个 Web 应用程序:Azure 正在托管您的机器人及其逻辑(对话框、FormFlow 等)。
    • Bot 是一个 Web 应用程序,但据说在任何地方都使用模拟器,所以我的问题是我如何在没有模拟器的情况下进行测试。另外,我没有在我的机器人应用程序中的任何地方使用过对话框。可以吗?对不起,如果它太基本的问题
    • Emulator 相当于一个频道,例如 Skype / Slack / Email / Webchat... 除了这个特定频道可以与您的 localhost 版本的机器人对话。因此,要在没有它的情况下使用“真实”通道进行测试,您必须按照回复部署您的机器人代码。您还将看到,当您注册您的机器人时,您可以使用网站中包含的网络聊天窗口直接对其进行测试。对于您的对话,它仅取决于您需要的逻辑,当您需要实现更复杂的逻辑(不仅仅是 1 个问题/1 个回复,而是一个真实的流程)时,您会发现它们真的很有帮助
    • 稍后我会查看对话框。我试过了,但没有它们我可以做任何事情时无法使用。非常感谢
    猜你喜欢
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多