Esempio n. 1
0
        public TelegramServiceImpl(IOptions <TelegramOptions> options,
                                   IHttpClientFactory clientFactory,
                                   ILogger <TelegramServiceImpl> logger)
        {
            this.clientFactory = clientFactory;
            this.logger        = logger;
            this.options       = options.Value;
            this.options.ThrowIfInvalid();

            this.sendMessageEndpoint = string.Concat("/bot", this.options.Token, "/sendMessage");
        }
        public static void AddTelegramClient(this IServiceCollection services, IConfiguration configuration)
        {
            ConnectionOptions connOptions = ConnectionOptions.FromConfiguration(configuration);

            connOptions.ThrowIfInvalid();

            services.AddHttpClient(HttpClientFactoryExtensions.ClientName)
            .AddTransientHttpErrorPolicy(builder => builder.WaitAndRetryAsync(connOptions.Attempts, i => TimeSpan.FromSeconds(connOptions.Interval)))
            .ConfigureHttpClient((di, client) =>
            {
                TelegramOptions tgramOptions = di.GetRequiredService <IOptions <TelegramOptions> >().Value;
                client.Timeout     = TimeSpan.FromSeconds(connOptions.Timeout);
                client.BaseAddress = tgramOptions.Endpoint;
            });
        }
Esempio n. 3
0
 public static void AddTelegramService(this IServiceCollection services, IConfiguration configuration)
 {
     services.Configure <TelegramOptions>(TelegramOptions.From(configuration));
     services.AddSingleton <ITelegramService, TelegramServiceImpl>();
     services.AddTelegramClient(configuration);
 }