diff --git a/BotService.cs b/BotService.cs index 2f3d6d1..d275507 100644 --- a/BotService.cs +++ b/BotService.cs @@ -28,26 +28,42 @@ namespace ZaoBot public void Run() { var token = config.RootElement.GetProperty("token").GetString(); - shouldRestart = false; - botClient = new TelegramBotClient(token); openhab = new OpenHab.OpenHabClient(config.RootElement.GetProperty("openhab_url").GetString()); - var me = botClient.GetMeAsync().Result; - Console.WriteLine($"Hello, World! I am user {me.Id} and my name is {me.FirstName}."); - botClient.OnMessage += OnBotMessage; - botClient.StartReceiving(); - while (!shouldRestart) + while (true) { - Thread.Sleep(1000); + 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 + ); + while (!shouldRestart) + { + Thread.Sleep(1000); + } } } - private async void OnBotMessage(object sender, MessageEventArgs e) + private Task OnPollingError(ITelegramBotClient bot, Exception exception, CancellationToken cts) { + Console.WriteLine("Error on polling: " + exception.Message); + Console.WriteLine("Will restart"); + return Task.CompletedTask; + } + + async Task OnBotUpdateAsync(ITelegramBotClient bot, Telegram.Bot.Types.Update updateInfo, CancellationToken cts) + { + if (updateInfo.Message == null) return; try { - if (e.Message.Text != null) + if (updateInfo.Message.Text != null) { - var id = e.Message.Chat.Id; + var id = updateInfo.Message.Chat.Id; IUserState state = null; if (userStates.ContainsKey(id)) { @@ -57,15 +73,15 @@ namespace ZaoBot { state = new MainMenuUserState(botClient, config, openhab); } - state = await state.ProcessMessage(state, e); + state = await state.ProcessMessage(state, updateInfo); if (state == null) { await botClient.SendTextMessageAsync( - chatId: e.Message.Chat, + chatId: updateInfo.Message.Chat, text: "Я запутался, давайте сначала" ); state = new MainMenuUserState(botClient, config, openhab); - state = await state.ProcessMessage(state, e); + state = await state.ProcessMessage(state, updateInfo); } userStates[id] = state; } diff --git a/CamerasMenuUserState.cs b/CamerasMenuUserState.cs index 3cda396..c098b87 100644 --- a/CamerasMenuUserState.cs +++ b/CamerasMenuUserState.cs @@ -6,8 +6,6 @@ using System.Text; using System.Text.Json; using System.Threading.Tasks; using Telegram.Bot; -using Telegram.Bot.Args; -using Telegram.Bot.Types.InputFiles; using Telegram.Bot.Types.ReplyMarkups; using ZaoBot.OpenHab; @@ -45,7 +43,7 @@ namespace ZaoBot } } - public async Task ProcessMessage(IUserState previous, MessageEventArgs e) + public async Task ProcessMessage(IUserState previous, Telegram.Bot.Types.Update e) { if (e.Message.Text == CMD_BACK) { @@ -78,8 +76,7 @@ namespace ZaoBot buttons[cameras.Count] = new KeyboardButton[] { CMD_BACK }; var replyKeyboardMarkup = new ReplyKeyboardMarkup( - buttons, - resizeKeyboard: true + buttons ); await botClient.SendTextMessageAsync( @@ -102,7 +99,7 @@ namespace ZaoBot { await botClient.SendDocumentAsync( chatId: chat, - document: new InputOnlineFile(content: stream, fileName: cam.name + ".jpg") + document: Telegram.Bot.Types.InputFile.FromStream(stream: stream, fileName: cam.name + ".jpg") ); } } diff --git a/IUserState.cs b/IUserState.cs index 07143e3..4af9ab4 100644 --- a/IUserState.cs +++ b/IUserState.cs @@ -8,6 +8,6 @@ namespace ZaoBot { interface IUserState { - Task ProcessMessage(IUserState previous, MessageEventArgs e); + Task ProcessMessage(IUserState previous, Telegram.Bot.Types.Update e); } } diff --git a/MainMenuUserState.cs b/MainMenuUserState.cs index 1cdf89d..9845fee 100644 --- a/MainMenuUserState.cs +++ b/MainMenuUserState.cs @@ -31,7 +31,7 @@ namespace ZaoBot this.openhab = openhab; } - public async Task ProcessMessage(IUserState previous, MessageEventArgs e) + public async Task ProcessMessage(IUserState previous, Telegram.Bot.Types.Update e) { var response = ""; if (e != null) @@ -89,8 +89,7 @@ namespace ZaoBot new KeyboardButton[] { CMD_LEFT_GATE, CMD_RIGHT_GATE }, new KeyboardButton[] { CMD_STATUS }, new KeyboardButton[] { CMD_CAMERAS }, - }, - resizeKeyboard: true + } ); await botClient.SendTextMessageAsync( diff --git a/StatusMenuUserState.cs b/StatusMenuUserState.cs index 6b4dd3a..656c06a 100644 --- a/StatusMenuUserState.cs +++ b/StatusMenuUserState.cs @@ -27,7 +27,7 @@ namespace ZaoBot this.openhab = openhab; } - public async Task ProcessMessage(IUserState previous, MessageEventArgs e) + public async Task ProcessMessage(IUserState previous, Telegram.Bot.Types.Update e) { var response = ""; if (previous != this) @@ -59,8 +59,7 @@ namespace ZaoBot new KeyboardButton[][] { new KeyboardButton[] { CMD_BACK }, - }, - resizeKeyboard: true + } ); await botClient.SendTextMessageAsync( diff --git a/ZaoBot.csproj b/ZaoBot.csproj index 875d691..0b90841 100644 --- a/ZaoBot.csproj +++ b/ZaoBot.csproj @@ -6,7 +6,7 @@ - +