From 23b6ae390e126d55a9c043e1ef573b425f3f5708 Mon Sep 17 00:00:00 2001 From: Denis Denisov Date: Tue, 4 Jan 2022 15:54:15 +0300 Subject: [PATCH] Added notification service --- BotService.cs | 3 -- NotificationReceiver.cs | 86 +++++++++++++++++++++++++++++++++++++++++ Program.cs | 4 ++ 3 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 NotificationReceiver.cs diff --git a/BotService.cs b/BotService.cs index 2f01b88..548a1c0 100644 --- a/BotService.cs +++ b/BotService.cs @@ -30,7 +30,6 @@ namespace ZaoBot public void Run() { - var token = config.RootElement.GetProperty("token").GetString(); shouldRestart = false; botClient = new TelegramBotClient(token); @@ -44,8 +43,6 @@ namespace ZaoBot } } - - private async void OnBotMessage(object sender, MessageEventArgs e) { try diff --git a/NotificationReceiver.cs b/NotificationReceiver.cs new file mode 100644 index 0000000..7505d36 --- /dev/null +++ b/NotificationReceiver.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.Net; +using System.Text; +using System.Text.Json; +using System.Threading; +using Telegram.Bot; + +namespace ZaoBot +{ + class NotificationReceiver + { + private JsonDocument config; + + public NotificationReceiver(JsonDocument config) + { + this.config = config; + } + + public void Run() + { + while (true) + { + try + { + var port = config.RootElement.GetProperty("notify_listen_port").GetInt32(); + var listener = new HttpListener(); + listener.Prefixes.Add("http://+:" + port + "/"); + listener.Start(); + Console.WriteLine("NotificationReceiver listening on port " + port); + ProcessRequests(listener); + listener.Stop(); + } + catch (Exception e) + { + Console.WriteLine("NotificationReceiver exception: " + e.Message + "\n" + e.StackTrace); + Thread.Sleep(1000); + } + } + } + + private void ProcessRequests(HttpListener listener) + { + while (true) + { + var context = listener.GetContext(); + HttpListenerRequest request = context.Request; + var message = request.QueryString.Get("message"); + if (string.IsNullOrEmpty(message)) + { + RespondWith(context, "FAIL"); + } + else + { + SendNotification(message); + RespondWith(context, "OK"); + } + } + } + + private async void SendNotification(string message) + { + var token = config.RootElement.GetProperty("token").GetString(); + var botClient = new TelegramBotClient(token); + foreach(var element in config.RootElement.GetProperty("notify_user_ids").EnumerateArray()) + { + await botClient.SendTextMessageAsync( + chatId: element.GetString(), + text: message + ); + } + } + + private void RespondWith(HttpListenerContext context, string v) + { + + HttpListenerResponse response = context.Response; + string responseString = v; + byte[] buffer = Encoding.UTF8.GetBytes(responseString); + response.ContentLength64 = buffer.Length; + System.IO.Stream output = response.OutputStream; + output.Write(buffer, 0, buffer.Length); + output.Close(); + } + } +} diff --git a/Program.cs b/Program.cs index cead281..d5f59e5 100644 --- a/Program.cs +++ b/Program.cs @@ -3,6 +3,7 @@ using System.Globalization; using System.IO; using System.Reflection; using System.Text.Json; +using System.Threading; namespace ZaoBot { @@ -15,6 +16,9 @@ namespace ZaoBot { CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; config = LoadConfig(); + var receiver = new NotificationReceiver(config); + var notificationThread = new Thread(new ThreadStart(receiver.Run)); + notificationThread.Start(); while (true) { try