Ejemplo n.º 1
0
        public async Task GetRiesgoPaisValueAsync()
        {
            try
            {
                using (Context.Channel.EnterTypingState())
                {
                    CountryRiskResponse result = await BcraService.GetCountryRisk();

                    if (result != null)
                    {
                        EmbedBuilder embed = await BcraService.CreateCountryRiskEmbedAsync(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="CountryRiskResponse"/> object.
        /// </summary>
        /// <returns>A task that contains a normalized <see cref="CountryRiskResponse"/> object.</returns>
        public async Task <CountryRiskResponse> GetCountryRiskValue()
        {
            CountryRiskResponse cachedResponse = Cache.GetObject <CountryRiskResponse>(RIESGO_PAIS_CACHE_KEY);

            if (cachedResponse != null)
            {
                return(cachedResponse);
            }
            else
            {
                RestRequest request = new(BcraIndicatorsEndpoints.RiesgoPais.GetDescription());
                RestResponse <CountryRiskResponse> response = await Client.ExecuteGetAsync <CountryRiskResponse>(request);

                if (response.IsSuccessful)
                {
                    Cache.SaveObject(RIESGO_PAIS_CACHE_KEY, response.Data);
                    return(response.Data);
                }
                else
                {
                    OnError(response);
                    return(null);
                }
            }
        }
        public Task <CountryRiskResponse> GetCountryRiskAsync(string countryCode)
        {
            var result = new CountryRiskResponse
            {
                Risk = _mapper.Map <RiskModel?>(_settingsService.GetCountryRisk(countryCode))
            };

            return(Task.FromResult(result));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates an <see cref="EmbedBuilder"/> object for a <see cref="CountryRiskResponse"/>.
        /// </summary>
        /// <param name="riesgoPaisResponse">The Riesgo Pais response.</param>
        /// <returns>An <see cref="EmbedBuilder"/> object ready to be built.</returns>
        public async Task <EmbedBuilder> CreateCountryRiskEmbedAsync(CountryRiskResponse riesgoPaisResponse)
        {
            var    emojis         = Configuration.GetSection("customEmojis");
            Emoji  chartEmoji     = new("\uD83D\uDCC8");
            Emoji  whatsappEmoji  = new(emojis["whatsapp"]);
            string riskImageUrl   = Configuration.GetSection("images").GetSection("risk")["64"];
            string footerImageUrl = Configuration.GetSection("images").GetSection("clock")["32"];

            TimeZoneInfo localTimeZone = GlobalConfiguration.GetLocalTimeZoneInfo();
            int          utcOffset     = localTimeZone.GetUtcOffset(DateTime.UtcNow).Hours;
            string       lastUpdated   = riesgoPaisResponse.Fecha.ToString(riesgoPaisResponse.Fecha.Date == TimeZoneInfo.ConvertTime(DateTime.UtcNow, localTimeZone).Date ? "HH:mm" : "dd/MM/yyyy - HH:mm");
            bool         isNumber      = double.TryParse(riesgoPaisResponse?.Valor, NumberStyles.Any, DolarBotApiService.GetApiCulture(), out double valor);
            string       value;

            if (isNumber)
            {
                int convertedValue = (int)Math.Round(valor, MidpointRounding.AwayFromZero);
                value = convertedValue.ToString("n0", GlobalConfiguration.GetLocalCultureInfo());
            }
            else
            {
                value = "No informado";
            }

            string       shareText = $"*Riesgo país*{Environment.NewLine}{Environment.NewLine}{(isNumber ? $"*{value}* puntos" : value)}{Environment.NewLine}Hora: {lastUpdated}";
            EmbedBuilder embed     = new EmbedBuilder().WithColor(GlobalConfiguration.Colors.Main)
                                     .WithTitle("Riesgo País")
                                     .WithDescription($"Valor del {Format.Bold("riesgo país")} de la República Argentina.".AppendLineBreak())
                                     .WithThumbnailUrl(riskImageUrl)
                                     .WithFooter(new EmbedFooterBuilder()
            {
                Text    = $"Ultima actualización: {lastUpdated} (UTC {utcOffset})",
                IconUrl = footerImageUrl
            })
                                     .AddInlineField($"Valor", $"{Format.Bold($"{chartEmoji} {GlobalConfiguration.Constants.BLANK_SPACE} {value}")} puntos".AppendLineBreak());

            await embed.AddFieldWhatsAppShare(whatsappEmoji, shareText, Api.Cuttly.ShortenUrl);

            return(embed.AddPlayStoreLink(Configuration));
        }
 public async Task GetRiesgoPaisValueAsync()
 {
     await DeferAsync().ContinueWith(async(task) =>
     {
         try
         {
             CountryRiskResponse result = await BcraService.GetCountryRisk();
             if (result != null)
             {
                 EmbedBuilder embed = await BcraService.CreateCountryRiskEmbedAsync(result);
                 await SendDeferredEmbedAsync(embed.Build());
             }
             else
             {
                 await SendDeferredApiErrorResponseAsync();
             }
         }
         catch (Exception ex)
         {
             await SendDeferredErrorResponseAsync(ex);
         }
     });
 }