예제 #1
0
        /// <summary>
        /// Generates a response for an Update with location.
        /// </summary>
        /// <param name="conversation"></param>
        /// <param name="update"></param>
        /// <param name="intellectInstance"></param>
        /// <returns></returns>
        public static async Task <Update[]> LocationUpdateHandler(Conversation conversation, Update update,
                                                                  IntellectInstanse intellectInstance)
        {
            if (update?.Location?.Latidute == null || update?.Location?.Longitude == null)
            {
                throw new Exception("No location");
            }

            //setting up a correct context inside api.ai
            var intellectResponse =
                intellectInstance.GetResponse($"Recieved location lat:{update.Location.Latidute}, lon:{update.Location.Longitude}");

            switch (intellectResponse.Intent.ToLower())
            {
            //we need to exit weather or AnotherCityWeather intent in case api.ai awaits a city name
            case "weather":
                intellectInstance.GetResponse("Moscow");
                intellectInstance.GetResponse($"Recieved location lat:{update.Location.Latidute}, lon:{update.Location.Longitude}");
                break;

            case "anothercityweather":
                goto case "weather";

            //set this place as default in case the intent is DefaultCitySetUp
            case "defaultcitysetup":
                intellectInstance.GetResponse("Moscow");
                try
                {
                    await dbController.SetDefaultCityAsync(conversation, $"{update.Location.Latidute},{update.Location.Longitude}");
                }
                catch
                {
                    return(new Update[] { new Update(UpdateType.Message, "ERROR!"),
                                          new Update(UpdateType.Message, "The location was not set up as default.") });
                }
                return(new Update[] { new Update(UpdateType.Message, "The location was set up as default.") });
            }

            return(new Update[] { (await GetLocationWeatherAsync(update.Location)) });
        }
예제 #2
0
        /// <summary>
        /// Generates a response for an Update with text.
        /// </summary>
        /// <param name="conversation"></param>
        /// <param name="update"></param>
        /// <param name="intellectInstance"></param>
        /// <returns></returns>
        public static async Task <Update[]> TextUpdateHandler(Conversation conversation, Update update,
                                                              IntellectInstanse intellectInstance)
        {
            if (update.Text == String.Empty || update.Text == null)
            {
                throw new Exception("Empty update");
            }

            var intellectResponse = intellectInstance.GetResponse(update.Text);

            //genarate a reply
            switch (intellectResponse.Action)
            {
            case "GetWeather":
                string city = intellectResponse.Parameters["geo-city"].ToString();
                if (city == "")
                {
                    //getting a default city from database
                    string defaultCity = await dbController.GetDefaultCityAsync(conversation);

                    //if there's no default city, pass API.AI message
                    if (defaultCity == null || defaultCity == "")
                    {
                        goto default;
                    }

                    //checking wether default location is set with coordinates
                    if (Regex.IsMatch(defaultCity, ".*[0-9]+.*"))
                    {
                        return(new Update[]
                               { new Update(UpdateType.Message, "Data on your default location:"),
                                 await GetLocationWeatherAsync(new GeoLocation(Regex.Match(defaultCity, "^[^\\,]+").Value,
                                                                               Regex.Match(defaultCity, "[^\\,]+$").Value)) });
                    }

                    city = defaultCity;
                    intellectInstance.GetResponse(defaultCity);
                }
                //post weather conditions to the user
                return(new Update[] { await GetCityWeatherAsync(city) });

            case "DefaultLocationSetUp":
                string latitude   = intellectResponse.Parameters["lat"].ToString();
                string longtidute = intellectResponse.Parameters["lon"].ToString();
                if (latitude != "" && longtidute != "")
                {
                    await dbController.SetDefaultCityAsync(conversation, $"{latitude},{longtidute}");
                }
                goto default;

            case "DefaultCitySetUp":
                city = intellectResponse.Parameters["geo-city"].ToString();
                if (city != "")
                {
                    await dbController.SetDefaultCityAsync(conversation, city);
                }
                goto default;

            default:
                //return a reply from IntellectInstance
                return(new Update[] { new Update(UpdateType.Message, intellectResponse.Speech ?? "") });
            }
        }