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;
namespace Telegrator.Handlers
{
///
/// Attribute that marks a handler to process callback query updates.
/// This handler will be triggered when users interact with inline keyboards or other callback mechanisms.
///
///
public sealed class CallbackQueryHandlerAttribute(int importance = 0) : UpdateHandlerAttribute(UpdateType.CallbackQuery, importance)
{
///
/// Always returns true, allowing any callback query update to pass through this filter.
///
/// The filter execution context (unused).
/// Always returns true to allow any callback query update.
public override bool CanPass(FilterExecutionContext context) => context.Input is { CallbackQuery: { } };
}
///
/// Abstract base class for handlers that process callback query updates.
/// Provides a foundation for creating handlers that respond to user interactions with inline keyboards.
///
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.
///
/// 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,
int? directMessageTopicId = null,
SuggestedPostParameters? suggestedPostParameters = null,
CancellationToken cancellationToken = default)
=> await Container.Responce(
text, parseMode, replyParameters,
replyMarkup, linkPreviewOptions,
messageThreadId, entities,
disableNotification, protectContent,
messageEffectId, businessConnectionId,
allowPaidBroadcast, directMessageTopicId,
suggestedPostParameters, 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 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 Answer(
string? text = null,
bool showAlert = false,
string? url = null,
int cacheTime = 0,
CancellationToken cancellationToken = default)
=> await Container.AnswerCallbackQuery(
text, showAlert, url, cacheTime, cancellationToken);
}
}