Fixed callback error on "send me another cat" button

This commit is contained in:
mahdium committed 2024-11-05 20:54:56 +03:30
1 parent 0c0ee21049
commit 97b06b7b4e
1 file changed
+5 -3
+5 -3
View File
@@ -12,7 +12,7 @@ namespace CatsOfMastodonBot.Services
{ {
public static async Task HandleStartMessageAsync(Message message, TelegramBotClient _bot, IDocumentCollection<Post> _db, ILogger<MastodonBot>? logger, CallbackQuery callbackQuery = null) public static async Task HandleStartMessageAsync(Message message, TelegramBotClient _bot, IDocumentCollection<Post> _db, ILogger<MastodonBot>? logger, CallbackQuery callbackQuery = null)
{ {
logger?.LogInformation("Start message received"); logger?.LogInformation("Start message received, trigger source: " + (callbackQuery != null ? "Callback" : "Start command"));
// choose all media attachments that are approved // choose all media attachments that are approved
var mediaAttachmentsToSelect = _db.AsQueryable() var mediaAttachmentsToSelect = _db.AsQueryable()
@@ -21,14 +21,16 @@ namespace CatsOfMastodonBot.Services
// select random approved media attachment // select random approved media attachment
var selectedMediaAttachment = mediaAttachmentsToSelect[new Random().Next(mediaAttachmentsToSelect.Count)]; var selectedMediaAttachment = mediaAttachmentsToSelect[new Random().Next(mediaAttachmentsToSelect.Count)];
// send media attachment // send media attachment
await _bot.SendPhoto(callbackQuery.Message.Chat.Id, selectedMediaAttachment.MediaAttachments.FirstOrDefault(m => m.Approved == true).Url, await _bot.SendPhoto(message.Chat.Id, selectedMediaAttachment.MediaAttachments.FirstOrDefault(m => m.Approved == true).Url,
caption: $"Here is your cat!🐈\n" + "<a href=\"" + selectedMediaAttachment.Url + "\">" + $"View on Mastodon " + " </a>", parseMode: ParseMode.Html caption: $"Here is your cat!🐈\n" + "<a href=\"" + selectedMediaAttachment.Url + "\">" + $"View on Mastodon " + " </a>", parseMode: ParseMode.Html
, replyMarkup: new InlineKeyboardMarkup().AddButton(InlineKeyboardButton.WithUrl("Join channel 😺", "https://t.me/catsofmastodon")) , replyMarkup: new InlineKeyboardMarkup().AddButton(InlineKeyboardButton.WithUrl("Join channel 😺", "https://t.me/catsofmastodon"))
.AddNewRow() .AddNewRow()
.AddButton(InlineKeyboardButton.WithCallbackData("Send me another one!", $"new_random"))); .AddButton(InlineKeyboardButton.WithCallbackData("Send me another one!", $"new_random")));
// answer callback query from "send me another cat" button
if (callbackQuery != null) if (callbackQuery != null)
{ {
await _bot.AnswerCallbackQuery(callbackQuery.Id, "Catch your cat!", url: selectedMediaAttachment.Url); await _bot.AnswerCallbackQuery(callbackQuery.Id, "Catch your cat! 😺");
} }
logger?.LogInformation("Random cat sent!"); logger?.LogInformation("Random cat sent!");