| | | 1 | | using TeleFlow.Telegram.Formatting; |
| | | 2 | | using TeleFlow.Telegram.Internal; |
| | | 3 | | using TeleFlow.Telegram.Schema.Abstractions; |
| | | 4 | | using TeleFlow.Telegram.Schema.Types; |
| | | 5 | | |
| | | 6 | | namespace TeleFlow.Telegram; |
| | | 7 | | |
| | | 8 | | public sealed class CallbackQueryActions |
| | | 9 | | { |
| | | 10 | | private readonly CallbackQueryContext _context; |
| | | 11 | | |
| | | 12 | | internal CallbackQueryActions(CallbackQueryContext context) |
| | | 13 | | { |
| | 88 | 14 | | _context = context; |
| | 88 | 15 | | } |
| | | 16 | | |
| | | 17 | | public Task<bool> AnswerAsync(CancellationToken cancellationToken) |
| | | 18 | | { |
| | 1 | 19 | | return AnswerCoreAsync(text: null, showAlert: null, cancellationToken); |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | public Task<bool> AnswerAsync(string? text, CancellationToken cancellationToken) |
| | | 23 | | { |
| | 2 | 24 | | 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 | | { |
| | 1 | 33 | | 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 | | { |
| | 9 | 41 | | return AnswerCoreAsync(text, showAlert, cancellationToken); |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | private async Task<bool> AnswerCoreAsync( |
| | | 45 | | string? text, |
| | | 46 | | bool? showAlert, |
| | | 47 | | CancellationToken cancellationToken) |
| | | 48 | | { |
| | 13 | 49 | | var result = await _context.Bot.AnswerCallbackQueryAsync( |
| | 13 | 50 | | _context.TelegramCallbackQuery.Id, |
| | 13 | 51 | | text, |
| | 13 | 52 | | showAlert, |
| | 13 | 53 | | cancellationToken: ResolveCancellationToken(cancellationToken)).ConfigureAwait(false); |
| | 11 | 54 | | _context.MarkCallbackQueryAnswered(); |
| | 11 | 55 | | return result; |
| | 11 | 56 | | } |
| | | 57 | | |
| | | 58 | | public Task<MessageBoolean> EditTextAsync(string text, CancellationToken cancellationToken = default) |
| | | 59 | | { |
| | 3 | 60 | | 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 | | { |
| | 2 | 71 | | ArgumentNullException.ThrowIfNull(text); |
| | 2 | 72 | | 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 | | { |
| | 1 | 80 | | ArgumentNullException.ThrowIfNull(replyMarkup); |
| | 1 | 81 | | 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 | | { |
| | 0 | 92 | | ArgumentNullException.ThrowIfNull(text); |
| | 0 | 93 | | ArgumentNullException.ThrowIfNull(replyMarkup); |
| | 0 | 94 | | 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 | | { |
| | 6 | 103 | | ArgumentException.ThrowIfNullOrWhiteSpace(text); |
| | | 104 | | |
| | 6 | 105 | | if (CallbackQueryMessageTargetResolver.TryResolve(_context.TelegramCallbackQuery, out var target)) |
| | | 106 | | { |
| | 4 | 107 | | if (target.IsEphemeral) |
| | | 108 | | { |
| | 2 | 109 | | return EditEphemeralTextAsync(target, text, replyMarkup, parseMode, cancellationToken); |
| | | 110 | | } |
| | | 111 | | |
| | 2 | 112 | | return _context.Bot.EditMessageTextAsync( |
| | 2 | 113 | | chatId: IntegerString.From(target.ChatId), |
| | 2 | 114 | | messageId: target.MessageId, |
| | 2 | 115 | | text: text, |
| | 2 | 116 | | parseMode: parseMode, |
| | 2 | 117 | | replyMarkup: replyMarkup, |
| | 2 | 118 | | cancellationToken: ResolveCancellationToken(cancellationToken)); |
| | | 119 | | } |
| | | 120 | | |
| | 2 | 121 | | if (!string.IsNullOrWhiteSpace(_context.TelegramCallbackQuery.InlineMessageId)) |
| | | 122 | | { |
| | 2 | 123 | | return _context.Bot.EditMessageTextAsync( |
| | 2 | 124 | | inlineMessageId: _context.TelegramCallbackQuery.InlineMessageId, |
| | 2 | 125 | | text: text, |
| | 2 | 126 | | parseMode: parseMode, |
| | 2 | 127 | | replyMarkup: replyMarkup, |
| | 2 | 128 | | cancellationToken: ResolveCancellationToken(cancellationToken)); |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | throw new InvalidOperationException( |
| | 0 | 132 | | "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 | | { |
| | 2 | 137 | | if (!CallbackQueryMessageTargetResolver.TryResolve(_context.TelegramCallbackQuery, out var target)) |
| | | 138 | | { |
| | 1 | 139 | | throw new InvalidOperationException( |
| | 1 | 140 | | "The current callback query does not contain a deletable chat message target."); |
| | | 141 | | } |
| | | 142 | | |
| | 1 | 143 | | if (target.IsEphemeral) |
| | | 144 | | { |
| | 1 | 145 | | return _context.Bot.DeleteEphemeralMessageAsync( |
| | 1 | 146 | | IntegerString.From(target.ChatId), |
| | 1 | 147 | | EphemeralMessageTargetResolver.ResolveReceiverUserId(_context.TelegramCallbackQuery, target), |
| | 1 | 148 | | target.EphemeralMessageId!.Value, |
| | 1 | 149 | | ResolveCancellationToken(cancellationToken)); |
| | | 150 | | } |
| | | 151 | | |
| | 0 | 152 | | return _context.Bot.DeleteMessageAsync( |
| | 0 | 153 | | IntegerString.From(target.ChatId), |
| | 0 | 154 | | target.MessageId, |
| | 0 | 155 | | 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 | | { |
| | 1 | 166 | | 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 | | { |
| | 0 | 178 | | ArgumentNullException.ThrowIfNull(replyMarkup); |
| | 0 | 179 | | 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 | | { |
| | 2 | 189 | | var result = await _context.Bot.EditEphemeralMessageTextAsync( |
| | 2 | 190 | | IntegerString.From(target.ChatId), |
| | 2 | 191 | | EphemeralMessageTargetResolver.ResolveReceiverUserId(_context.TelegramCallbackQuery, target), |
| | 2 | 192 | | target.EphemeralMessageId!.Value, |
| | 2 | 193 | | text, |
| | 2 | 194 | | parseMode: parseMode, |
| | 2 | 195 | | replyMarkup: replyMarkup, |
| | 2 | 196 | | cancellationToken: ResolveCancellationToken(cancellationToken)).ConfigureAwait(false); |
| | | 197 | | |
| | 2 | 198 | | return MessageBoolean.From(result); |
| | 2 | 199 | | } |
| | | 200 | | |
| | | 201 | | private Task<EphemeralMessageReference> SendEphemeralCoreAsync( |
| | | 202 | | string text, |
| | | 203 | | InlineKeyboardMarkup? inlineKeyboard, |
| | | 204 | | CancellationToken cancellationToken) |
| | | 205 | | { |
| | 1 | 206 | | if (!CallbackQueryMessageTargetResolver.TryResolve(_context.TelegramCallbackQuery, out var target)) |
| | | 207 | | { |
| | 0 | 208 | | throw new InvalidOperationException( |
| | 0 | 209 | | "The current callback query does not contain a group or supergroup chat target for an ephemeral message. |
| | | 210 | | } |
| | | 211 | | |
| | 1 | 212 | | var ephemeralTarget = EphemeralMessageTargetResolver.ResolveForCallback( |
| | 1 | 213 | | _context.TelegramCallbackQuery, |
| | 1 | 214 | | target); |
| | | 215 | | |
| | 1 | 216 | | return _context.Bot.SendEphemeralMessageAsync( |
| | 1 | 217 | | ephemeralTarget, |
| | 1 | 218 | | text, |
| | 1 | 219 | | replyMarkup: inlineKeyboard, |
| | 1 | 220 | | callbackQueryId: _context.TelegramCallbackQuery.Id, |
| | 1 | 221 | | cancellationToken: ResolveCancellationToken(cancellationToken)); |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | private CancellationToken ResolveCancellationToken(CancellationToken cancellationToken) |
| | | 225 | | { |
| | 21 | 226 | | return cancellationToken.CanBeCanceled ? cancellationToken : _context.CancellationToken; |
| | | 227 | | } |
| | | 228 | | } |