Skip to content

Commit d4c73c0

Browse files
committed
Refactor deletion logic and add new properties/methods
Abstracted message deletion into a new Delete() method in CafeController.cs. Added Client property to BotApp.cs and BotControllerBase.cs. Updated BotControllerBase.cs to include new namespaces and a safe Delete() method. Introduced TryGetMessageId extension method in TelegramUpdateExtensions.cs.
1 parent 717f49e commit d4c73c0

4 files changed

Lines changed: 51 additions & 1 deletion

File tree

Sources/TelegramBot.ConsoleTest/Controllers/CafeController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public async Task<IActionResult> HandleBurgersAsync(int burgers, bool drink)
4949

5050
await telegramBotClient.SendTextMessageAsync(adminId, orderText);
5151
SetValue("customerId", User.Id);
52-
await telegramBotClient.DeleteMessageAsync(User.Id, Update.CallbackQuery!.Message!.MessageId);
52+
Delete();
5353
return Text(text);
5454
}
5555

Sources/TelegramBot/BotApp.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ await authorizationHandler
250250
BotControllerBase controller = (BotControllerBase)ActivatorUtilities.CreateInstance(_serviceProvider, method.DeclaringType!);
251251
controller.Update = update;
252252
controller.User = user;
253+
controller.Client = _client;
253254
if (_serviceProvider.GetService<IKeyValueProvider>() is IKeyValueProvider keyValueProvider)
254255
{
255256
controller.KeyValueProvider = keyValueProvider;

Sources/TelegramBot/Controllers/BotControllerBase.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Linq;
3+
using Telegram.Bot;
34
using Telegram.Bot.Types;
5+
using TelegramBot.Extensions;
46
using TelegramBot.Abstractions;
57
using TelegramBot.ActionResults;
68
using Telegram.Bot.Types.ReplyMarkups;
@@ -24,6 +26,11 @@ public abstract class BotControllerBase
2426
/// </summary>
2527
public Update Update { get; internal set; } = null!;
2628

29+
/// <summary>
30+
/// <see cref="ITelegramBotClient"/> instance.
31+
/// </summary>
32+
public ITelegramBotClient Client { get; internal set; } = null!;
33+
2734
/// <summary>
2835
/// Sends a text message to the sender.
2936
/// </summary>
@@ -155,5 +162,24 @@ public TValue GetValue<TValue>(string key)
155162
string json = GetValue(key);
156163
return string.IsNullOrWhiteSpace(json) ? default! : System.Text.Json.JsonSerializer.Deserialize<TValue>(json)!;
157164
}
165+
166+
/// <summary>
167+
/// Deletes message from the current update, or does nothing if the message identifier is not found.
168+
/// <br/>
169+
/// Note: this method does not throw exceptions when the message ID is not found, access is denied, message is already deleted etc.
170+
/// </summary>
171+
public void Delete()
172+
{
173+
bool hasMessageId = Update.TryGetMessageId(out int messageId);
174+
if (!hasMessageId)
175+
{
176+
return;
177+
}
178+
try
179+
{
180+
Client.DeleteMessageAsync(User.Id, messageId);
181+
}
182+
catch (Exception) { }
183+
}
158184
}
159185
}

Sources/TelegramBot/Extensions/TelegramUpdateExtensions.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,28 @@ public static bool TryGetUser(this Update update, out User user)
5757
user = null!;
5858
return false;
5959
}
60+
61+
/// <summary>
62+
/// Tries to get the message ID of the update.
63+
/// </summary>
64+
/// <param name="update">Telegram update.</param>
65+
/// <param name="messageId">Message identifier.</param>
66+
/// <returns>If the message identifier is not 0, returns true; otherwise, false.</returns>
67+
public static bool TryGetMessageId(this Update update, out int messageId)
68+
{
69+
if (update.Message != null)
70+
{
71+
messageId = update.Message.MessageId;
72+
return true;
73+
}
74+
else if (update.CallbackQuery != null && update.CallbackQuery.Message != null)
75+
{
76+
messageId = update.CallbackQuery.Message.MessageId;
77+
return true;
78+
}
79+
80+
messageId = 0;
81+
return false;
82+
}
6083
}
6184
}

0 commit comments

Comments
 (0)