Initial commit
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
using System.Diagnostics;
|
||||
using MemeVault.Bot.Telegram.Handlers;
|
||||
using MemeVault.Bot.Telegram.InlineMode;
|
||||
using MemeVault.Bot.Telemetry;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
|
||||
namespace MemeVault.Bot.Telegram;
|
||||
|
||||
/// <summary>
|
||||
/// Центральный диспетчер: получает Update от Telegram и маршрутизирует к нужному хендлеру.
|
||||
/// Попутно фиксирует метрики через <see cref="BotMetrics"/>.
|
||||
/// </summary>
|
||||
public sealed class UpdateDispatcher(
|
||||
MessageHandler messageHandler,
|
||||
InlineQueryHandler inlineQueryHandler,
|
||||
BotMetrics metrics,
|
||||
ILogger<UpdateDispatcher> logger)
|
||||
{
|
||||
public async Task DispatchAsync(Update update, CancellationToken ct)
|
||||
{
|
||||
var updateType = update.Type.ToString();
|
||||
metrics.UpdatesReceived.WithLabels(updateType).Inc();
|
||||
|
||||
var sw = Stopwatch.StartNew();
|
||||
try
|
||||
{
|
||||
await (update.Type switch
|
||||
{
|
||||
UpdateType.Message when update.Message is not null
|
||||
=> messageHandler.HandleAsync(update.Message, ct),
|
||||
|
||||
UpdateType.InlineQuery when update.InlineQuery is not null
|
||||
=> inlineQueryHandler.HandleAsync(update.InlineQuery, ct),
|
||||
|
||||
_ => Task.CompletedTask
|
||||
});
|
||||
|
||||
metrics.UpdatesProcessed.WithLabels(updateType).Inc();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
metrics.UpdateErrors.WithLabels(updateType).Inc();
|
||||
logger.LogError(ex, "Error dispatching update {UpdateId} ({Type})",
|
||||
update.Id, update.Type);
|
||||
}
|
||||
finally
|
||||
{
|
||||
metrics.UpdateDuration.WithLabels(updateType).Observe(sw.Elapsed.TotalSeconds);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user