public async Task GetReservasAsync() { try { using (Context.Channel.EnterTypingState()) { BcraResponse result = await BcraService.GetReserves(); if (result != null) { EmbedBuilder embed = await BcraService.CreateReservesEmbedAsync(result); embed.AddCommandDeprecationNotice(Configuration); await ReplyAsync(embed : embed.Build()); } else { await ReplyAsync(REQUEST_ERROR_MESSAGE); } } } catch (Exception ex) { await SendErrorReply(ex); } }
/// <summary> /// Queries the API endpoint asynchronously and returns a <see cref="BcraResponse"/> object. /// </summary> /// <returns>A task that contains a normalized <see cref="BcraResponse"/> object.</returns> public async Task <BcraResponse> GetBcraValue(BcraIndicatorsEndpoints bcraValue) { BcraResponse cachedResponse = Cache.GetObject <BcraResponse>(bcraValue); if (cachedResponse != null) { return(cachedResponse); } else { string endpoint = bcraValue.GetDescription(); RestRequest request = new(endpoint); RestResponse <BcraResponse> response = await Client.ExecuteGetAsync <BcraResponse>(request); if (response.IsSuccessful) { Cache.SaveObject(bcraValue, response.Data); return(response.Data); } else { OnError(response); return(null); } } }
/// <summary> /// Creates an <see cref="EmbedBuilder"/> object for a <see cref="BcraResponse"/>. /// </summary> /// <param name="bcraResponse">The BCRA response.</param> /// <returns>An <see cref="EmbedBuilder"/> object ready to be built.</returns> public async Task <EmbedBuilder> CreateCirculatingMoneyEmbedAsync(BcraResponse bcraResponse) { var emojis = Configuration.GetSection("customEmojis"); Emoji circulatingMoneyEmoji = new(":money_with_wings:"); Emoji whatsappEmoji = new(emojis["whatsapp"]); string reservesImageUrl = Configuration.GetSection("images").GetSection("money")["64"]; string footerImageUrl = Configuration.GetSection("images").GetSection("clock")["32"]; TimeZoneInfo localTimeZone = GlobalConfiguration.GetLocalTimeZoneInfo(); int utcOffset = localTimeZone.GetUtcOffset(DateTime.UtcNow).Hours; string lastUpdated = bcraResponse.Fecha.ToString(bcraResponse.Fecha.Date == TimeZoneInfo.ConvertTime(DateTime.UtcNow, localTimeZone).Date ? "HH:mm" : "dd/MM/yyyy - HH:mm"); bool isNumber = double.TryParse(bcraResponse?.Valor, NumberStyles.Any, DolarBotApiService.GetApiCulture(), out double valor); string text; string value; if (isNumber) { long convertedValue = (long)Math.Round(valor, MidpointRounding.AwayFromZero); value = convertedValue.ToString("n2", GlobalConfiguration.GetLocalCultureInfo()); text = $"{Format.Bold($"{circulatingMoneyEmoji} {GlobalConfiguration.Constants.BLANK_SPACE} $ {value}")}"; } else { value = "No informado"; text = $"{Format.Bold($"{circulatingMoneyEmoji} {GlobalConfiguration.Constants.BLANK_SPACE} {value}")}"; } string shareText = $"*Pesos en circulación*{Environment.NewLine}{Environment.NewLine}{(isNumber ? $"$ *{value}*" : value)}{Environment.NewLine}Hora: {lastUpdated} (UTC {utcOffset})"; EmbedBuilder embed = new EmbedBuilder().WithColor(GlobalConfiguration.Colors.Main) .WithTitle("Pesos en circulación") .WithDescription($"Cantidad total aproximada de {Format.Bold("pesos argentinos")} en circulación.".AppendLineBreak()) .WithThumbnailUrl(reservesImageUrl) .WithFooter(new EmbedFooterBuilder() { Text = $"Ultima actualización: {lastUpdated} (UTC {utcOffset})", IconUrl = footerImageUrl }) .AddInlineField($"Valor", text); await embed.AddFieldWhatsAppShare(whatsappEmoji, shareText, Api.Cuttly.ShortenUrl); return(embed.AddPlayStoreLink(Configuration)); }
public async Task GetCirculanteAsync() { await DeferAsync().ContinueWith(async(task) => { try { BcraResponse result = await BcraService.GetCirculatingMoney(); if (result != null) { EmbedBuilder embed = await BcraService.CreateCirculatingMoneyEmbedAsync(result); await SendDeferredEmbedAsync(embed.Build()); } else { await SendDeferredApiErrorResponseAsync(); } } catch (Exception ex) { await SendDeferredErrorResponseAsync(ex); } }); }