Bot delay after update from version 16
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) {}
It's a known problem. We are still investigating its origin
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
Unfortunately, no. I recommend to downgrade to v16.
Unfortunately, no. I recommend to downgrade to v16.
Is v18 fix this issue?
Unfortunately, no. I recommend to downgrade to v16.
I just upgraded library to v19 and still same issue. Downgrade to v16 again 🥲
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.
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.
You should ask for help in our support group here. We use Github issues mainly for bug reports and development purposes
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();
}
});
}
Fixed by adding Limit = 100 in receiver options. Will be default value in next major version (v20+)