TransWikia.com

Test and use chat bot without emulator

Stack Overflow Asked by vish1990 on February 20, 2021

It may seems very basic question but any guidance is appreciated .Where can i start to learn?.
I have used bot framework and made some code that gives total number of cities in particular country .I integrated and used LUIS .All went good but now to test,i use bot emulator of microsoft.
In real time ,i want to use this chat application on my asp.net application .How can i use this code in Asp.net and test without bot emulator.

Working Code(in case it may help someone) –

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

One Answer

A few steps must be followed:

  1. Publish your bot on Azure (can be done from Visual Studio directly), see documentation
  2. Register your bot on dev.botframework.com (see documentation)
  3. Integrate the chat in your Asp.net site, for example using iFrame: see the tutorial on the documentation . There are also other possibilities described here. An important note is that you should use a token instead of secret in the iframe src, see details in the documentation provided before (option 1 vs option 2)

In a nutshell: everything you need is on the official documentation, in the How-to guides

Correct answer by Nicolas R on February 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP