Add Publishing #7 #16

Open
seba76 wants to merge 2 commits from seba76/master into master
8 changed files with 692 additions and 240 deletions
Showing only changes of commit 2c18f91d68 - Show all commits

View File

@@ -101,12 +101,13 @@ namespace ntfysh_client
// //
topicContextMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { sendNotificationMenuItem }); topicContextMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { sendNotificationMenuItem });
topicContextMenu.Name = "topicContextMenu"; topicContextMenu.Name = "topicContextMenu";
topicContextMenu.Size = new System.Drawing.Size(167, 26); topicContextMenu.Size = new System.Drawing.Size(181, 48);
// //
// sendNotificationMenuItem // sendNotificationMenuItem
// //
sendNotificationMenuItem.Image = (System.Drawing.Image)resources.GetObject("sendNotificationMenuItem.Image");
sendNotificationMenuItem.Name = "sendNotificationMenuItem"; sendNotificationMenuItem.Name = "sendNotificationMenuItem";
sendNotificationMenuItem.Size = new System.Drawing.Size(166, 22); sendNotificationMenuItem.Size = new System.Drawing.Size(180, 22);
sendNotificationMenuItem.Text = "Send Notification"; sendNotificationMenuItem.Text = "Send Notification";
sendNotificationMenuItem.Click += SendNotificationMenuItem_Click; sendNotificationMenuItem.Click += SendNotificationMenuItem_Click;
// //
@@ -162,7 +163,7 @@ namespace ntfysh_client
// //
exitToolStripMenuItem1.Image = (System.Drawing.Image)resources.GetObject("exitToolStripMenuItem1.Image"); exitToolStripMenuItem1.Image = (System.Drawing.Image)resources.GetObject("exitToolStripMenuItem1.Image");
exitToolStripMenuItem1.Name = "exitToolStripMenuItem1"; exitToolStripMenuItem1.Name = "exitToolStripMenuItem1";
exitToolStripMenuItem1.Size = new System.Drawing.Size(116, 22); exitToolStripMenuItem1.Size = new System.Drawing.Size(180, 22);
exitToolStripMenuItem1.Text = "Exit"; exitToolStripMenuItem1.Text = "Exit";
exitToolStripMenuItem1.Click += exitToolStripMenuItem1_Click; exitToolStripMenuItem1.Click += exitToolStripMenuItem1_Click;
// //
@@ -170,7 +171,7 @@ namespace ntfysh_client
// //
settingsToolStripMenuItem.Image = (System.Drawing.Image)resources.GetObject("settingsToolStripMenuItem.Image"); settingsToolStripMenuItem.Image = (System.Drawing.Image)resources.GetObject("settingsToolStripMenuItem.Image");
settingsToolStripMenuItem.Name = "settingsToolStripMenuItem"; settingsToolStripMenuItem.Name = "settingsToolStripMenuItem";
settingsToolStripMenuItem.Size = new System.Drawing.Size(116, 22); settingsToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
settingsToolStripMenuItem.Text = "Settings"; settingsToolStripMenuItem.Text = "Settings";
settingsToolStripMenuItem.Click += settingsToolStripMenuItem_Click; settingsToolStripMenuItem.Click += settingsToolStripMenuItem_Click;
// //

View File

@@ -426,15 +426,19 @@ namespace ntfysh_client
{ {
if (notificationTopics.SelectedItem != null && string.IsNullOrEmpty(notificationTopics.SelectedItem as string) == false) if (notificationTopics.SelectedItem != null && string.IsNullOrEmpty(notificationTopics.SelectedItem as string) == false)
{ {
var dlg = new SendMessageForm(); try
var r = dlg.ShowDialog(this);
if (r == DialogResult.OK)
{ {
var topicAndHost = notificationTopics.SelectedItem.ToString()?.Split("@"); SendMessageForm dlg = new SendMessageForm();
var topic = topicAndHost[0]; DialogResult r = dlg.ShowDialog(this);
var host = topicAndHost[1].Replace("wss", "https"); if (r == DialogResult.OK)
var msg = dlg.Message; {
await _notificationListener.SendNotification(host, new NtfyEvent { Topic = topic, Title = dlg.Title, Message = msg }); string key = notificationTopics.SelectedItem.ToString();
await _notificationListener.SendNotification(key, dlg.Title, dlg.Message);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK);
} }
} }
} }

View File

@@ -120,6 +120,26 @@
<metadata name="topicContextMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="topicContextMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>384, 17</value> <value>384, 17</value>
</metadata> </metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="sendNotificationMenuItem.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAALtSURBVGhD7ZfPaxNBFMeTkqS9WDyotTHJvjdJ/VUpirQK
elFKa1pSK2lNapOrP8BDPRQEL7X+qFjEg2JBEERrEc2hh0jT6EHUo3+DPSi2tV6E4sGmREaSsHlM1iaZ
lVnJFz6X3TeZ98lLZhObrZZaaqnF0kHEcUR8rTjjtO+iIOIkImYtwiTtvxBEXBAsUJUF2n8hehEAmELE
UZXI9VSeiKZpYU3TmhUjbKqIM/z47Pb9J4BeNwHzRByRF1dt8XTWfubV26a2ENL7kjFHxBF9eY1L5KmL
zj6hNZKRL5KfRIHY3KfNnZcP0DrJyBWhk7DF0+t1kcQdx+DMaKVsCt48QvcRIE+kqb2XEQkpOAemR+he
AuSJbDvY76dNyOCfi7j97V57fO4jaWTNHku+tw8n31WK69TDKN1LgDwRzpaOyE4qUzc0O+317nLTWsnI
FeEIZaKJ+7Tub2w9FG6h1wyQL8IpkomlftQH7wZpjRHOgZkRWzz1raFr4ji9VwJzRDh/ZIaTb+qDt7vo
PSOcp59d4sd27k1Yaei+cYzWCDBPpFL47zN+SBQ+mrH57xuYjHoinApkzBHhZz85hqsnllpxtx710b2s
JxJPZ/lDl+5lPZFY+qfBfxtzRPgzoLFz7HCl5E6ujF7CFZoy2tcckWpwhe712+Lzq0USfQ8GaR1BPRFH
JDFRxiTyqCfCcUQS1zc4iTxqinAau6900GsGqCtSJuWLIGIaEZ8a0CfYSE8vADwSrKsG3lPZIoYAwCoA
nBQINDPGLiBihq6RTGkRxhgf3S/BolI8pxIAcA4R1wS1MskwxoZo/0VhjPUwxm6VAhE/6F6wSAQRL5JJ
LNP1kuihfZcdxtiYSAQAzpNJLPv9/n10vTIRiVhOgoeKCL7YS4FAoJWuUy56EQD4gojrOomvALCbrlEy
ZCJ6lnw+315ar2xKiCxaSoJHILKoadoeWqd8ELENERO5U+ozY6yF1lgqHo9nByJq9Hottfxn+Q2nlJ4k
a2hbYwAAAABJRU5ErkJggg==
</value>
</data>
<metadata name="toolTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="toolTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>537, 17</value> <value>537, 17</value>
</metadata> </metadata>
@@ -129,7 +149,6 @@
<metadata name="trayContextMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="trayContextMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>123, 17</value> <value>123, 17</value>
</metadata> </metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="showControlWindowToolStripMenuItem.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> <data name="showControlWindowToolStripMenuItem.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value> <value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6 iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6
@@ -342,7 +361,7 @@
<data name="settingsToolStripMenuItem.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> <data name="settingsToolStripMenuItem.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value> <value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6 iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6
JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAACXBIWXMAAA6/AAAOvwE4BVMkAAABQUlE JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAACXBIWXMAAA69AAAOvQFH+5CtAAABQUlE
QVQ4T7WTTU4CQRCF5whG48KxnfoqMxfAcyjoDdz4cw1xLXoC2AgnULmPBuMST4B5pEigGeNGK+mkp1+/ QVQ4T7WTTU4CQRCF5whG48KxnfoqMxfAcyjoDdz4cw1xLXoC2AgnULmPBuMST4B5pEigGeNGK+mkp1+/
V1Wva4riv8Ld79x9Eauf460BXAPjlNIu8GpmF1ruPo2zse7kvGUAV8AbMHL3D+DLzDp1XR9r7+4zYAi8 V1Wva4riv8Ld79x9Eauf460BXAPjlNIu8GpmF1ruPo2zse7kvGUAV8AbMHL3D+DLzDp1XR9r7+4zYAi8
t4oAE12oquoA6JnZo/Za2gPdwEaqJOcXZrYjdTM7q+v6CHhQJZF5oDN3P9W32tkgyyT1DMwj2wB4Tikd t4oAE12oquoA6JnZo/Za2gPdwEaqJOcXZrYjdTM7q+v6CHhQJZF5oDN3P9W32tkgyyT1DMwj2wB4Tikd

View File

@@ -352,10 +352,24 @@ namespace ntfysh_client.Notifications
SubscribedTopicsByUnique.Remove(topicUniqueString); SubscribedTopicsByUnique.Remove(topicUniqueString);
} }
public async Task<HttpResponseMessage> SendNotification(string host, NtfyEvent message) public async Task SendNotification(string key, string title, string message)
{ {
var httpClient = new HttpClient(); if (SubscribedTopicsByUnique.TryGetValue(key, out SubscribedTopic topic))
return await httpClient.PostAsJsonAsync<NtfyEvent>(host, message); {
HttpClient httpClient = new HttpClient();
PublishEvent notification = new PublishEvent();
notification.Title = title;
notification.Message = message;
notification.Topic = topic.TopicId;
if (!string.IsNullOrEmpty(topic.Username) || !string.IsNullOrEmpty(topic.Password))
{
string value = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{topic.Username}:{topic.Password}"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", value);
}
HttpResponseMessage response = await httpClient.PostAsJsonAsync<PublishEvent>(topic.ServerUrl.Replace("wss://", "https://"), notification);
response.EnsureSuccessStatusCode();
}
} }
} }
} }

View File

@@ -0,0 +1,50 @@
using Newtonsoft.Json;
using System.Text.Json.Nodes;
namespace ntfysh_client.Notifications
{
internal class PublishEvent
{
[JsonProperty("topic")]
public string Topic { get; set; } = null!;
[JsonProperty("message")]
public string? Message { get; set; } = null;
[JsonProperty("title")]
public string? Title { get; set; } = null;
[JsonProperty("tags")]
public string[] Tags { get; set; } = null!;
[JsonProperty("priority")]
public NotificationPriority? Priority { get; set; } = null;
[JsonProperty("actions")]
public JsonArray? Actions { get; set; } = null;
[JsonProperty("click")]
public string? Click { get; set; } = null;
[JsonProperty("attach")]
public string? Attach { get; set; } = null;
[JsonProperty("markdown")]
public bool? Markdown { get; set; } = null;
[JsonProperty("icon")]
public string? Icon { get; set; } = null;
[JsonProperty("filename")]
public string? Filename { get; set; } = null;
[JsonProperty("delay")]
public string? Delay { get; set; } = null;
[JsonProperty("email")]
public string? Email { get; set; } = null;
[JsonProperty("call")]
public string? Call { get; set; } = null;
}
}

View File

@@ -92,7 +92,7 @@
MaximizeBox = false; MaximizeBox = false;
MinimizeBox = false; MinimizeBox = false;
Name = "SendMessageForm"; Name = "SendMessageForm";
Text = "SendMessageForm"; Text = "Send Message";
ResumeLayout(false); ResumeLayout(false);
PerformLayout(); PerformLayout();
} }