| | | 1 | | using TeleFlow.Telegram.Schema.Abstractions; |
| | | 2 | | using TeleFlow.Telegram.Schema.Types; |
| | | 3 | | |
| | | 4 | | namespace TeleFlow.Telegram; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides explicit client-level conveniences for sending Telegram ephemeral text messages. |
| | | 8 | | /// Framework context helpers use this surface after deriving an ephemeral target from an incoming update. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class TelegramClientEphemeralMessageExtensions |
| | | 11 | | { |
| | | 12 | | public static async Task<EphemeralMessageReference> SendEphemeralMessageAsync( |
| | | 13 | | this ITelegramClient bot, |
| | | 14 | | EphemeralMessageTarget target, |
| | | 15 | | string text, |
| | | 16 | | InlineKeyboardMarkup? replyMarkup = null, |
| | | 17 | | string? callbackQueryId = null, |
| | | 18 | | CancellationToken cancellationToken = default) |
| | | 19 | | { |
| | 5 | 20 | | ArgumentNullException.ThrowIfNull(bot); |
| | 5 | 21 | | ArgumentNullException.ThrowIfNull(target); |
| | 5 | 22 | | ArgumentException.ThrowIfNullOrWhiteSpace(text); |
| | | 23 | | |
| | 5 | 24 | | if (callbackQueryId is not null) |
| | | 25 | | { |
| | 2 | 26 | | ArgumentException.ThrowIfNullOrWhiteSpace(callbackQueryId); |
| | | 27 | | } |
| | | 28 | | |
| | 4 | 29 | | var message = await bot.SendMessageAsync( |
| | 4 | 30 | | target.ChatId, |
| | 4 | 31 | | text, |
| | 4 | 32 | | receiverUserId: target.ReceiverUserId, |
| | 4 | 33 | | callbackQueryId: callbackQueryId, |
| | 4 | 34 | | replyMarkup: replyMarkup is null ? null : ReplyMarkup.From(replyMarkup), |
| | 4 | 35 | | cancellationToken: cancellationToken).ConfigureAwait(false); |
| | | 36 | | |
| | 4 | 37 | | if (message.EphemeralMessageId is not long ephemeralMessageId) |
| | | 38 | | { |
| | 1 | 39 | | throw new InvalidOperationException( |
| | 1 | 40 | | "Telegram returned an ephemeral message without an ephemeral_message_id."); |
| | | 41 | | } |
| | | 42 | | |
| | 3 | 43 | | return new EphemeralMessageReference(bot, target, ephemeralMessageId, message); |
| | 3 | 44 | | } |
| | | 45 | | } |