From 84842aed3c42c38726bc1a423a0a02d9d93c13ba Mon Sep 17 00:00:00 2001 From: glax Date: Thu, 15 Jun 2023 18:50:02 +0200 Subject: [PATCH] Added connector NotificationManager LunaSea --- Tranga/NotificationManager.cs | 2 +- Tranga/NotificationManagers/LunaSea.cs | 43 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 Tranga/NotificationManagers/LunaSea.cs diff --git a/Tranga/NotificationManager.cs b/Tranga/NotificationManager.cs index 5030288..6488d63 100644 --- a/Tranga/NotificationManager.cs +++ b/Tranga/NotificationManager.cs @@ -16,7 +16,7 @@ public abstract class NotificationManager this.logger = logger; } - public enum NotificationManagerType : byte { Gotify = 0 } + public enum NotificationManagerType : byte { Gotify = 0, LunaSea = 1 } public abstract void SendNotification(string title, string notificationText); diff --git a/Tranga/NotificationManagers/LunaSea.cs b/Tranga/NotificationManagers/LunaSea.cs new file mode 100644 index 0000000..3ebc165 --- /dev/null +++ b/Tranga/NotificationManagers/LunaSea.cs @@ -0,0 +1,43 @@ +using System.Text; +using Logging; +using Newtonsoft.Json; + +namespace Tranga.NotificationManagers; + +public class LunaSea : NotificationManager +{ + public string webhook { get; } + private readonly HttpClient _client = new(); + public LunaSea(string webhook, Logger? logger = null) : base(NotificationManagerType.LunaSea, logger) + { + this.webhook = webhook; + } + + public override void SendNotification(string title, string notificationText) + { + logger?.WriteLine(this.GetType().ToString(), $"Sending notification: {title} - {notificationText}"); + MessageData message = new(title, notificationText); + HttpRequestMessage request = new(HttpMethod.Post, webhook); + request.Content = new StringContent(JsonConvert.SerializeObject(message, Formatting.None), Encoding.UTF8, "application/json"); + HttpResponseMessage response = _client.Send(request); + if (!response.IsSuccessStatusCode) + { + StreamReader sr = new (response.Content.ReadAsStream()); + logger?.WriteLine(this.GetType().ToString(), $"{response.StatusCode}: {sr.ReadToEnd()}"); + } + } + + private class MessageData + { + public string title { get; } + public string body { get; } + public string image { get; } + + public MessageData(string title, string body) + { + this.title = title; + this.body = body; + this.image = ""; + } + } +} \ No newline at end of file