-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ae06e9
commit 02d6071
Showing
5 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Http.Json; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using SynoAI.App; | ||
using SynoAI.Models; | ||
|
||
namespace SynoAI.Notifiers.Discord | ||
{ | ||
public class Discord : NotifierBase | ||
{ | ||
|
||
/// <summary> | ||
/// Discord Webhook Url | ||
/// </summary> | ||
public string DiscordWebhookUrl { get; set; } | ||
public override async Task SendAsync(Camera camera, Notification notification, ILogger logger) | ||
{ | ||
var formData = new MultipartFormDataContent(); | ||
ProcessedImage processedImage = notification.ProcessedImage; | ||
|
||
var message = base.GetMessage(camera, notification.FoundTypes); | ||
//Discord seems to require double escaped newlines | ||
message = message.Replace("\n", "\\n"); | ||
|
||
formData.Add(new StringContent($"{{\"content\":\"{message}\"}}"),"payload_json"); | ||
formData.Add(new StreamContent(processedImage.GetReadonlyStream()),"file",processedImage.FileName); | ||
|
||
HttpResponseMessage responseMessage = await Shared.HttpClient.PostAsync(DiscordWebhookUrl, formData); | ||
if (responseMessage.IsSuccessStatusCode) | ||
{ | ||
logger.LogInformation($"{camera.Name}: Discord: Notification sent successfully"); | ||
} | ||
else | ||
{ | ||
string error = await responseMessage.Content.ReadAsStringAsync(); | ||
logger.LogError($"{camera.Name}: Discord: The end point responded with HTTP status code '{responseMessage.StatusCode}' and error '{error}'."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace SynoAI.Notifiers.Discord | ||
{ | ||
public class DiscordFactory : NotifierFactory | ||
{ | ||
public override INotifier Create(ILogger logger, IConfigurationSection section) | ||
{ | ||
string discordWebhookUrl = section.GetValue<string>("DiscordWebhookUrl"); | ||
logger.LogInformation("Processing Discord Config", discordWebhookUrl); | ||
|
||
return new Discord() | ||
{ | ||
DiscordWebhookUrl = discordWebhookUrl | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters