Fix back button on AddSubscription view

This commit is contained in:
Sergey Nazarov 2024-05-07 10:53:44 +03:00
parent cf167e75c9
commit a69d372362
5 changed files with 55 additions and 14 deletions

View File

@ -27,6 +27,9 @@ public sealed class MessageDispatcherHandler : IMessageDispatcherHandler
case TextMessage tm:
await _bot.SendMessageAsync(tm, cancellationToken);
break;
case AnimationMessage am:
await _bot.SendAnimationAsync(am, cancellationToken);
break;
default:
_logger.LogWarning("Dequeued message has unsupported type: {Type}", typeof(Message).FullName);
break;

View File

@ -2,6 +2,7 @@ using Insight.Localizer;
using Insight.TelegramBot;
using Insight.TelegramBot.Keyboards;
using Insight.TelegramBot.Models;
using Microsoft.Extensions.Logging;
using Nocr.TelegramClient.AppServices.Bots;
using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher;
using Telegram.Bot.Types;
@ -12,18 +13,20 @@ namespace Nocr.TelegramClient.AppServices.Handlers.BaseHandlers;
public abstract class StartHandlerBase
{
private readonly ILogger _logger;
protected IBot Bot { get; }
protected ILocalizer Localizer { get; }
protected IMessageDispatcherQueue MessageQueue { get; }
public StartHandlerBase(ILocalizer localizer, IMessageDispatcherQueue messageQueue, IBot bot)
public StartHandlerBase(ILogger logger, ILocalizer localizer, IMessageDispatcherQueue messageQueue, IBot bot)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
Bot = bot ?? throw new ArgumentNullException(nameof(bot));
Localizer = localizer ?? throw new ArgumentNullException(nameof(localizer));
MessageQueue = messageQueue ?? throw new ArgumentNullException(nameof(messageQueue));
}
public Task Handle(Update update, CancellationToken cancellationToken = default)
public async Task Handle(Update update, CancellationToken cancellationToken = default)
{
long telegramId;
switch (update.Type)
@ -47,11 +50,25 @@ public abstract class StartHandlerBase
if (update.Type == UpdateType.CallbackQuery)
{
return Bot.EditMessageTextAsync(update.CallbackQuery.Message.MessageId, message, cancellationToken);
var messageId = update.CallbackQuery.Message.MessageId;
if (update.CallbackQuery.Message.Type == MessageType.Text)
{
await Bot.EditMessageTextAsync(messageId, message, cancellationToken);
return;
}
var chatId = update.CallbackQuery.Message.Chat.Id;
try
{
await Bot.DeleteMessageAsync(chatId, messageId, cancellationToken);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to delete message {MessageId} in chat {ChatId}", messageId, chatId);
}
}
MessageQueue.Enqueue(message);
return Task.CompletedTask;
}
private IReplyMarkup GetKeyboard()

View File

@ -2,7 +2,9 @@ using Insight.Localizer;
using Insight.TelegramBot;
using Insight.TelegramBot.Handling.Handlers;
using Insight.TelegramBot.Models;
using Microsoft.Extensions.Logging;
using Nocr.TelegramClient.AppServices.Bots;
using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher;
using Telegram.Bot.Types;
using Telegram.Bot.Types.ReplyMarkups;
@ -10,20 +12,27 @@ namespace Nocr.TelegramClient.AppServices.Handlers.CallbackQueries.AddSubscripti
public sealed class AddSubscriptionHandler : IMatchingUpdateHandler<AddSubscriptionMatcher>
{
private readonly ILogger<AddSubscriptionHandler> _logger;
private readonly ILocalizer _localizer;
private readonly IBot _bot;
private readonly IMessageDispatcherQueue _messageDispatcherQueue;
private const string FaqFileId = "CgACAgIAAxkBAAIPQmY5H7c3pi5_4OxdPbpRVdnwEFMxAAIBTAACnxzJSefFRShccLRXNQQ";
public AddSubscriptionHandler(ILocalizer localizer, IBot bot)
public AddSubscriptionHandler(ILogger<AddSubscriptionHandler> logger, ILocalizer localizer, IBot bot, IMessageDispatcherQueue messageDispatcherQueue)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_localizer = localizer ?? throw new ArgumentNullException(nameof(localizer));
_bot = bot ?? throw new ArgumentNullException(nameof(bot));
_messageDispatcherQueue = messageDispatcherQueue ?? throw new ArgumentNullException(nameof(messageDispatcherQueue));
}
public Task Handle(Update update, CancellationToken cancellationToken = default)
public async Task Handle(Update update, CancellationToken cancellationToken = default)
{
var message = new AnimationMessage(update.CallbackQuery.Message.Chat.Id)
var chatId = update.CallbackQuery.Message.Chat.Id;
var messageId = update.CallbackQuery.Message.MessageId;
var message = new AnimationMessage(chatId)
{
InputOnlineFile = new InputFileId(FaqFileId),
Caption = _localizer.Get(nameof(AddSubscriptionHandler), "Text"),
@ -32,6 +41,16 @@ public sealed class AddSubscriptionHandler : IMatchingUpdateHandler<AddSubscript
CallbackData = NocrCallbackData.Start().ToString()
})
};
return _bot.SendAnimationAsync(message, cancellationToken);
try
{
await _bot.DeleteMessageAsync(chatId, messageId, cancellationToken);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to delete message {MessageId} in chat {ChatId}", messageId, chatId);
}
_messageDispatcherQueue.Enqueue(message);
}
}

View File

@ -1,6 +1,7 @@
using Insight.Localizer;
using Insight.TelegramBot;
using Insight.TelegramBot.Handling.Handlers;
using Microsoft.Extensions.Logging;
using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher;
using Nocr.TelegramClient.AppServices.Handlers.BaseHandlers;
@ -8,8 +9,8 @@ namespace Nocr.TelegramClient.AppServices.Handlers.CallbackQueries.Start;
public class StartHandler : StartHandlerBase, IMatchingUpdateHandler<StartMatcher>
{
public StartHandler(ILocalizer localizer, IMessageDispatcherQueue messageQueue, IBot bot)
: base(localizer, messageQueue, bot)
public StartHandler(ILogger<StartHandler> logger, ILocalizer localizer, IMessageDispatcherQueue messageQueue, IBot bot)
: base(logger, localizer, messageQueue, bot)
{
}
}

View File

@ -1,6 +1,7 @@
using Insight.Localizer;
using Insight.TelegramBot;
using Insight.TelegramBot.Handling.Handlers;
using Microsoft.Extensions.Logging;
using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher;
using Nocr.TelegramClient.AppServices.Handlers.BaseHandlers;
@ -9,8 +10,8 @@ namespace Nocr.TelegramClient.AppServices.Handlers.Messages.StartMessage;
public class StartMessageHandler : StartHandlerBase, IMatchingUpdateHandler<StartMessageMatcher>
{
public StartMessageHandler(ILocalizer localizer, IMessageDispatcherQueue messageQueue, IBot bot)
:base(localizer, messageQueue, bot)
public StartMessageHandler(ILogger<StartMessageHandler> logger, ILocalizer localizer, IMessageDispatcherQueue messageQueue, IBot bot)
:base(logger, localizer, messageQueue, bot)
{
}
}