Newsbot.Collector/Newsbot.Collector.Services/Notifications/Discord/DiscordWebhookClient.cs
James Tombleson 9be985da0a
Features/adding youtube (#13)
* Found the meta tags on youtube... in the body and updated the client to pull them out.

* Updated namespace on test

* I think formatting cleaned this up

* Seed migrations have been cleaned up to get my configs out and moving them to a script.

* Updates to the ISourcesRepository.cs to allow for new calls to the db.

* formatter

* Db models updated. Icon now can track sourceID and source can have a youtube id.

* Updated api logger to ignore otel if no connection string given.

* updated docker init so I can run migrations from the image

* seed was updated to reflect the new api changes

* Updated the SourcesController.cs to grab icon data.

* Added reddit const values

* Minor changes to HtmlPageReader.cs

* Jobs are now pulling in the config section to bundle values.

* Removed youtube api, not needed anymore.

* test updates
2023-03-31 22:49:39 -07:00

43 lines
1.4 KiB
C#

using System.Net;
using System.Text;
using Newsbot.Collector.Domain.Interfaces;
using Newsbot.Collector.Domain.Models;
using Newtonsoft.Json;
namespace Newsbot.Collector.Services.Notifications.Discord;
public class DiscordWebhookClient : IDiscordNotificatioClient
{
private readonly string[] _webhooks;
public DiscordWebhookClient(string webhook)
{
_webhooks = new[] { webhook };
}
public DiscordWebhookClient(string[] webhooks)
{
_webhooks = webhooks;
}
public void SendMessage(DiscordMessage payload)
{
if (payload.Embeds is not null) MessageValidation.IsEmbedFooterValid(payload.Embeds);
foreach (var webhook in _webhooks)
{
var jsonRaw = JsonConvert.SerializeObject(payload, Formatting.None,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
using StringContent jsonContent = new(jsonRaw, Encoding.UTF8, "application/json");
using var client = new HttpClient();
var resp = client.PostAsync(webhook, jsonContent);
resp.Wait();
// can be 204 or a message, might be 200
Console.WriteLine(resp.Result.StatusCode);
if (resp.Result.StatusCode != HttpStatusCode.NoContent)
throw new Exception("Message was not accepted by the sever.");
}
}
}