< Summary

Information
Class: TeleFlow.Telegram.CallbackQueryActions
Assembly: TeleFlow.Framework
File(s): /_/src/TeleFlow.Framework/CallbackQueryActions.cs
Line coverage
83%
Covered lines: 68
Uncovered lines: 13
Coverable lines: 81
Total lines: 228
Line coverage: 83.9%
Branch coverage
71%
Covered branches: 10
Total branches: 14
Branch coverage: 71.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
AnswerAsync(...)100%11100%
AnswerAsync(...)100%11100%
AnswerAsync(...)100%11100%
AnswerAsync(...)100%11100%
AnswerCoreAsync()100%11100%
EditTextAsync(...)100%11100%
EditTextAsync(...)100%11100%
EditTextAsync(...)100%11100%
EditTextAsync(...)100%210%
EditTextCoreAsync(...)83.33%6690%
DeleteMessageAsync(...)75%4469.23%
SendEphemeralAsync(...)100%11100%
SendEphemeralAsync(...)100%210%
EditEphemeralTextAsync()100%11100%
SendEphemeralCoreAsync(...)50%2283.33%
ResolveCancellationToken(...)50%22100%

File(s)

/_/src/TeleFlow.Framework/CallbackQueryActions.cs

#LineLine coverage
 1using TeleFlow.Telegram.Formatting;
 2using TeleFlow.Telegram.Internal;
 3using TeleFlow.Telegram.Schema.Abstractions;
 4using TeleFlow.Telegram.Schema.Types;
 5
 6namespace TeleFlow.Telegram;
 7
 8public sealed class CallbackQueryActions
 9{
 10    private readonly CallbackQueryContext _context;
 11
 12    internal CallbackQueryActions(CallbackQueryContext context)
 13    {
 8814        _context = context;
 8815    }
 16
 17    public Task<bool> AnswerAsync(CancellationToken cancellationToken)
 18    {
 119        return AnswerCoreAsync(text: null, showAlert: null, cancellationToken);
 20    }
 21
 22    public Task<bool> AnswerAsync(string? text, CancellationToken cancellationToken)
 23    {
 224        return AnswerCoreAsync(text, showAlert: null, cancellationToken);
 25    }
 26
 27    [System.Diagnostics.CodeAnalysis.SuppressMessage(
 28        "Performance",
 29        "CA1822:Mark members as static",
 30        Justification = "Callback action methods intentionally remain instance methods for a consistent fluent context A
 31    public Task<bool> AnswerAsync(string? text, bool showAlert, CancellationToken cancellationToken)
 32    {
 133        return AnswerCoreAsync(text, showAlert, cancellationToken);
 34    }
 35
 36    public Task<bool> AnswerAsync(
 37        string? text = null,
 38        bool? showAlert = null,
 39        CancellationToken cancellationToken = default)
 40    {
 941        return AnswerCoreAsync(text, showAlert, cancellationToken);
 42    }
 43
 44    private async Task<bool> AnswerCoreAsync(
 45        string? text,
 46        bool? showAlert,
 47        CancellationToken cancellationToken)
 48    {
 1349        var result = await _context.Bot.AnswerCallbackQueryAsync(
 1350            _context.TelegramCallbackQuery.Id,
 1351            text,
 1352            showAlert,
 1353            cancellationToken: ResolveCancellationToken(cancellationToken)).ConfigureAwait(false);
 1154        _context.MarkCallbackQueryAnswered();
 1155        return result;
 1156    }
 57
 58    public Task<MessageBoolean> EditTextAsync(string text, CancellationToken cancellationToken = default)
 59    {
 360        return EditTextCoreAsync(text, replyMarkup: null, parseMode: null, cancellationToken);
 61    }
 62
 63    /// <summary>
 64    /// Edits the callback message with formatted text.
 65    /// The formatted value carries its explicit Telegram parse mode and does not use the client's parse-mode default.
 66    /// </summary>
 67    public Task<MessageBoolean> EditTextAsync(
 68        TelegramFormattedText text,
 69        CancellationToken cancellationToken = default)
 70    {
 271        ArgumentNullException.ThrowIfNull(text);
 272        return EditTextCoreAsync(text.Text, replyMarkup: null, text.ParseMode, cancellationToken);
 73    }
 74
 75    public Task<MessageBoolean> EditTextAsync(
 76        string text,
 77        InlineKeyboardMarkup replyMarkup,
 78        CancellationToken cancellationToken = default)
 79    {
 180        ArgumentNullException.ThrowIfNull(replyMarkup);
 181        return EditTextCoreAsync(text, replyMarkup, parseMode: null, cancellationToken);
 82    }
 83
 84    /// <summary>
 85    /// Edits the callback message with formatted text and an inline keyboard.
 86    /// </summary>
 87    public Task<MessageBoolean> EditTextAsync(
 88        TelegramFormattedText text,
 89        InlineKeyboardMarkup replyMarkup,
 90        CancellationToken cancellationToken = default)
 91    {
 092        ArgumentNullException.ThrowIfNull(text);
 093        ArgumentNullException.ThrowIfNull(replyMarkup);
 094        return EditTextCoreAsync(text.Text, replyMarkup, text.ParseMode, cancellationToken);
 95    }
 96
 97    private Task<MessageBoolean> EditTextCoreAsync(
 98        string text,
 99        InlineKeyboardMarkup? replyMarkup,
 100        TelegramParseMode? parseMode,
 101        CancellationToken cancellationToken)
 102    {
 6103        ArgumentException.ThrowIfNullOrWhiteSpace(text);
 104
 6105        if (CallbackQueryMessageTargetResolver.TryResolve(_context.TelegramCallbackQuery, out var target))
 106        {
 4107            if (target.IsEphemeral)
 108            {
 2109                return EditEphemeralTextAsync(target, text, replyMarkup, parseMode, cancellationToken);
 110            }
 111
 2112            return _context.Bot.EditMessageTextAsync(
 2113                chatId: IntegerString.From(target.ChatId),
 2114                messageId: target.MessageId,
 2115                text: text,
 2116                parseMode: parseMode,
 2117                replyMarkup: replyMarkup,
 2118                cancellationToken: ResolveCancellationToken(cancellationToken));
 119        }
 120
 2121        if (!string.IsNullOrWhiteSpace(_context.TelegramCallbackQuery.InlineMessageId))
 122        {
 2123            return _context.Bot.EditMessageTextAsync(
 2124                inlineMessageId: _context.TelegramCallbackQuery.InlineMessageId,
 2125                text: text,
 2126                parseMode: parseMode,
 2127                replyMarkup: replyMarkup,
 2128                cancellationToken: ResolveCancellationToken(cancellationToken));
 129        }
 130
 0131        throw new InvalidOperationException(
 0132            "The current callback query does not contain a chat message target or an inline message identifier.");
 133    }
 134
 135    public Task<bool> DeleteMessageAsync(CancellationToken cancellationToken = default)
 136    {
 2137        if (!CallbackQueryMessageTargetResolver.TryResolve(_context.TelegramCallbackQuery, out var target))
 138        {
 1139            throw new InvalidOperationException(
 1140                "The current callback query does not contain a deletable chat message target.");
 141        }
 142
 1143        if (target.IsEphemeral)
 144        {
 1145            return _context.Bot.DeleteEphemeralMessageAsync(
 1146                IntegerString.From(target.ChatId),
 1147                EphemeralMessageTargetResolver.ResolveReceiverUserId(_context.TelegramCallbackQuery, target),
 1148                target.EphemeralMessageId!.Value,
 1149                ResolveCancellationToken(cancellationToken));
 150        }
 151
 0152        return _context.Bot.DeleteMessageAsync(
 0153            IntegerString.From(target.ChatId),
 0154            target.MessageId,
 0155            ResolveCancellationToken(cancellationToken));
 156    }
 157
 158    /// <summary>
 159    /// Sends a text message visible only to the user who triggered the callback in a group or supergroup chat.
 160    /// Sending the message does not acknowledge the callback query.
 161    /// </summary>
 162    public Task<EphemeralMessageReference> SendEphemeralAsync(
 163        string text,
 164        CancellationToken cancellationToken = default)
 165    {
 1166        return SendEphemeralCoreAsync(text, inlineKeyboard: null, cancellationToken);
 167    }
 168
 169    /// <summary>
 170    /// Sends a text message with an inline keyboard visible only to the user who triggered the callback in a group or s
 171    /// Sending the message does not acknowledge the callback query.
 172    /// </summary>
 173    public Task<EphemeralMessageReference> SendEphemeralAsync(
 174        string text,
 175        InlineKeyboardMarkup replyMarkup,
 176        CancellationToken cancellationToken = default)
 177    {
 0178        ArgumentNullException.ThrowIfNull(replyMarkup);
 0179        return SendEphemeralCoreAsync(text, replyMarkup, cancellationToken);
 180    }
 181
 182    private async Task<MessageBoolean> EditEphemeralTextAsync(
 183        TelegramMessageTarget target,
 184        string text,
 185        InlineKeyboardMarkup? replyMarkup,
 186        TelegramParseMode? parseMode,
 187        CancellationToken cancellationToken)
 188    {
 2189        var result = await _context.Bot.EditEphemeralMessageTextAsync(
 2190            IntegerString.From(target.ChatId),
 2191            EphemeralMessageTargetResolver.ResolveReceiverUserId(_context.TelegramCallbackQuery, target),
 2192            target.EphemeralMessageId!.Value,
 2193            text,
 2194            parseMode: parseMode,
 2195            replyMarkup: replyMarkup,
 2196            cancellationToken: ResolveCancellationToken(cancellationToken)).ConfigureAwait(false);
 197
 2198        return MessageBoolean.From(result);
 2199    }
 200
 201    private Task<EphemeralMessageReference> SendEphemeralCoreAsync(
 202        string text,
 203        InlineKeyboardMarkup? inlineKeyboard,
 204        CancellationToken cancellationToken)
 205    {
 1206        if (!CallbackQueryMessageTargetResolver.TryResolve(_context.TelegramCallbackQuery, out var target))
 207        {
 0208            throw new InvalidOperationException(
 0209                "The current callback query does not contain a group or supergroup chat target for an ephemeral message.
 210        }
 211
 1212        var ephemeralTarget = EphemeralMessageTargetResolver.ResolveForCallback(
 1213            _context.TelegramCallbackQuery,
 1214            target);
 215
 1216        return _context.Bot.SendEphemeralMessageAsync(
 1217            ephemeralTarget,
 1218            text,
 1219            replyMarkup: inlineKeyboard,
 1220            callbackQueryId: _context.TelegramCallbackQuery.Id,
 1221            cancellationToken: ResolveCancellationToken(cancellationToken));
 222    }
 223
 224    private CancellationToken ResolveCancellationToken(CancellationToken cancellationToken)
 225    {
 21226        return cancellationToken.CanBeCanceled ? cancellationToken : _context.CancellationToken;
 227    }
 228}