Skip to content

Commit

Permalink
Add Discord Notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
holycow234234 committed Feb 13, 2022
1 parent 6ae06e9 commit 02d6071
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,18 @@ The [Pushover](https://www.pushover.net/) notification will send an image and a
* Sound [optional]: The [sound](https://pushover.net/api#sounds) to override the user's default sound choice
* Priority [optional]: The [priority](https://pushover.net/api#priority) with which to send the message

### Discord
Send notifications to a discord server via Discord Webhooks. You can get a Discord Webhook URL for your Discord server by following the instructions [here](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks).

```json
{
"Type": "Discord",
"DiscordWebhookUrl": " https://discord.com/api/webhooks/F4K3W3BH00K"
}
```
* DiscordWebhookUrl [required]: The URL of the Discord Webhook you want to send messages to.


## Caveats
* SynoAI still relies on Surveillance Station triggering the motion alerts
* Looking for an object, such as a car on a driveway, will continually trigger alerts if that object is in view of the camera when Surveillance Station detects movement, e.g. a tree blowing in the wind.
Expand Down
47 changes: 47 additions & 0 deletions SynoAI/Notifiers/Discord/Discord.cs
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}'.");
}
}
}
}
19 changes: 19 additions & 0 deletions SynoAI/Notifiers/Discord/DiscordFactory.cs
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
};
}
}
}
4 changes: 4 additions & 0 deletions SynoAI/Notifiers/NotifierFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using SynoAI.Notifiers.Pushover;
using SynoAI.Notifiers.Telegram;
using SynoAI.Notifiers.Webhook;
using SynoAI.Notifiers.Discord;
using System;
using System.Collections.Generic;

Expand Down Expand Up @@ -37,6 +38,9 @@ public static INotifier Create(NotifierType type, ILogger logger, IConfiguration
case NotifierType.Webhook:
factory = new WebhookFactory();
break;
case NotifierType.Discord:
factory = new DiscordFactory();
break;
default:
throw new NotImplementedException(type.ToString());
}
Expand Down
6 changes: 5 additions & 1 deletion SynoAI/Notifiers/NotifierType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public enum NotifierType
/// <summary>
/// Calls a webhook with the image attached.
/// </summary>
Webhook
Webhook,
/// <summary>
/// Sends a notification message using Discord with the image attached.
/// </summary>
Discord
}
}

0 comments on commit 02d6071

Please sign in to comment.