public async Task StartAsync(CancellationToken cancellationToken)
        {
            var webHookInfo = await _client.GetWebhookInfoAsync(cancellationToken);

            if (webHookInfo != null)
            {
                if (_configuration.WebHookConfiguration.UseWebHook &&
                    webHookInfo.Url.Equals(_configuration.WebHookConfiguration.WebHookUrl, StringComparison.OrdinalIgnoreCase))
                {
                    return;
                }

                await _client.DeleteWebhookAsync(cancellationToken);
            }

            if (!_configuration.WebHookConfiguration.UseWebHook)
            {
                throw new InvalidOperationException("You can't use this host for polling bots");
            }

            if (string.IsNullOrWhiteSpace(_configuration.WebHookConfiguration.WebHookUrl))
            {
                throw new ArgumentNullException($"Empty webhook url: {nameof(_configuration.WebHookConfiguration.WebHookUrl)}");
            }

            await _client.SetWebhookAsync(_configuration.WebHookConfiguration.WebHookUrl, cancellationToken : cancellationToken);
        }
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            await _client.DeleteWebhookAsync(cancellationToken);

            _logger.Information("Start webhook with {url} {token}", _configuration.WebhookUrl, _configuration.BotToken);
            await _client.SetWebhookAsync($"{_configuration.WebhookUrl}/{_configuration.BotToken}",
                                          cancellationToken : cancellationToken);

            _logger.Information("End set webhook");
        }
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            await _client.DeleteWebhookAsync(cancellationToken);

            _logger.Information($"Setting web-hook with {_configuration.WebhookUrl} {_configuration.BotToken}");

            await _client.SetWebhookAsync(_configuration.ToString(), cancellationToken : cancellationToken);

            _logger.Information("End setting web-hook");
        }
Ejemplo n.º 4
0
        public static async Task Main(string[] args)
        {
            try
            {
                var host  = CreateHostBuilder(args).Build();
                var token = Environment.GetEnvironmentVariable("BOT_TOKEN");
                _botClient = new TelegramBotClient(token);
                await _botClient.DeleteWebhookAsync();

                _botClient.OnMessage += Bot_OnMessage;
                _botClient.StartReceiving();

                _timer = new Timer {
                    Interval = TimeSpan.FromMinutes(28).TotalMilliseconds, AutoReset = true
                };
                _timer.Elapsed += delegate(object sender, ElapsedEventArgs eventArgs)
                {
                    var httpClient = new HttpClient();
                    var result     = httpClient.GetAsync("https://zkolos-bot.herokuapp.com/telegram/check").GetAwaiter().GetResult();
                };

                _timer.Start();
                await host.RunAsync();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);

                Debug.WriteLine(e.Message);
                Debug.WriteLine(e.StackTrace);
            }
        }
Ejemplo n.º 5
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            var address = _configuration.GetValue <string>("WebHookAddress");

            _logger.LogInformation("Removing WebHook");
            await _telegramBotClient.DeleteWebhookAsync(cancellationToken);

            _logger.LogInformation("Setting WebHook to {Address}", address);
            await _telegramBotClient.SetWebhookAsync(
                address,
                maxConnections : 5,
                cancellationToken : cancellationToken);

            _logger.LogInformation("WebHook is set to {Address}", address);

            var webHookInfo = await _telegramBotClient.GetWebhookInfoAsync(cancellationToken);

            _logger.LogInformation("WebHook info: {Info}", JsonConvert.SerializeObject(webHookInfo));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> UnSubscribe(string id)
        {
            if (id == _webApiConfiguration.SubscribeKey)
            {
                await _botClient.DeleteWebhookAsync();

                return(Ok("Unsubscribed"));
            }
            return(Forbid("Unauthorized"));
        }
Ejemplo n.º 7
0
        private async Task InitWebHook(string webHookUrl)
        {
            var webHookInfo = await _botClient.GetWebhookInfoAsync();

            if (!string.IsNullOrEmpty(webHookInfo.Url))
            {
                await _botClient.DeleteWebhookAsync();
            }

            await _botClient.SetWebhookAsync(webHookUrl);
        }
Ejemplo n.º 8
0
        public async Task Start(CancellationToken cancellationToken = default)
        {
            _client = await _clientFactory.Create(_options.Connection.ApiToken);

            // webhooks not supported
            WebhookInfo webhookInfo = await _client.GetWebhookInfoAsync(cancellationToken);

            if (!string.IsNullOrEmpty(webhookInfo.Url))
            {
                _logger.LogWarning("A webhook is set up on the server. Deleting...");
                await _client.DeleteWebhookAsync(cancellationToken);
            }

            _client.OnMessage       += OnClientMessage;
            _client.OnCallbackQuery += OnCallbackQuery;
            _client.StartReceiving(AllowedUpdates, cancellationToken);
            _logger.LogInformation("Started receiving updates.");

            _cancellationToken = cancellationToken;
        }
Ejemplo n.º 9
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ITelegramBotClient client)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            if (!env.IsDevelopment())
            {
                client.DeleteWebhookAsync().Wait();
                client.SetWebhookAsync(
                    url: "https://" + Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME") + "/api",
                    allowedUpdates: new UpdateType[] { UpdateType.Message, UpdateType.CallbackQuery }).Wait();
            }
        }
        public async Task Invoke(HttpContext context)
        {
            var webHookInfo = await _botClient.GetWebhookInfoAsync();

            var webhookExists = !string.IsNullOrEmpty(webHookInfo.Url);

            if (webhookExists)
            {
                if (!ValidateWebhookUri(webHookInfo))
                {
                    await _botClient.DeleteWebhookAsync();

                    await CreateWebhookAsync(context);
                }
            }
            else
            {
                await CreateWebhookAsync(context);
            }

            await _next(context);
        }
        public async void Init()
        {
            if (!string.IsNullOrWhiteSpace(_settings.WEBSITE_URL))
            {
                await _client.SetWebhookAsync(_settings.WEBSITE_URL);
            }
            else
            {
                var whi = await _client.GetWebhookInfoAsync();

                Console.WriteLine(JsonConvert.SerializeObject(whi));

                if (!string.IsNullOrWhiteSpace(whi.Url))
                {
                    await _client.DeleteWebhookAsync();
                }

                int lastId = -1;

                _timer = new Timer(async _ =>
                {
                    try
                    {
                        var updates = await _client.GetUpdatesAsync(lastId + 1);
                        lastId      = updates.LastOrDefault()?.Id ?? lastId;
                        foreach (var up in updates)
                        {
                            try
                            {
                                await Process(up);
                            }
                            catch { }
                        }
                    }
                    catch { }
                }, null, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2));
            }
        }
Ejemplo n.º 12
0
 public Task DeleteWebhook()
 {
     return(_botClient.DeleteWebhookAsync());
 }