Example #1
0
        public HttpResponseMessage SetPreference(ApiAiModel model)
        {
            var preference = model.Result.Parameters.Preference;
            var value      = model.Result.Parameters.Value;

            try
            {
                Preferences.SetUserPreference(preference, value);
                var result = $"Your preference {preference} has been set to {value}.";

                return(IoOperations.GenerateJsonResponse(result));
            }
            catch (PreferenceException e)
            {
                return(IoOperations.GenerateJsonResponse(e.Message));
            }
        }
Example #2
0
        public HttpResponseMessage WebHook(ApiAiModel model)
        {
            switch (model.Result.Action)
            {
            case "delay":
                return(Delay(model));

            case "info":
                return(Info(model));

            case "set.preference":
                return(SetPreference(model));

            default:
                return(IoOperations.GenerateJsonResponse("ConductorAPI could not be reached."));
            }
        }
Example #3
0
        public HttpResponseMessage Delay(ApiAiModel model)
        {
            try
            {
                var connections    = Connections.GetConnections(model);
                var connection     = connections.ConnectionList[0];
                var nextConnection = connections.ConnectionList[0];

                string result;

                if (connection.Departure.IsCanceled)
                {
                    result =
                        $"The train from {connection.Departure.Station} to {connection.Arrival.Station} at {connection.Departure.Time.DateTime.TimeOfDay} has been canceled." +
                        $"The next train will be departing at {nextConnection.Departure.Time.DateTime:HH:mm}.";
                }
                else
                {
                    if (connection.Departure.Delay > 0)
                    {
                        result =
                            $"The train from {connection.Departure.Station} to {connection.Arrival.Station} has a delay of {connection.Departure.Delay / 60} minutes. " +
                            $"It will be departing at {connection.Departure.Time.DateTime.AddSeconds(connection.Departure.Delay):HH:mm}.";
                    }
                    else
                    {
                        result =
                            $"The train from {connection.Departure.Station} to {connection.Arrival.Station} has no delay. " +
                            $"It will be departing at {connection.Departure.Time.DateTime:HH:mm}.";
                    }
                }

                return(IoOperations.GenerateJsonResponse(result));
            }
            catch (PreferenceException e)
            {
                return(IoOperations.GenerateJsonResponse(e.Message));
            }
            catch (Exception)
            {
                return(IoOperations.GenerateJsonResponse("I'm sorry, something went wrong."));
            }
        }
Example #4
0
        public HttpResponseMessage Info(ApiAiModel model)
        {
            try
            {
                var connections = Connections.GetConnections(model);
                var connection  = connections.ConnectionList.First(x => x.Departure.IsCanceled == false);

                var result =
                    $"The train from {connection.Departure.Station} to {connection.Arrival.Station} will be departing at {connection.Departure.Time.DateTime:HH:mm}.";

                return(IoOperations.GenerateJsonResponse(result));
            }
            catch (PreferenceException e)
            {
                return(IoOperations.GenerateJsonResponse(e.Message));
            }
            catch (Exception)
            {
                return(IoOperations.GenerateJsonResponse("I'm sorry, something went wrong."));
            }
        }
Example #5
0
        public static Models.iRail.Connections GetConnections(ApiAiModel model)
        {
            var from = model.Result.Parameters.From;
            var to   = model.Result.Parameters.To;
            var time = model.Result.Parameters.Time;

            if (to == "Work")
            {
                to = Preferences.Preferences.GetUserPreference("Work");
            }

            if (string.IsNullOrEmpty(from))
            {
                from = Preferences.Preferences.GetUserPreference("Home");
            }

            var apiUrl = $"http://api.irail.be/connections/?to={to}&from={from}";

            if (!string.IsNullOrEmpty(time))
            {
                var ts            = TimeSpan.FromHours(int.Parse(time));
                var formattedTime = ts.ToString("hhmm");

                apiUrl = $"http://api.irail.be/connections/?to={to}&from={from}&time={formattedTime}";
            }

            var xmlResponse = IoOperations.GetXmlDocumentFromUrl(apiUrl);

            Models.iRail.Connections result;

            using (TextReader sr = new StringReader(xmlResponse.OuterXml))
            {
                var serializer = new XmlSerializer(typeof(Models.iRail.Connections));
                result = (Models.iRail.Connections)serializer.Deserialize(sr);
            }

            return(result);
        }