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);
        }
Пример #2
0
    public async Task GetWebHookInfo()
    {
        var webhookInfo = await _botClient.GetWebhookInfoAsync();

        _logger.LogInformation($"Webhook info: {webhookInfo.Url}");
        var webHookInfoStr = webhookInfo.ParseWebHookInfo();

        await _botClient.SendTextMessageAsync("", webHookInfoStr.ToString());
    }
Пример #3
0
        private async Task InitWebHook(string webHookUrl)
        {
            var webHookInfo = await _botClient.GetWebhookInfoAsync();

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

            await _botClient.SetWebhookAsync(webHookUrl);
        }
Пример #4
0
    private void UpdateWebHook(BotConfiguration configuration)
    {
        var webHookUrl    = $"https://{configuration.HostN}/WebHook/Post";
        var webHookInfo   = _botClient.GetWebhookInfoAsync().GetAwaiter();
        var setWebHookUrl = webHookInfo.GetResult().Url;

        _logger.LogWarning("Webhook set on the server: {SetWebHookUrl}", setWebHookUrl);
        if (setWebHookUrl == webHookUrl)
        {
            return;
        }

        _botClient.SetWebhookAsync(webHookUrl).Wait();
    }
Пример #5
0
 public void SetWebHook(string url)
 {
     try
     {
         var webHookInfo = _client.GetWebhookInfoAsync().Result;
         if (string.IsNullOrEmpty(webHookInfo.Url) || webHookInfo.Url != url)
         {
             _client.SetWebhookAsync(url).Wait();
         }
     }
     catch (Exception e)
     {
         Trace.WriteLine($"Error setting telegram webhook: {e}");
     }
 }
Пример #6
0
        private async Task <IDictionary <string, object> > GetStatusData(CancellationToken cancellationToken)
        {
            var status = new Dictionary <string, object>
            {
                { "botInfo", await myBot.GetMeAsync(cancellationToken) },
                { "webhookInfo", await myBot.GetWebhookInfoAsync(cancellationToken) },
                { "is64BitProcess", System.Environment.Is64BitProcess },
            };

            foreach (var statusProvider in myStatusProviders)
            {
                await statusProvider.Handle(status, ControllerContext, cancellationToken);
            }

            return(status);
        }
Пример #7
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));
        }
Пример #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;
        }
        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));
            }
        }
Пример #11
0
 public Task <WebhookInfo> GetWebhookInfo()
 {
     return(_botClient.GetWebhookInfoAsync());
 }