Код: Выделить всё
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;
Спасибо за помощь и потраченное время
Я пробовал искать в Google, stackoverflow и задавать ChatGPT, но ничего не получается
Подробнее здесь: https://stackoverflow.com/questions/792 ... se-timeout