TelegramBots / Telegram.Bot.Examples

Examples for the Telegram.Bot C# Library
MIT License
630 stars 289 forks source link

Systematically stopping the WebHook bot from receiving messages #490

Closed fox-15 closed 2 months ago

fox-15 commented 11 months ago

Good afternoon For my project I am using the Telegram.Bot v.19.0.0 library, doing some research and testing. I took the example Telegram.Bot.Examples.WebHook as a basis. I configured the IIS server so that it is always active, configured certificates and everything necessary. The bot started working and receiving messages. And everything seems to be in order! The bot receives messages throughout the day, but constantly stops receiving messages. Sometimes after 2 hours, sometimes after 12 hours, different all the time. If I stop the server and start it again, the bot starts receiving messages again Recently he received messages for almost 24 hours, but he stopped receiving messages again. What could be the reason for these stops specifically when receiving messages? I followed all the recommendations to configure the IIS server, my test application guaranteed to work, the bot sends a message every hour and never stops sending, but receiving messages systematically stops

InnerCat commented 11 months ago

Periodically IIS puts all running applications to sleep and the method StopAsync from ConfigureWebhook is triggered. Change the method like this and everything should be fine :)

public async Task StopAsync(CancellationToken cancellationToken)
    {
        /*
        using var scope = _serviceProvider.CreateScope();
        var botClient = scope.ServiceProvider.GetRequiredService<ITelegramBotClient>();

        // Remove webhook on app shutdown
        _logger.LogInformation("Removing webhook");
        await botClient.DeleteWebhookAsync(cancellationToken: cancellationToken);
        */
    }

But remember that the webhook for this bot will exist until you stop it or start a new one with a different URL

wiz0u commented 2 months ago

fixed by 66878d67bb9a9e05dabfda23e5d50fb753d7fcea