Telegram.Bot icon indicating copy to clipboard operation
Telegram.Bot copied to clipboard

Bot delay after update from version 16

Open uponatime2019 opened this issue 3 years ago • 4 comments

I just upgrade code from version 16 (without polling), notice bot delay. May be something wrong with my code. Please take a look

public static async Task StartBot()
        {
            var receiverOptions = new ReceiverOptions
            {
                AllowedUpdates = { }, // receive all update types
                ThrowPendingUpdates = true,
            };

            try
            {
                await BotClient.ReceiveAsync(
                    HandleUpdateAsync,
                    HandleErrorAsync,
                    receiverOptions);
            }
            catch (Exception ex)
            {

            }
        }

 private static async Task HandleUpdateAsync(ITelegramBotClient client, Update update, CancellationToken token)
        {
            try
            {
                switch (update.Type)
                {
                    case UpdateType.Message:
                        Bot_OnMessage(update);
                        break;
                }
            }
            catch (Exception ex)
            {
            }
        }

private static async void Bot_OnMessage(Update e) {}

I try change HandleUpdate code to this (add await and async Task), but it still delay (15-150s)


 private static async Task HandleUpdateAsync(ITelegramBotClient client, Update update, CancellationToken token)
        {
            try
            {
                switch (update.Type)
                {
                    case UpdateType.Message:
                        await Bot_OnMessage(update);
                        break;
                }
            }
            catch (Exception ex)
            {
            }
        }

private static async Task Bot_OnMessage(Update e) {}

uponatime2019 avatar Mar 08 '22 15:03 uponatime2019

It's a known problem. We are still investigating its origin

tuscen avatar Mar 08 '22 16:03 tuscen

It's a known problem. We are still investigating its origin

Is any quick fix? The bot almost stopped after run for few hours. Im using stable version

uponatime2019 avatar Mar 08 '22 16:03 uponatime2019

Unfortunately, no. I recommend to downgrade to v16.

tuscen avatar Mar 08 '22 17:03 tuscen

Unfortunately, no. I recommend to downgrade to v16.

Is v18 fix this issue?

uponatime2019 avatar Mar 03 '23 06:03 uponatime2019

Unfortunately, no. I recommend to downgrade to v16.

I just upgraded library to v19 and still same issue. Downgrade to v16 again 🥲

uponatime2019 avatar Mar 09 '24 15:03 uponatime2019

It's probably not a bug. This is intended behaviour because built-in polling mechanism processes each update sequentially and awaits each task before processing the next update. Your best bet is to offload update processing to other threads and make it parallel yourself.

tuscen avatar Mar 09 '24 18:03 tuscen

It's probably not a bug. This is intended behaviour because built-in polling mechanism processes each update sequentially and awaits each task before processing the next update. Your best bet is to offload update processing to other threads and make it parallel yourself.

can you give any code suggestion. My bot read all messages from group, not only messages start with /command.

uponatime2019 avatar Mar 10 '24 05:03 uponatime2019

You should ask for help in our support group here. We use Github issues mainly for bug reports and development purposes

tuscen avatar Mar 10 '24 10:03 tuscen

You should ask for help in our support group here. We use Github issues mainly for bug reports and development purposes

An example how v19 slow. Run the bot and forward 100 messages to bot and calculate how many time it take Result v16 [3/11/2024 10:54:35] Run time 0.690716s. Speed 146.23 msg/s. Total 101

v19 [3/11/2024 11:02:08] Total time 22.8088313. 4.43 msg/s. Total 101

v16 testing code

public static async void _StartCryptoBot()
        {
            try
            {
                CryptoBotClient = TelegramBot.GetClient(CryptoCommon.KEY_TEST_BOT);
                CryptoBotClient.OnMessage += CryptoBotClient_OnMessage;
                CryptoBotClient.StartReceiving();
            }
            catch (Exception ex)
            {
                ex.ConsoleWriteLine(nameof(StartCryptoBot));
            }
        }

        private static void CryptoBotClient_OnMessage(object sender, MessageEventArgs e)
        {
            if (StartTime == null)
            {
                StartTime = DateTime.Now;
            }
            count++;
            var since = StartTime.Value.GetTimeSince();
            $"Run time {since.TotalSeconds}s. Speed {(count / since.TotalSeconds).FormatDecimal2()} msg/s. Total {count}".ConsoleWriteLine();
        }

        static int count = 1;
        static DateTime? StartTime;

v19 testing code

public static async void StartCryptoBot()
        {
            try
            {
                CryptoBotClient = TelegramBot.GetClient(CryptoCommon.KEY_TEST_BOT);
                CryptoBotClient.StartReceiving(
   updateHandler: HandleUpdateAsync2,
   pollingErrorHandler: HandlePollingErrorAsync2,
   receiverOptions: TelegramBot.BotReceiverOptions);
            }
            catch (Exception ex)
            {
                ex.ConsoleWriteLine(nameof(StartCryptoBot));
            }
        }

        private static async Task HandlePollingErrorAsync2(ITelegramBotClient client, Exception exception, CancellationToken token)
        {
        }

        static int count = 1;
        static DateTime? StartTime;
        private static async Task HandleUpdateAsync2(ITelegramBotClient client, Update update, CancellationToken token)
        {
            _ = Task.Run(() =>
            {
                if (update.Type == UpdateType.Message)
                {
                    if (StartTime == null)
                    {
                        StartTime = DateTime.Now;
                    }
                    count++;
                    var since = StartTime.Value.GetTimeSince();
                    $"Total time {since.TotalSeconds}. {(count / since.TotalSeconds).FormatDecimal2()} msg/s. Total {count}".ConsoleWriteLine();
                }
            });
        }

uponatime2019 avatar Mar 11 '24 05:03 uponatime2019

Fixed by adding Limit = 100 in receiver options. Will be default value in next major version (v20+)

Fedorus avatar Mar 11 '24 18:03 Fedorus