|
| 1 | +using Microsoft.Extensions.Logging; |
| 2 | +using Newtonsoft.Json; |
| 3 | +using SynoAI.Models; |
| 4 | +using SynoAI.Services; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.IO; |
| 8 | +using System.Linq; |
| 9 | +using System.Net.Http; |
| 10 | +using System.Net.Http.Headers; |
| 11 | +using System.Net.Http.Json; |
| 12 | +using System.Text; |
| 13 | +using System.Threading.Tasks; |
| 14 | + |
| 15 | +namespace SynoAI.Notifiers.SynologyChat |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Calls a third party API. |
| 19 | + /// </summary> |
| 20 | + public class SynologyChat : NotifierBase |
| 21 | + { |
| 22 | + /// <summary> |
| 23 | + /// The URL to send the request to including the token. |
| 24 | + /// </summary> |
| 25 | + public string Url { get; set; } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Sends a notification to the Webhook. |
| 29 | + /// </summary> |
| 30 | + /// <param name="camera">The camera that triggered the notification.</param> |
| 31 | + /// <param name="notification">The notification data to process.</param> |
| 32 | + /// <param name="logger">A logger.</param> |
| 33 | + public override async Task SendAsync(Camera camera, Notification notification, ILogger logger) |
| 34 | + { |
| 35 | + logger.LogInformation($"{camera.Name}: SynologyChat: Processing"); |
| 36 | + using (HttpClient client = new()) |
| 37 | + { |
| 38 | + IEnumerable<string> foundTypes = notification.FoundTypes; |
| 39 | + string message = GetMessage(camera, foundTypes); |
| 40 | + |
| 41 | + var request = new |
| 42 | + { |
| 43 | + text = message, |
| 44 | + file_url = new Uri(new Uri(Config.Url), new Uri($"Image/{camera.Name}/{notification.ProcessedImage.FileName}", UriKind.Relative)) |
| 45 | + }; |
| 46 | + |
| 47 | + string requestJson = JsonConvert.SerializeObject(request); |
| 48 | + Dictionary<string, string> payload = new() |
| 49 | + { |
| 50 | + { "payload", requestJson }, |
| 51 | + }; |
| 52 | + |
| 53 | + using (FormUrlEncodedContent content = new(payload)) |
| 54 | + { |
| 55 | + content.Headers.Clear(); |
| 56 | + content.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); |
| 57 | + |
| 58 | + logger.LogInformation($"{camera.Name}: SynologyChat: POSTing message."); |
| 59 | + |
| 60 | + HttpResponseMessage response = await client.PostAsync(Url, content); |
| 61 | + if (response.IsSuccessStatusCode) |
| 62 | + { |
| 63 | + // Check that it's actually successful, because Synology like to make things awkward |
| 64 | + string responseString = await response.Content.ReadAsStringAsync(); |
| 65 | + SynologyChatResponse actualResponse = JsonConvert.DeserializeObject<SynologyChatResponse>(responseString); |
| 66 | + if (actualResponse.Success) |
| 67 | + { |
| 68 | + logger.LogInformation($"{camera.Name}: SynologyChat: Success."); |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + logger.LogInformation($"{camera.Name}: SynologyChat: Failed with error '{actualResponse.Error.Code}': {actualResponse.Error.Errors}."); |
| 73 | + } |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + logger.LogWarning($"{camera.Name}: SynologyChat: The end point responded with HTTP status code '{response.StatusCode}'."); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments