예제 #1
0
        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <IMessageActivity> result)
        {
            var messageActivity = await result;

            if (messageActivity.Text.Contains("help"))
            {
                await ShowHelpAsync(context);
            }

            var luisResult = await _luisService.QueryAsync(messageActivity.Text, CancellationToken.None);

            await HandleTopScoringIntentAsync(context, luisResult, messageActivity);
        }
        private async Task <RecognizerResult> Recognize(LuisRequest request, CancellationToken ct, bool verbose)
        {
            var luisResult = await _luisService.QueryAsync(request, ct).ConfigureAwait(false);

            var recognizerResult = new RecognizerResult
            {
                Text     = request.Query,
                Intents  = GetIntents(luisResult),
                Entities = GetEntitiesAndMetadata(luisResult.Entities, luisResult.CompositeEntities, verbose)
            };

            return(recognizerResult);
        }
예제 #3
0
        /// <summary>
        /// Query Luis API (Async).
        /// </summary>
        /// <param name="query">Luis Query.</param>
        /// <returns></returns>
        internal static async Task <dynamic> RunQuery(string query)
        {
            // Process message

            LuisService luisService = new LuisService(new LuisModelAttribute(ConfigurationManager.AppSettings["LuisConfiguration:AppId"], ConfigurationManager.AppSettings["LuisConfiguration:AppSecret"]));

            LuisResult luisResult = await luisService.QueryAsync(query, CancellationToken.None);

            IList <EntityRecommendation> luisEntities = luisResult.Entities;
            IList <IntentRecommendation> luisIntents  = luisResult.Intents;

            return(luisResult);
        }
        public async Task <T> GetResultDate(string text)
        {
            Result = default(T);
            //invoke LUIS
            if (string.IsNullOrEmpty(text))
            {
                return(default(T));
            }
            if ((EqualityComparer <T> .Default.Equals(Result, default(T))))
            {
                LuisService luis = new LuisService(new LuisModelAttribute(
                                                       modelID: ConfigurationManager.AppSettings["LuisModel"],
                                                       subscriptionKey: ConfigurationManager.AppSettings["LuisKey"],
                                                       apiVersion: LuisApiVersion.V2
                                                       ));
                bool hasResult = true;
                var  result    = Task.Run(() => luis.QueryAsync(new LuisRequest(text), System.Threading.CancellationToken.None)).Result;
                EntityRecommendation datetime = null;
                if (!result.TryFindEntity("builtin.datetimeV2.datetime", out datetime) &&
                    !result.TryFindEntity("builtin.datetimeV2.date", out datetime) &&
                    !result.TryFindEntity("builtin.datetimeV2.time", out datetime) &&
                    !result.TryFindEntity("builtin.datetimeV2.duration", out datetime))
                {
                    hasResult = false;
                }

                if (!hasResult)
                {
                    Result = default(T);
                }
                else
                {
                    //Result = datetime.ToDateTime();
                    if (Result.GetType() == typeof(DateTimeOffset))
                    {
                        Result = (T)Convert.ChangeType(datetime.ToDateTime(), typeof(DateTimeOffset));
                    }
                    else if (Result.GetType() == typeof(TimeSpan))
                    {
                        Result = (T)Convert.ChangeType(datetime.ToTimeSpan(), typeof(TimeSpan));
                    }
                    else if ((Result.GetType() == typeof(Int64)))
                    {
                        Result = (T)Convert.ChangeType(datetime.ToInt64(), typeof(Int64));
                    }
                }
            }
            return(Result);
        }
예제 #5
0
        public static async Task <LuisResult> GetIntentAndEntitiesFromLUIS(string query)
        {
            LuisService luisService = new LuisService(
                new LuisModelAttribute(
                    ConfigurationManager.AppSettings["LuisAppId"],
                    ConfigurationManager.AppSettings["LuisAPIKey"],
                    LuisApiVersion.V2,
                    ConfigurationManager.AppSettings["LuisAPIHostName"],
                    double.Parse(ConfigurationManager.AppSettings["LuisAPIThreshold"], System.Globalization.CultureInfo.InvariantCulture)
                    )
                );
            LuisResult luisData = await luisService.QueryAsync(query, CancellationToken.None);

            return(luisData);
        }
예제 #6
0
        public async Task <ActionResult> Index(QueryViewModel model)
        {
            if (!model.HasIntent)
            {
                var luisService = new LuisService(new LuisModelAttribute(ConfigurationManager.AppSettings["LUIS_ModelId"], ConfigurationManager.AppSettings["LUIS_SubscriptionKey"]));
                var luisResult  = await luisService.QueryAsync(model.Query, CancellationToken.None);

                var resolver = new LuisActionResolver(typeof(GetTimeInPlaceAction).Assembly);
                var action   = resolver.ResolveActionFromLuisIntent(luisResult);

                // Triggering Contextual Action from scratch is not supported on this Web Sample
                if (action != null && !LuisActionResolver.IsContextualAction(action))
                {
                    model.LuisAction = action;

                    // TODO: this is dangerous. This should be stored somewhere else, not in the client, or at least encrypted
                    model.LuisActionType = action.GetType().AssemblyQualifiedName;
                }
                else
                {
                    // no action recnogized
                    return(this.View(new QueryViewModel()));
                }
            }

            ModelState.Clear();
            var isValid = TryValidateModel(model.LuisAction);

            if (isValid)
            {
                // fulfill
                var actionResult = await model.LuisAction.FulfillAsync();

                if (actionResult == null)
                {
                    actionResult = "Cannot resolve your query";
                }

                return(this.View("ActionFulfill", actionResult));
            }
            else
            {
                // not valid, continue to present form with missing/invalid parameters
                return(this.View(model));
            }
        }
예제 #7
0
        private static async Task RunQuery(string query)
        {
            // Process message
            var luisService = new LuisService(new LuisModelAttribute(ConfigurationManager.AppSettings["LUIS_ModelId"], ConfigurationManager.AppSettings["LUIS_SubscriptionKey"]));
            var luisResult  = await luisService.QueryAsync(query, CancellationToken.None);

            // Try to resolve intent to action
            var intentName     = default(string);
            var intentEntities = default(IList <EntityRecommendation>);
            var intentAction   = new LuisActionResolver(typeof(GetTimeInPlaceAction).Assembly)
                                 .ResolveActionFromLuisIntent(luisResult, out intentName, out intentEntities);

            if (intentAction != null)
            {
                var executionContextChain = new List <ActionExecutionContext> {
                    new ActionExecutionContext(intentName, intentAction)
                };
                while (LuisActionResolver.IsContextualAction(intentAction))
                {
                    var luisActionDefinition = default(LuisActionBindingAttribute);
                    if (!LuisActionResolver.CanStartWithNoContextAction(intentAction, out luisActionDefinition))
                    {
                        Console.WriteLine($"Cannot start contextual action '{luisActionDefinition.FriendlyName}' without a valid context.");

                        return;
                    }

                    intentAction = LuisActionResolver.BuildContextForContextualAction(intentAction, out intentName);
                    if (intentAction != null)
                    {
                        executionContextChain.Insert(0, new ActionExecutionContext(intentName, intentAction));
                    }
                }

                await RunActions(luisService, executionContextChain);
            }
            else
            {
                Console.WriteLine("Could not understand the input.");
            }
        }
예제 #8
0
        /// <summary>
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// </summary>
        ///
        public async Task <Message> Post([FromBody] Message message)
        {
            if (message.Type == "Message")
            {
                String reply = String.Empty;
                if (message.Text.Equals("hi"))
                {
                    return(message.CreateReplyMessage("hello"));
                }
                LUISToSql          lReply          = new LUISToSql();
                LuisModelAttribute shoppingModel   = new LuisModelAttribute("be32716c-0d3f-4df6-bacf-bf809547d67a", "8e313738104945008db930cb54f355a7");
                LuisService        shoppingService = new LuisService(shoppingModel);

                //getting current location co-ordinates
                GoogleLocationService service = new GoogleLocationService();
                MapPoint currentPoint         = service.GetLatLongFromAddress("Hyderabad");

                GeoCoordinate userLocation = new GeoCoordinate(currentPoint.Latitude, currentPoint.Longitude);


                LuisResult LuisResponse = await shoppingService.QueryAsync(message.Text);

                reply = lReply.QueryToData(LuisResponse, userLocation);

                // return our reply to the user
                if (reply == null)
                {
                    return(message.CreateReplyMessage($"sorry i do not understand"));
                }

                return(message.CreateReplyMessage($"Result :\n {reply}"));
            }
            else
            {
                return(HandleSystemMessage(message));
            }
        }
예제 #9
0
        public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable <IMessageActivity> result)
        {
            try
            {
                var message = await result;

                //Robot sends a first message to notify our service it's up and running. Right now we don't do anything with it, just ignore it
                if (message.Text.ToLower() == "app started")
                {
                    context.Wait(this.MessageReceivedAsync);
                    return;
                }


                if (!string.IsNullOrEmpty(message.Text))
                {
                    //We run the user query agianst LUIS
                    LuisService luis       = new LuisService(new LuisModelAttribute(ConfigurationManager.AppSettings[@"LUISAppID"], ConfigurationManager.AppSettings[@"LUISKey"]));
                    var         luisResult = await luis.QueryAsync(message.Text, System.Threading.CancellationToken.None);

                    //For the sake of simplicity we consider 3 scenarios:
                    //"Look" - a user is asking the robot to coment about something it sees
                    //"Query name" a user is asking information about a given topic
                    //default: Everything else we can't understand, we just run against the knowledge graph (potentially we could use Bing Search here as well)
                    switch (luisResult.TopScoringIntent.Intent)
                    {
                    case "Look":
                        await context.Forward(new DrawingDialog(), this.DialogCallBackAsync, message, System.Threading.CancellationToken.None);

                        break;

                    case "Query Name":
                        //The trick here is to simplify the work for the knowledge graph: Let's say the user answers "this is a picture of Isaac Newtson",
                        //In this case we extract only the entity "Name" (see our LUIS model in this repo) and only send that forward to Bing. So Bing doesn't need to
                        //understand the context of the conversation, but just find the relevant information about Isaac newton for us
                        if (luisResult.Entities.Any(e => e.Type == "Name"))
                        {
                            message.Text = (from e in luisResult.Entities
                                            where e.Type == "Name"
                                            select e.Entity).First();
                        }
                        await context.Forward(new KnowledgeGraphDialog(), this.DialogCallBackAsync, message, System.Threading.CancellationToken.None);

                        break;

                    default:
                        await context.Forward(new KnowledgeGraphDialog(), this.DialogCallBackAsync, message, System.Threading.CancellationToken.None);

                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.TraceError(ex.ToString());

                await context.PostAsync($"Sorry, I don't think I understood. Can you try again?");

                context.Wait(this.MessageReceivedAsync);
            }
        }
예제 #10
0
        async Task <List <string> > EnviaMensaje(string query)
        {
            LuisObject luis = await LuisService.QueryAsync(query);

            List <string> mensajes = new List <string>();

            if (luis == null)
            {
                mensajes.Add("Error: LUIS SERVICE NOT FOUND");
                return(mensajes);
            }

            if (luis.intents.Count() == 0)
            {
                mensajes.Add("Error: LUIS SERVICE INTENTS NOT FOUND");
                return(mensajes);
            }

            switch (luis.intents[0]?.intent)
            {
            case "BusquedaNoticias":
                if (luis.entities.Count() == 0)
                {
                    mensajes.Add("Error: LUIS SERVICE ENTITIES NOT FOUND");
                    return(mensajes);
                }

                var entityType        = luis.entities[0].type;
                var searchQueryString = "";

                switch (entityType)
                {
                case "TemaNoticias":
                    searchQueryString = $"searchQuery={luis.entities[0].entity}";
                    break;

                case "CategoriaNoticias":
                    searchQueryString = $"searchQuery={luis.entities[0].entity}";
                    break;

                default:
                    searchQueryString = $"searchQuery={luis.entities[0].entity}";
                    break;
                }

                var lista = await Servicios.BingService.QueryAsync(searchQueryString);

                foreach (var item in lista)
                {
                    mensajes.Add(item.Title);
                }

                return(mensajes);

                break;

            default:
                mensajes.Add("Sorry, I did not understand you. Try again, please :-)");
                return(mensajes);
            }
        }
예제 #11
0
        public async Task MessageReceivedAsync(IDialogContext context, IAwaitable <IMessageActivity> argument)
        {
            var message    = await argument;
            var luisResult = await luis.QueryAsync(message.Text, CancellationToken.None);

            switch (luisResult.TopScoringIntent.Intent)
            {
            case "Turn On":
                await HomeAssistant.AllLightsOn();

                string msg = "I turned on all the lights.";
                await context.PostAsync(MakeMessage(context, msg, msg));

                break;

            case "Turn Off":
                await HomeAssistant.AllLightsOff();

                string msg2 = "I turned off all the lights.";
                await context.PostAsync(MakeMessage(context, msg2, msg2));

                break;

            case "Hi":
                string msg3 = $"Hi, I am { Settings.Instance.BotName}. I can help you to manage your smart home.";
                await context.PostAsync(MakeMessage(context, msg3, msg3));

                break;

            case "Get Entities":
                var entities = await HomeAssistant.GetDomains();

                string responseText = "Get services was successful\n\n";
                foreach (var entity in entities)
                {
                    responseText += $"- {entity.Domain}\n\n";
                    foreach (var feature in entity.Features)
                    {
                        responseText += $" \t- {feature.Name}\n\n";
                    }
                }
                await context.PostAsync(responseText);

                break;

            case "Get State":
                if (luisResult.Entities.Count > 0)
                {
                    string room   = GetFirstEntityForType(luisResult, EntityTypes.Room);
                    string device = GetFirstEntityForType(luisResult, EntityTypes.Device);

                    await context.PostAsync($"You asked to GET STATE of the {device} in {room}.");
                }
                else
                {
                    await context.PostAsync($"Not sure what entity you want to get the state for. Please make sure to specify an entity.");
                }
                break;

            case "Set State":
                if (luisResult.Entities.Count > 1)
                {
                    string device = GetFirstEntityForType(luisResult, EntityTypes.Device);
                    string room   = GetFirstEntityForType(luisResult, EntityTypes.Room);

                    await context.PostAsync($"You asked to SET STATE of the {device} in {room}.");
                }
                else
                {
                    await context.PostAsync($"Not sure what entity you want to set the state for and to what value. Please make sure to specify an entity and value.");
                }
                break;

            case "Help":
                await context.PostAsync(MakeMessage(context, "Here are some ideas", "You could say: 'turn on the lights' or 'what is the outdoor temperature'."));

                break;

            default:
                await context.PostAsync(MakeMessage(context, "I'm sorry, I did not get that.", "I'm sorry, I did not understand that. Ask for help to get help."));

                break;
            }
            context.Wait(MessageReceivedAsync);
        }
 internal static async Task <LuisResult> RunQueryAsync(string query) =>
 await luisService.QueryAsync(query, CancellationToken.None);
예제 #13
0
        async Task <string> EnviaMensaje(string query)
        {
            OpenWeatherService openWeatherService = new OpenWeatherService();
            string             city, time, condition;

            LuisService luisService = new LuisService();
            LuisObject  luis = await luisService.QueryAsync(query);

            if (luis == null)
            {
                return("Error: LUIS SERVICE NOT FOUND");
            }

            if (luis.intents.Count() == 0)
            {
                return("Error: LUIS SERVICE INTENTS NOT FOUND");
            }

            switch (luis.intents[0]?.intent)
            {
            case "Weather":
                if (luis.entities.Count() == 0)
                {
                    return("Error: LUIS SERVICE ENTITIES NOT FOUND");
                }

                city = luis.entities.Where(ent => ent.type == "City").FirstOrDefault()?.entity;
                time = luis.entities.Where(ent => ent.type == "Time").FirstOrDefault()?.entity;

                if (city == null)
                {
                    return("Error: Please specify the city.");
                }

                if (time == null)
                {
                    time = DateTime.Now.ToShortDateString();     //Default time is now..
                }
                DateTime requestedDt = DateTime.Now;
                switch (time)
                {
                case "yesterday": requestedDt.AddDays(-1); break;

                case "tomorrow": requestedDt.AddDays(1); break;

                case "next week": requestedDt.AddDays(7); break;

                case "last week": requestedDt.AddDays(-7); break;
                }

                string replyBase;

                if ((requestedDt - DateTime.Now).Days > 0)
                {
                    var forecast = await openWeatherService.GetForecastData(city, requestedDt);

                    List lastDayWeather = forecast.list.Last();

                    string description = lastDayWeather.weather.FirstOrDefault()?.description;
                    string lowAt       = Math.Round(lastDayWeather.temp.min) + "°";
                    string highAt      = Math.Round(lastDayWeather.temp.max) + "°";
                    string cityName    = forecast.city.name + ", " + forecast.city.country;

                    DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
                    dtDateTime = dtDateTime.AddSeconds(lastDayWeather.dt).ToLocalTime();
                    DateTime date = dtDateTime;

                    replyBase = string.Format(OpenWeatherService.ForecastMessage,
                                              date.ToString("dddd, MMMM, yyyy"), description, cityName, lowAt, highAt);
                }
                else
                {
                    var weather = await openWeatherService.GetWeatherData(city);

                    string description = weather.weather.FirstOrDefault()?.description;
                    string lowAt       = weather.main.temp_min + "";
                    string highAt      = weather.main.temp_min + "";
                    string cityName    = "";
                    cityName = weather.name + ", " + weather.sys.country;

                    replyBase = string.Format(OpenWeatherService.WeatherMessage,
                                              description, cityName, lowAt, highAt);
                }

                return(replyBase);

            case "Condition":
                city      = luis.entities.Where(ent => ent.type == "City").FirstOrDefault()?.entity;
                condition = luis.entities.Where(ent => ent.type == "Condition").FirstOrDefault()?.entity;

                if (city == null)
                {
                    return("Error: Please specify the city.");
                }

                var weatherForecast = await openWeatherService.GetWeatherData(city);

                string descriptionF = weatherForecast.weather.FirstOrDefault()?.description;
                string status       = weatherForecast.weather.FirstOrDefault()?.main;

                string cityNameF = weatherForecast.name + ", " + weatherForecast.sys.country;
                descriptionF = descriptionF.Replace("nice", "clear|sun|bright|fine|partially cloudy").Replace("good", "clear|sun|bright|fine").Replace("bad", "rain|snow|cloud").Replace("cold", "snow|hail|sleet|blizzard").Replace("day", "").Replace("night", "").Replace("morning", "").Replace("afternoon", "");
                string message =
                    (condition.ToLower().StartsWith(status.ToLower()) || descriptionF.Contains(condition))
                        ? string.Format(OpenWeatherService.YesMessage, descriptionF, city)
                        : string.Format(OpenWeatherService.NoMessage, descriptionF, city);

                return(message);

            default:
                return("Sorry, I did not understand you. Try again, please :-)");
            }

            return("---");
        }
예제 #14
0
        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <object> result)
        {
            var activity = await result as Activity;

            // calculate something for us to return
            //int length = (activity.Text ?? string.Empty).Length;

            //await context.PostAsync($"Starting Luis query");

            try
            {
                //LuisService luis = new LuisService(new LuisModelAttribute("86684636-488d-48b3-a4c6-233ef496d3d1", "5f3fca65f0a64d68ab6d4d474b1b0fa6", LuisApiVersion.V2, "westus", true, false, true, true));
                LuisService luis       = new LuisService(new LuisModelAttribute("86684636-488d-48b3-a4c6-233ef496d3d1", "5f3fca65f0a64d68ab6d4d474b1b0fa6"));
                var         luisResult = await luis.QueryAsync(activity.Text, System.Threading.CancellationToken.None);

                //await context.PostAsync($"Luis Result received .. Processing intents");

                switch (luisResult.TopScoringIntent.Intent)
                {
                case "":
                case "None":
                    await context.PostAsync($"Please repeat your query. I did not understand...");

                    break;

                case "Greeting":
                    await context.PostAsync($"Greetings to you too. Anything I can do for you?");

                    break;

                case "HomeAutomation.TurnOn":

                    await handleHomeAutomation(context, luisResult, true);

                    break;

                case "HomeAutomation.TurnOff":

                    await handleHomeAutomation(context, luisResult, false);

                    break;

                case "Music.DecreaseVolume":
                    await context.PostAsync("Decreasing the volume");

                    break;

                case "Music.IncreaseVolume":
                    await context.PostAsync("Increasing the volume");

                    break;

                case "Music.Mute":
                    await context.PostAsync("Muting the volume");

                    break;

                case "Music.Unmute":
                    await context.PostAsync("Unmuting the volume");

                    break;

                case "Music.PlayMusic":
                    await context.PostAsync("Playing Music");

                    break;

                case "Music.Pause":
                    await context.PostAsync("Pausing Music");

                    break;

                case "Music.Repeat":
                    await context.PostAsync("Replaying Music");

                    break;

                case "Music.Stop":
                    await context.PostAsync("Stopping Music");

                    break;

                case "Music.SkipForward":
                    await context.PostAsync("Skipping current music track");

                    break;

                default:
                    await context.PostAsync($"I recognized your intent as {luisResult.TopScoringIntent.Intent}...\nHowever I'm not configured to reply to it");

                    break;
                }
            }
            catch (Exception exc)
            {
                await context.PostAsync($"Error while processing Luis query\n{exc}");
            }

            // return our reply to the user
            //await context.PostAsync($"You sent {activity.Text} which was {length} characters");
            context.Wait(MessageReceivedAsync);
            //await context.Forward(new RootLuisDialog(), MessageReceivedAsync, activity, System.Threading.CancellationToken.None);
        }