From d641de96898ea490222d1f976b491d2197bad1e5 Mon Sep 17 00:00:00 2001 From: Denis Denisov Date: Sat, 20 Jan 2024 18:37:52 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B0=D0=BB=20=D0=B2=D1=81=D1=91=20=D0=BD=D0=B0=20DI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BotService.cs | 36 ++++++++++++++++++++---------------- CamerasMenuUserState.cs | 14 +++++++++----- MainMenuUserState.cs | 16 +++++++++------- Program.cs | 23 ++++++++++++++++++++--- StatusMenuUserState.cs | 12 ++++++++---- 5 files changed, 66 insertions(+), 35 deletions(-) diff --git a/BotService.cs b/BotService.cs index 3169881..6070c88 100644 --- a/BotService.cs +++ b/BotService.cs @@ -1,13 +1,10 @@ -using System; +using Microsoft.Extensions.DependencyInjection; +using System; using System.Collections.Generic; -using System.Net.Http; -using System.Text; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Telegram.Bot; -using Telegram.Bot.Args; -using Telegram.Bot.Types.ReplyMarkups; using ZaoBot.Config; namespace ZaoBot @@ -18,42 +15,49 @@ namespace ZaoBot static ITelegramBotClient botClient; Dictionary userStates = new Dictionary(); private OpenHab.OpenHabClient openhab; + private IServiceProvider services; bool shouldRestart = false; - public BotService(IConfig config, OpenHab.OpenHabClient openhab) + public BotService(IServiceProvider services, IConfig config, OpenHab.OpenHabClient openhab) { + this.services = services; this.config = config.Data; this.openhab = openhab; } public void Run() { - var token = config.RootElement.GetProperty("token").GetString(); - while (true) + shouldRestart = false; + + var botClient = services.GetRequiredService(); + + using CancellationTokenSource cts = new CancellationTokenSource(); + try { - shouldRestart = false; - botClient = new TelegramBotClient(token); - - using CancellationTokenSource cts = new CancellationTokenSource(); - var me = botClient.GetMeAsync().Result; Console.WriteLine($"Hello, World! I am user {me.Id} and my name is {me.FirstName}."); botClient.StartReceiving( updateHandler: OnBotUpdateAsync, - pollingErrorHandler: OnPollingError + pollingErrorHandler: OnPollingError, + cancellationToken: cts.Token ); while (!shouldRestart) { Thread.Sleep(1000); } } + finally + { + cts.Cancel(); + } } private Task OnPollingError(ITelegramBotClient bot, Exception exception, CancellationToken cts) { Console.WriteLine("Error on polling: " + exception.Message); Console.WriteLine("Will restart"); + shouldRestart = true; return Task.CompletedTask; } @@ -72,7 +76,7 @@ namespace ZaoBot } if (state == null) { - state = new MainMenuUserState(botClient, config, openhab); + state = services.GetService(); } state = await state.ProcessMessage(state, updateInfo); if (state == null) @@ -81,7 +85,7 @@ namespace ZaoBot chatId: updateInfo.Message.Chat, text: "Я запутался, давайте сначала" ); - state = new MainMenuUserState(botClient, config, openhab); + state = services.GetService(); state = await state.ProcessMessage(state, updateInfo); } userStates[id] = state; diff --git a/CamerasMenuUserState.cs b/CamerasMenuUserState.cs index c098b87..c5e0ce5 100644 --- a/CamerasMenuUserState.cs +++ b/CamerasMenuUserState.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.DependencyInjection; +using System; using System.Collections.Generic; using System.IO; using System.Net; @@ -7,6 +8,7 @@ using System.Text.Json; using System.Threading.Tasks; using Telegram.Bot; using Telegram.Bot.Types.ReplyMarkups; +using ZaoBot.Config; using ZaoBot.OpenHab; namespace ZaoBot @@ -25,14 +27,16 @@ namespace ZaoBot private JsonDocument config; List cameras; OpenHabClient openhab; + IServiceProvider services; - public CamerasMenuUserState(ITelegramBotClient botClient, JsonDocument config, OpenHabClient openhab) + public CamerasMenuUserState(IServiceProvider services, ITelegramBotClient botClient, IConfig config, OpenHabClient openhab) { this.botClient = botClient; - this.config = config; + this.config = config.Data; this.openhab = openhab; + this.services = services; cameras = new List(); - foreach (var element in config.RootElement.GetProperty("cameras").EnumerateObject()) + foreach (var element in this.config.RootElement.GetProperty("cameras").EnumerateObject()) { var cam = new CameraConfigData() { @@ -47,7 +51,7 @@ namespace ZaoBot { if (e.Message.Text == CMD_BACK) { - var newState = new MainMenuUserState(botClient, config, openhab); + var newState = services.GetRequiredService(); await newState.ProcessMessage(this, e); return newState; } diff --git a/MainMenuUserState.cs b/MainMenuUserState.cs index 9845fee..972c1aa 100644 --- a/MainMenuUserState.cs +++ b/MainMenuUserState.cs @@ -1,12 +1,12 @@ -using System; +using Microsoft.Extensions.DependencyInjection; +using System; using System.Collections.Generic; using System.Net.Http; -using System.Text; using System.Text.Json; using System.Threading.Tasks; using Telegram.Bot; -using Telegram.Bot.Args; using Telegram.Bot.Types.ReplyMarkups; +using ZaoBot.Config; using ZaoBot.OpenHab; namespace ZaoBot @@ -23,11 +23,13 @@ namespace ZaoBot private JsonDocument config; HttpClient http = new HttpClient(); OpenHabClient openhab; + IServiceProvider services; - public MainMenuUserState(ITelegramBotClient botClient, JsonDocument config, OpenHabClient openhab) + public MainMenuUserState(IServiceProvider services, ITelegramBotClient botClient, IConfig config, OpenHabClient openhab) { + this.services = services; this.botClient = botClient; - this.config = config; + this.config = config.Data; this.openhab = openhab; } @@ -61,13 +63,13 @@ namespace ZaoBot } else if (e.Message.Text == CMD_STATUS) { - var newState = new StatusMenuUserState(botClient, config, openhab); + var newState = services.GetRequiredService(); await newState.ProcessMessage(this, e); return newState; } else if (e.Message.Text == CMD_CAMERAS) { - var newState = new CamerasMenuUserState(botClient, config, openhab); + var newState = services.GetRequiredService(); await newState.ProcessMessage(this, e); return newState; } diff --git a/Program.cs b/Program.cs index 5653c84..73fcea6 100644 --- a/Program.cs +++ b/Program.cs @@ -5,6 +5,7 @@ using System.IO; using System.Reflection; using System.Text.Json; using System.Threading; +using Telegram.Bot; using ZaoBot.Config; using ZaoBot.OpenHab; @@ -23,9 +24,16 @@ namespace ZaoBot config = LoadConfig(); services = new ServiceCollection(); services.AddSingleton(CreateConfig); - services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + + services.AddScoped(); + services.AddScoped(p => p); + services.AddScoped(CreateTelegramBot); + + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); using (var provider = services.BuildServiceProvider()) { var receiver = provider.GetRequiredService(); @@ -35,8 +43,11 @@ namespace ZaoBot { try { - bot = provider.GetRequiredService(); - bot.Run(); + using (var scope = provider.CreateScope()) + { + bot = scope.ServiceProvider.GetRequiredService(); + bot.Run(); + } } catch (Exception e) { @@ -49,6 +60,12 @@ namespace ZaoBot } } + private static TelegramBotClient CreateTelegramBot(IServiceProvider arg) + { + var token = config.RootElement.GetProperty("token").GetString(); + return new TelegramBotClient(token); + } + private static Config.Config CreateConfig(IServiceProvider arg) { return new Config.Config(config); diff --git a/StatusMenuUserState.cs b/StatusMenuUserState.cs index 656c06a..fa65782 100644 --- a/StatusMenuUserState.cs +++ b/StatusMenuUserState.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.DependencyInjection; +using System; using System.Collections.Generic; using System.Net.Http; using System.Text; @@ -7,6 +8,7 @@ using System.Threading.Tasks; using Telegram.Bot; using Telegram.Bot.Args; using Telegram.Bot.Types.ReplyMarkups; +using ZaoBot.Config; using ZaoBot.OpenHab; namespace ZaoBot @@ -19,12 +21,14 @@ namespace ZaoBot private JsonDocument config; HttpClient http = new HttpClient(); OpenHabClient openhab; + IServiceProvider services; - public StatusMenuUserState(ITelegramBotClient botClient, JsonDocument config, OpenHabClient openhab) + public StatusMenuUserState(IServiceProvider services, ITelegramBotClient botClient, IConfig config, OpenHabClient openhab) { this.botClient = botClient; - this.config = config; + this.config = config.Data; this.openhab = openhab; + this.services = services; } public async Task ProcessMessage(IUserState previous, Telegram.Bot.Types.Update e) @@ -47,7 +51,7 @@ namespace ZaoBot } else if (e.Message.Text == CMD_BACK) { - var newState = new MainMenuUserState(botClient, config, openhab); + var newState = services.GetRequiredService(); await newState.ProcessMessage(this, e); return newState; }