-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from teknologi-umum/resilience
Resilience
- Loading branch information
Showing
5 changed files
with
75 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
BotNet.Services/TelegramClient/TelegramBotClientResilienceExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using BotNet.Services.MarkdownV2; | ||
using Telegram.Bot; | ||
using Telegram.Bot.Exceptions; | ||
using Telegram.Bot.Types; | ||
using Telegram.Bot.Types.Enums; | ||
using Telegram.Bot.Types.ReplyMarkups; | ||
|
||
namespace BotNet.Services.TelegramClient { | ||
public static class TelegramBotClientResilienceExtensions { | ||
public static async Task<Message> EditMessageTextAsync( | ||
this ITelegramBotClient telegramBotClient, | ||
ChatId chatId, | ||
int messageId, | ||
string text, | ||
ParseMode[] parseModes, | ||
InlineKeyboardMarkup? replyMarkup = null, | ||
CancellationToken cancellationToken = default | ||
) { | ||
if (parseModes.Length == 0) throw new ArgumentException("At least one parse mode must be provided.", nameof(parseModes)); | ||
|
||
foreach (ParseMode parseMode in parseModes) { | ||
try { | ||
return await telegramBotClient.EditMessageTextAsync( | ||
chatId: chatId, | ||
messageId: messageId, | ||
text: parseMode == ParseMode.MarkdownV2 | ||
? MarkdownV2Sanitizer.Sanitize(text) | ||
: text, | ||
parseMode: parseMode, | ||
replyMarkup: replyMarkup, | ||
cancellationToken: cancellationToken | ||
); | ||
} catch (ApiRequestException) { | ||
continue; | ||
} | ||
} | ||
throw new ApiRequestException("Text could not be parsed."); | ||
} | ||
} | ||
} |