测试和使用没有模拟器的聊天机器人

时间:2017-06-01 06:21:01

标签: asp.net botframework luis

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

工作代码(如果有人帮助的话) -

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

1 个答案:

答案 0 :(得分:0)

必须遵循几个步骤:

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