Added apprise plugin

This commit is contained in:
John Andrews
2022-04-24 17:52:34 +12:00
parent 67aa8d549e
commit 4d7662b861
26 changed files with 288 additions and 0 deletions

1
.gitignore vendored
View File

@@ -16,3 +16,4 @@ Plex/settings.json
Emby/settings.invalid.json
Emby/settings.json
VideoNodes/test.settings.dev.json
Apprise/settings.json

BIN
Apprise/Apprise.csproj Normal file

Binary file not shown.

34
Apprise/Apprise.en.json Normal file
View File

@@ -0,0 +1,34 @@
{
"Plugins": {
"Apprise": {
"Description": "A plugin that allows you to send messages to a Apprise server.",
"Fields": {
"ServerUrl": "Server",
"ServerUrl-Placeholder": "http://apprise.lan",
"ServerUrl-Help": "The URL of the Apprise server",
"Endpoint": "Endpoint",
"Endpoint-Placeholder": "notify/apprise",
"Endpoint-Help": "The endpoint on the Apprise server, for example notify/apprise"
}
}
},
"Flow": {
"Parts": {
"Apprise": {
"Outputs": {
"1": "Apprise message sent",
"2": "Apprise message failed to send"
},
"Description": "Sends a message to a Apprise server.",
"Fields": {
"Message": "Message",
"Message-Help": "The message to send to the Apprise server",
"Tag": "Tag",
"Tag-Help": "A list of tags to send this message as, if empty it will be tagged with ''all''.",
"MessageType": "Type",
"MessageType-Help": "The type of message to be sent"
}
}
}
}
}

View File

@@ -0,0 +1,92 @@
using System.ComponentModel;
using System.Text.Json;
namespace FileFlows.Apprise.Communication;
public class Apprise: Node
{
public override int Inputs => 1;
public override int Outputs => 2;
public override FlowElementType Type => FlowElementType.Communication;
public override string Icon => "fas fa-bell";
public override bool FailureNode => true;
[Required]
[TextVariable(1)]
public string Message { get; set; }
[StringArray(2)]
public string[] Tag { get; set; }
[DefaultValue("info")]
[Select(nameof(MessageTypeOptions), 3)]
public string MessageType { get; set; }
private static List<ListOption> _MessageTypeOptions;
public static List<ListOption> MessageTypeOptions
{
get
{
if (_MessageTypeOptions == null)
{
_MessageTypeOptions = new List<ListOption>
{
new ListOption { Label = "Information", Value = "info"},
new ListOption { Label = "Success", Value = "success"},
new ListOption { Label = "Warning", Value = "warning" },
new ListOption { Label = "Failure", Value = "failure"}
};
}
return _MessageTypeOptions;
}
}
public override int Execute(NodeParameters args)
{
var settings = args.GetPluginSettings<PluginSettings>();
if (string.IsNullOrWhiteSpace(settings?.Endpoint))
{
args.Logger?.WLog("No endpoint set");
return 2;
}
if (string.IsNullOrWhiteSpace(settings?.ServerUrl))
{
args.Logger?.WLog("No server URL set");
return 2;
}
string url = settings.ServerUrl;
if (url.EndsWith("/") == false)
url += "/";
if (settings.Endpoint.EndsWith("/"))
url += settings.Endpoint[1..];
else
url += settings.Endpoint;
string message = args.ReplaceVariables(this.Message);
if (string.IsNullOrWhiteSpace(message))
{
args.Logger?.WLog("No message to send");
return 2;
}
object data = new
{
body = message,
tag= Tag?.Any() != true ? "all" : String.Join(";", this.Tag),
type = this.MessageType?.EmptyAsNull() ?? "info"
};
var content = new StringContent(JsonSerializer.Serialize(data), Encoding.UTF8, "application/json");
using var httpClient = new HttpClient();
var response = httpClient.PostAsync(url, content).Result;
if (response.IsSuccessStatusCode)
return 1;
string error = response.Content.ReadAsStringAsync().Result;
args.Logger?.WLog("Error from Apprise: " + error);
return 2;
}
}

View File

@@ -0,0 +1,6 @@
namespace FileFlows.Apprise;
internal static class ExtensionMethods
{
public static string? EmptyAsNull(this string str) => str == string.Empty ? null : str;
}

5
Apprise/GlobalUsings.cs Normal file
View File

@@ -0,0 +1,5 @@
global using System;
global using System.Text;
global using System.ComponentModel.DataAnnotations;
global using FileFlows.Plugin;
global using FileFlows.Plugin.Attributes;

11
Apprise/Plugin.cs Normal file
View File

@@ -0,0 +1,11 @@
namespace FileFlows.Apprise;
public class Plugin : FileFlows.Plugin.IPlugin
{
public string Name => "Apprise Nodes";
public string MinimumVersion => "0.5.2.690";
public void Init()
{
}
}

18
Apprise/PluginSettings.cs Normal file
View File

@@ -0,0 +1,18 @@
namespace FileFlows.Apprise
{
using FileFlows.Plugin;
using FileFlows.Plugin.Attributes;
using System;
using System.ComponentModel.DataAnnotations;
public class PluginSettings:IPluginSettings
{
[Text(1)]
[Required]
public string ServerUrl { get; set; }
[Text(2)]
[Required]
public string Endpoint { get; set; }
}
}

View File

@@ -0,0 +1,56 @@
#if(DEBUG)
using FileFlows.Apprise.Communication;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace FileFlows.Apprise.Tests;
[TestClass]
public class AppriseTests
{
[TestMethod]
public void Apprise_Basic_All()
{
var args = new NodeParameters("test.file", new TestLogger(), false, string.Empty);
args.GetPluginSettingsJson = (string input) =>
{
return File.ReadAllText("../../../settings.json");
};
var node = new FileFlows.Apprise.Communication.Apprise();
node.Message = "a message";
Assert.AreEqual(1, node.Execute(args));
}
[TestMethod]
public void Apprise_Basic_Valid()
{
var args = new NodeParameters("test.file", new TestLogger(), false, string.Empty);
args.GetPluginSettingsJson = (string input) =>
{
return File.ReadAllText("../../../settings.json");
};
var node = new FileFlows.Apprise.Communication.Apprise();
node.Message = "a message";
node.Tag = new[] { "test" };
Assert.AreEqual(1, node.Execute(args));
}
[TestMethod]
public void Apprise_Basic_Invalid()
{
var args = new NodeParameters("test.file", new TestLogger(), false, string.Empty);
args.GetPluginSettingsJson = (string input) =>
{
return File.ReadAllText("../../../settings.json");
};
var node = new FileFlows.Apprise.Communication.Apprise();
node.Message = "a message";
node.Tag = new[] { "invalid" };
Assert.AreEqual(2, node.Execute(args));
}
}
#endif

View File

@@ -0,0 +1,59 @@
#if(DEBUG)
namespace FileFlows.Apprise.Tests;
using FileFlows.Plugin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
internal class TestLogger : ILogger
{
private List<string> Messages = new List<string>();
public void DLog(params object[] args) => Log("DBUG", args);
public void ELog(params object[] args) => Log("ERRR", args);
public void ILog(params object[] args) => Log("INFO", args);
public void WLog(params object[] args) => Log("WARN", args);
private void Log(string type, object[] args)
{
if (args == null || args.Length == 0)
return;
string message = type + " -> " +
string.Join(", ", args.Select(x =>
x == null ? "null" :
x.GetType().IsPrimitive || x is string ? x.ToString() :
System.Text.Json.JsonSerializer.Serialize(x)));
Messages.Add(message);
}
public bool Contains(string message)
{
if (string.IsNullOrWhiteSpace(message))
return false;
string log = string.Join(Environment.NewLine, Messages);
return log.Contains(message);
}
public override string ToString()
{
return String.Join(Environment.NewLine, this.Messages.ToArray());
}
public string GetTail(int length = 50)
{
if (length <= 0)
length = 50;
if (Messages.Count <= length)
return string.Join(Environment.NewLine, Messages);
return string.Join(Environment.NewLine, Messages.TakeLast(length));
}
}
#endif

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -25,6 +25,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plex", "Plex\Plex.csproj",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Emby", "Emby\Emby.csproj", "{C73A4E1F-E1CD-4803-BE6D-886DCBE18727}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Apprise", "Apprise\Apprise.csproj", "{CA750701-C4CF-482F-B5F3-A40E188F3E14}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -75,6 +77,10 @@ Global
{C73A4E1F-E1CD-4803-BE6D-886DCBE18727}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C73A4E1F-E1CD-4803-BE6D-886DCBE18727}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C73A4E1F-E1CD-4803-BE6D-886DCBE18727}.Release|Any CPU.Build.0 = Release|Any CPU
{CA750701-C4CF-482F-B5F3-A40E188F3E14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CA750701-C4CF-482F-B5F3-A40E188F3E14}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CA750701-C4CF-482F-B5F3-A40E188F3E14}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CA750701-C4CF-482F-B5F3-A40E188F3E14}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.