Example #1
0
        public static async Task <Update[]> NewUpdateHandler(Conversation conversation, Update update)
        {
            List <Update>     resultingMessages = new List <Update>();
            IntellectInstanse intellectInstance;
            Tuple <int, bool> dictionaryValue;

            //Getting ApiAi object to call the service and getting a bool value which indicates wether it is the first  message in the conversation
            try
            {
                dictionaryValue = apiDictionary[new Tuple <string, string, string>(conversation.Channel, conversation.Id,
                                                                                   conversation.Platfrom)];
                intellectInstance = IntellectInstanse.GetInstance(dictionaryValue.Item1);
            }
            catch
            {
                OnStart(conversation);
                dictionaryValue = apiDictionary[new Tuple <string, string, string>(conversation.Channel, conversation.Id,
                                                                                   conversation.Platfrom)];
                intellectInstance = IntellectInstanse.GetInstance(dictionaryValue.Item1);
            }

            //check wether its the first message and if so - add greeting messages to the output and insert the data into the apiDictionary
            if (dictionaryValue.Item2)
            {
                resultingMessages.InsertRange(0, GetGreetingMessages());
                apiDictionary[new Tuple <string, string, string>(conversation.Channel, conversation.Id,
                                                                 conversation.Platfrom)] = new Tuple <int, bool>(intellectInstance.Idx, false);
            }

            //create response to the text inside the message
            if (update?.Text != null && update.Text?.Length != 0)
            {
                resultingMessages.AddRange(await TextUpdateHandler(conversation, update, intellectInstance));
            }

            //create response to the location inside the message
            if (update?.Location != null)
            {
                resultingMessages.AddRange(await LocationUpdateHandler(conversation, update, intellectInstance));
            }

            return(resultingMessages.ToArray());
        }
Example #2
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)) });
        }
Example #3
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 ?? "") });
            }
        }