diff --git a/Telegrator/Handlers/CallbackQueryHandler.cs b/Telegrator/Handlers/CallbackQueryHandler.cs index 6c2f7d3..9c5ae19 100644 --- a/Telegrator/Handlers/CallbackQueryHandler.cs +++ b/Telegrator/Handlers/CallbackQueryHandler.cs @@ -28,7 +28,20 @@ namespace Telegrator.Handlers /// public abstract class CallbackQueryHandler() : AbstractUpdateHandler(UpdateType.CallbackQuery) { - + /// + /// 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. + /// + protected string TypeData + { + get => Input switch + { + { Data: { } data } => data, + { ChatInstance: { } chatInstance } => chatInstance, + { GameShortName: { } gameShortName } => gameShortName + }; + } + /// /// Sends a response message to the current chat. /// @@ -76,7 +89,6 @@ namespace Telegrator.Handlers /// 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( @@ -106,21 +118,5 @@ namespace Telegrator.Handlers 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. - /// - protected string TypeData - { - get => Input switch - { - { Data: { } data } => data, - { ChatInstance: { } chatInstance } => chatInstance, - { GameShortName: { } gameShortName } => gameShortName - }; - } } } diff --git a/Telegrator/TypesExtensions.cs b/Telegrator/TypesExtensions.cs index 67ea1e1..5755475 100644 --- a/Telegrator/TypesExtensions.cs +++ b/Telegrator/TypesExtensions.cs @@ -179,7 +179,26 @@ namespace Telegrator disableNotification, protectContent, messageEffectId, businessConnectionId, allowPaidBroadcast, cancellationToken); - + + /// + /// Responnces to message that this CallbackQuery was originated from + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// public static async Task Responce( this IAbstractHandlerContainer container, string text, @@ -195,14 +214,32 @@ namespace Telegrator string? businessConnectionId = null, bool allowPaidBroadcast = false, CancellationToken cancellationToken = default) - => await container.Client.SendMessage( - container.ActualUpdate.From.Id, text, parseMode, replyParameters, + { + CallbackQuery query = container.ActualUpdate; + if (query.Message == null) + throw new Exception("Callback origin message not found!"); + + return await container.Client.SendMessage( + query.Message.Chat, text, parseMode, replyParameters, replyMarkup, linkPreviewOptions, messageThreadId, entities, disableNotification, protectContent, messageEffectId, businessConnectionId, allowPaidBroadcast, cancellationToken); + } + /// + /// Edits message text that this CallbackQuery was originated from + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// public static async Task EditMessage( this IAbstractHandlerContainer container, string text, @@ -212,18 +249,37 @@ namespace Telegrator 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); + CallbackQuery query = container.ActualUpdate; + if (query.Message == null) + throw new Exception("Callback origin message not found!"); + + return await container.Client.EditMessageText( + query.Message.Chat, + query.Message.MessageId, + text: text, + parseMode: parseMode, + replyMarkup: replyMarkup, + entities: entities, + linkPreviewOptions: linkPreviewOptions, + cancellationToken: cancellationToken); } - + + /// + /// Use this method to send answers to callback queries sent from inline keyboards. + /// The answer will be displayed to the user as a notification at the top of the chat screen or as an alert + /// + /// + /// Alternatively, the user can be redirected to the specified Game URL. + /// For this option to work, you must first create a game for your bot via @BotFather and accept the terms. + /// Otherwise, you may use links like t.me/your_bot?start=XXXX that open your bot with a parameter. + /// + /// + /// + /// + /// + /// + /// + /// public static async Task AnswerCallbackQuery( this IAbstractHandlerContainer container, string? text = null, @@ -231,22 +287,14 @@ namespace Telegrator string? url = null, int cacheTime = 0, CancellationToken cancellationToken = default) - { - var callbackQueryId = container.ActualUpdate.Id; - - await container.Client.AnswerCallbackQuery( - callbackQueryId: callbackQueryId, + => await container.Client.AnswerCallbackQuery( + callbackQueryId: container.ActualUpdate.Id, text: text, showAlert: showAlert, url: url, cacheTime: cacheTime, cancellationToken: cancellationToken); - } - - } - - /// /// Extensions methods for Awaiter Handler Builders