/
d.alekseev
/
SmartMenuBot
Обзор
Документация
Войти
/
d.alekseev
/
SmartMenuBot
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
docs/final-readme
ConsoleBot/BackgroundTasks/ResetScenarioBackgroundTask.cs
35 строк
1 KB
d.alekseev
feat: добавить механизм фоновых задач и автосброс сценариев по таймауту
26 фев 2026, 09:13
26 фев 2026, 09:13
d397ee8
Код
Авторство
О чём код?
using Telegram.Bot; using SmartMenuBot.TelegramBot; using SmartMenuBot.TelegramBot.Scenarios.Interfaces; namespace SmartMenuBot.BackgroundTasks { public class ResetScenarioBackgroundTask( TimeSpan resetScenarioTimeout, IScenarioContextRepository scenarioRepository, ITelegramBotClient bot) : BackgroundTask(TimeSpan.FromHours(1), nameof(ResetScenarioBackgroundTask)) { protected override async Task Execute(CancellationToken ct) { var contexts = await scenarioRepository.GetContexts(ct); var now = DateTime.UtcNow; foreach (var contextPair in contexts) { var userId = contextPair.Key; var context = contextPair.Value; if (now - context.CreatedAt < resetScenarioTimeout) continue; await scenarioRepository.ResetContext(userId, ct); await bot.SendMessage( userId, $"Сценарий отменен, так как не поступил ответ в течение {resetScenarioTimeout}", replyMarkup: ReplyKeyboards.PostRegistration, cancellationToken: ct); } } } }