Search code examples
c#.net-coretelegram-bottelegram-apiwtelegramclient

WTelegramClient Messages_GetBotCallbackAnswer always throw BOT_RESPONSE_TIMEOUT exception


im trying to write simple bot on C# .net8-0 and im need to press some inline buttons in messages, but im always see error BOT_RESPONSE_TIMEOUT, here is my code

bool result = false;
if (GetInputPeer(chatId, out InputPeer? inputPeer) && inputPeer is not null)
{
    try
    {
        // get last messages in chat
        var history = await Client.Messages_GetHistory(inputPeer, limit: 50); // taking more messages for reliability
        Console.WriteLine($"Found {history.Messages.Length} messages in history.");

        // finding first message with needed inline button by callbackData
        foreach (var messageBase in history.Messages)
        {
            if (result) break;
            if (messageBase is Message message && message.reply_markup is ReplyInlineMarkup replyMarkup)
            {
                Console.WriteLine($"Found message with inline buttons: ID={message.ID}");

                // sorting out buttons lines 
                foreach (var row in replyMarkup.rows)
                {
                    if (result) break;
                    foreach (var button in row.buttons)
                    {
                        if (result) break;
                        if (button is KeyboardButtonCallback callbackButton &&
                            callbackButton.data.SequenceEqual(Encoding.UTF8.GetBytes(inlineButtonCallbackData)))
                        {
                            Console.WriteLine($"Found matching inline button with callback data: {inlineButtonCallbackData}");

                            // pressing inline button
                            var response = await Client.Messages_GetBotCallbackAnswer(inputPeer, message.ID, callbackButton.data);  // 
                            Console.WriteLine("Button pressed successfully.");
                            result = true; // break after success press
                            break;
                        }
                    }
                }
            }
        }

        if (!result) Console.WriteLine("No matching button with specified callback data found.");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error pressing inline button: {ex.Message}");
    }
}
else
{
    Console.WriteLine($"InputPeer is null, chatId: {chatId} is skipped!");
}
return result;

method GetInputPeer works well, problem only with Messages_GetBotCallbackAnswer, but maybe its becouse of my code, but i dont think so. Thanks for your help and for wasted time

I tried to search in Google, stackoverflow and ask ChatGPT, but nothing works


Solution

  • BOT_RESPONSE_TIMEOUT is returned (thrown) when the bot doesn't acknowledge the button-press within 10 seconds.

    It either mean the bot is offline, or it correctly received your request but didn't call answerCallbackQuery in time.

    In any case, nothing wrong on your side. But you HAVE to try..catch this error because it can happen.