public override async Task ExecuteAsync(Update update, Dictionary<string, string> parsedMessage) { var responseStream = new StreamReader(await GetCurrencyRates()); var xmlDeserializer = new XmlSerializer(typeof(CurrencyContainerXml.DailyExRates)); var currencyContainer = new CurrencyContainerXml() { DailyRates = xmlDeserializer.Deserialize(responseStream) as CurrencyContainerXml.DailyExRates }; // Get data for previous date for comparison if (_previousDayCurrencyContainer == null || DateTime.ParseExact(_previousDayCurrencyContainer.DailyRates.Date, "MM/dd/yyyy", null).AddDays(1) != DateTime.ParseExact(currencyContainer.DailyRates.Date, "MM/dd/yyyy", null)) { var ondate = DateTime.ParseExact(currencyContainer.DailyRates.Date, "MM/dd/yyyy", null).AddDays(-1); // Rates do not update on weekend (at least here, duh) if (ondate.DayOfWeek == DayOfWeek.Sunday) { ondate = ondate.AddDays(-2); } var ondatestring = ondate.ToString(@"MM\/dd\/yyyy"); responseStream = new StreamReader(await GetCurrencyRates(OnDateParam + ondatestring)); _previousDayCurrencyContainer = new CurrencyContainerXml() { DailyRates = xmlDeserializer.Deserialize(responseStream) as CurrencyContainerXml.DailyExRates }; } var sb = new StringBuilder(); sb.Append("Курсы валют на "); sb.AppendLine(DateTime.ParseExact(currencyContainer.DailyRates.Date, "MM/dd/yyyy", null).ToShortDateString()); sb.Append("По отношению к "); sb.AppendLine(DateTime.ParseExact(_previousDayCurrencyContainer.DailyRates.Date, "MM/dd/yyyy", null).ToShortDateString()); sb.AppendLine(); List<string> currenciesList = new List<string>(); if (parsedMessage.ContainsKey("message")) { var names = parsedMessage["message"].Split(',').ToList(); currenciesList.AddRange(names.Select(cname => cname.ToUpper())); } if (currenciesList.Count == 0) currenciesList = _defaultCurrenciesList; foreach (var currency in currencyContainer.DailyRates.Currency.Where(x => currenciesList.Contains(x.CharCode))) { sb.Append(currency.CharCode + ": " + currency.Rate); if (_previousDayCurrencyContainer != null) { var diff = currency.Rate - _previousDayCurrencyContainer.DailyRates.Currency.First( x => x.CharCode == currency.CharCode).Rate; sb.Append(" _("); sb.Append(diff > 0 ? "+" : "-"); sb.Append(Math.Abs(diff)); sb.Append(")_"); } sb.AppendLine(); } await Bot.SendTextMessage(update.Message.Chat.Id, sb.ToString(), false, update.Message.MessageId, null, true); }
public override async Task ExecuteAsync(Update update, Dictionary <string, string> parsedMessage) { var responseStream = new StreamReader(await GetCurrencyRates()); var xmlDeserializer = new XmlSerializer(typeof(CurrencyContainerXml.DailyExRates)); var currencyContainer = new CurrencyContainerXml() { DailyRates = xmlDeserializer.Deserialize(responseStream) as CurrencyContainerXml.DailyExRates }; // Get data for previous date for comparison if (_previousDayCurrencyContainer == null || DateTime.ParseExact(_previousDayCurrencyContainer.DailyRates.Date, "MM/dd/yyyy", null).AddDays(1) != DateTime.ParseExact(currencyContainer.DailyRates.Date, "MM/dd/yyyy", null)) { var ondate = DateTime.ParseExact(currencyContainer.DailyRates.Date, "MM/dd/yyyy", null).AddDays(-1); // Rates do not update on weekend (at least here, duh) if (ondate.DayOfWeek == DayOfWeek.Sunday) { ondate = ondate.AddDays(-2); } var ondatestring = ondate.ToString(@"MM\/dd\/yyyy"); responseStream = new StreamReader(await GetCurrencyRates(OnDateParam + ondatestring)); _previousDayCurrencyContainer = new CurrencyContainerXml() { DailyRates = xmlDeserializer.Deserialize(responseStream) as CurrencyContainerXml.DailyExRates }; } var sb = new StringBuilder(); sb.Append("Курсы валют на "); sb.AppendLine(DateTime.ParseExact(currencyContainer.DailyRates.Date, "MM/dd/yyyy", null).ToShortDateString()); sb.Append("По отношению к "); sb.AppendLine(DateTime.ParseExact(_previousDayCurrencyContainer.DailyRates.Date, "MM/dd/yyyy", null).ToShortDateString()); sb.AppendLine(); List <string> currenciesList = new List <string>(); if (parsedMessage.ContainsKey("message")) { var names = parsedMessage["message"].Split(',').ToList(); currenciesList.AddRange(names.Select(cname => cname.ToUpper())); } if (currenciesList.Count == 0) { currenciesList = _defaultCurrenciesList; } foreach (var currency in currencyContainer.DailyRates.Currency.Where(x => currenciesList.Contains(x.CharCode))) { sb.Append(currency.CharCode + ": " + currency.Rate); if (_previousDayCurrencyContainer != null) { var diff = currency.Rate - _previousDayCurrencyContainer.DailyRates.Currency.First( x => x.CharCode == currency.CharCode).Rate; sb.Append(" _("); sb.Append(diff > 0 ? "+" : "-"); sb.Append(Math.Abs(diff)); sb.Append(")_"); } sb.AppendLine(); } await Bot.SendTextMessage(update.Message.Chat.Id, sb.ToString(), false, update.Message.MessageId, null, true); }