Added notification service
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user