From 2293f2480829a0a6cf9bbd3b058b0b5b97b412a8 Mon Sep 17 00:00:00 2001 From: HONOR Date: Sat, 2 Aug 2025 03:11:07 +0300 Subject: [PATCH] Add Response, EditText, AnswerCallback in CallbackQueryHandler --- Telegrator/Handlers/CallbackQueryHandler.cs | 82 +++++++++++++++++++++ Telegrator/TypesExtensions.cs | 67 +++++++++++++++++ 2 files changed, 149 insertions(+) diff --git a/Telegrator/Handlers/CallbackQueryHandler.cs b/Telegrator/Handlers/CallbackQueryHandler.cs index 48dce69..6c2f7d3 100644 --- a/Telegrator/Handlers/CallbackQueryHandler.cs +++ b/Telegrator/Handlers/CallbackQueryHandler.cs @@ -1,5 +1,6 @@ using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; +using Telegram.Bot.Types.ReplyMarkups; using Telegrator.Attributes; using Telegrator.Filters.Components; using Telegrator.Handlers.Components; @@ -27,6 +28,87 @@ namespace Telegrator.Handlers /// public abstract class CallbackQueryHandler() : AbstractUpdateHandler(UpdateType.CallbackQuery) { + + /// + /// Sends a response message to the current chat. + /// + /// The text of the message to send. + /// The parse mode for the message text. + /// The reply parameters for the message. + /// The reply markup for the message. + /// Options for link preview generation. + /// The thread ID for forum topics. + /// The message entities to include. + /// Whether to disable notification for the message. + /// Whether to protect the message content. + /// The message effect ID. + /// The business connection ID. + /// Whether to allow paid broadcast. + /// The cancellation token. + /// The sent message. + protected async Task Responce( + string text, + ParseMode parseMode = ParseMode.None, + ReplyParameters? replyParameters = null, + ReplyMarkup? replyMarkup = null, + LinkPreviewOptions? linkPreviewOptions = null, + int? messageThreadId = null, + IEnumerable? entities = null, + bool disableNotification = false, + bool protectContent = false, + string? messageEffectId = null, + string? businessConnectionId = null, + bool allowPaidBroadcast = false, + CancellationToken cancellationToken = default) + => await Container.Responce( + text, parseMode, replyParameters, + replyMarkup, linkPreviewOptions, + messageThreadId, entities, + disableNotification, protectContent, + messageEffectId, businessConnectionId, + allowPaidBroadcast, cancellationToken); + + /// + /// Edits the current callback message with new text. + /// + /// The new text of the message. + /// The parse mode for the message text. + /// The reply markup for the message. + /// The message entities to include. + /// Options for link preview generation. + /// The message effect ID. + /// The cancellation token. + /// The edited message. + protected async Task EditMessage( + string text, + ParseMode parseMode = ParseMode.None, + InlineKeyboardMarkup? replyMarkup = null, + IEnumerable? entities = null, + LinkPreviewOptions? linkPreviewOptions = null, + CancellationToken cancellationToken = default) + => await Container.EditMessage( + text, parseMode, replyMarkup, + entities, linkPreviewOptions, cancellationToken); + + /// + /// Answers the current callback query with optional alert or message. + /// + /// The text to display in the callback answer. + /// Whether to show an alert popup instead of a toast. + /// A URL that will be opened by the client. + /// The maximum amount of time in seconds that the result of the callback query may be cached client-side. + /// The cancellation token. + protected async Task AnswerCallbackQuery( + string? text = null, + bool showAlert = false, + string? url = null, + int cacheTime = 0, + CancellationToken cancellationToken = default) + => await Container.AnswerCallbackQuery( + text, showAlert, url, cacheTime, cancellationToken); + + + /// /// Gets the type-specific data from the callback query. /// Returns the data string, chat instance, or game short name depending on the callback query type. diff --git a/Telegrator/TypesExtensions.cs b/Telegrator/TypesExtensions.cs index a65ce46..67ea1e1 100644 --- a/Telegrator/TypesExtensions.cs +++ b/Telegrator/TypesExtensions.cs @@ -179,7 +179,74 @@ namespace Telegrator disableNotification, protectContent, messageEffectId, businessConnectionId, allowPaidBroadcast, cancellationToken); + + public static async Task Responce( + this IAbstractHandlerContainer container, + string text, + ParseMode parseMode = ParseMode.None, + ReplyParameters? replyParameters = null, + ReplyMarkup? replyMarkup = null, + LinkPreviewOptions? linkPreviewOptions = null, + int? messageThreadId = null, + IEnumerable? entities = null, + bool disableNotification = false, + bool protectContent = false, + string? messageEffectId = null, + string? businessConnectionId = null, + bool allowPaidBroadcast = false, + CancellationToken cancellationToken = default) + => await container.Client.SendMessage( + container.ActualUpdate.From.Id, text, parseMode, replyParameters, + replyMarkup, linkPreviewOptions, + messageThreadId, entities, + disableNotification, protectContent, + messageEffectId, businessConnectionId, + allowPaidBroadcast, cancellationToken); + + public static async Task EditMessage( + this IAbstractHandlerContainer container, + string text, + ParseMode parseMode = ParseMode.None, + InlineKeyboardMarkup? replyMarkup = null, + IEnumerable? entities = null, + LinkPreviewOptions? linkPreviewOptions = null, + CancellationToken cancellationToken = default) + { + var update = container.ActualUpdate; + return await container.Client.EditMessageText( + chatId: update.Message.Chat.Id, + messageId: update.Message.MessageId, + text: text, + parseMode: parseMode, + replyMarkup: replyMarkup, + entities: entities, + linkPreviewOptions: linkPreviewOptions, + cancellationToken: cancellationToken); + } + + public static async Task AnswerCallbackQuery( + this IAbstractHandlerContainer container, + string? text = null, + bool showAlert = false, + string? url = null, + int cacheTime = 0, + CancellationToken cancellationToken = default) + { + var callbackQueryId = container.ActualUpdate.Id; + + await container.Client.AnswerCallbackQuery( + callbackQueryId: callbackQueryId, + text: text, + showAlert: showAlert, + url: url, + cacheTime: cacheTime, + cancellationToken: cancellationToken); + } + + } + + /// /// Extensions methods for Awaiter Handler Builders