feat: add Discord notification options and handlers for YouTube events

This commit is contained in:
Stevan Freeborn
2025-05-28 17:14:55 -05:00
parent 1afd17f2d1
commit 2ddc472f46
4 changed files with 35 additions and 11 deletions
@@ -0,0 +1,10 @@
namespace StevesBot.Webhook.YouTube;
internal sealed record DiscordNotificationOptions
{
[Required]
public string ChannelId { get; init; } = string.Empty;
[Required]
public string MessageFormat { get; init; } = string.Empty;
}
@@ -1,10 +1,5 @@
using StevesBot.Library.Discord.Rest; namespace StevesBot.Webhook.YouTube.Handlers;
using StevesBot.Library.Discord.Rest.Requests;
namespace StevesBot.Webhook.Handlers;
// TODO: Implement logic to do the following:
// - if is stream create discord message
internal static class NotificationHandler internal static class NotificationHandler
{ {
public static async Task<IResult> HandleAsync( public static async Task<IResult> HandleAsync(
@@ -12,6 +7,7 @@ internal static class NotificationHandler
[FromServices] ILogger<Program> logger, [FromServices] ILogger<Program> logger,
[FromServices] IYouTubeDataApiClient youTubeDataApiClient, [FromServices] IYouTubeDataApiClient youTubeDataApiClient,
[FromServices] IDiscordRestClient discordRestClient, [FromServices] IDiscordRestClient discordRestClient,
[FromServices] IOptionsMonitor<DiscordNotificationOptions> discordNotificationOptions,
CancellationToken cancellationToken CancellationToken cancellationToken
) )
{ {
@@ -43,13 +39,22 @@ internal static class NotificationHandler
return Results.Ok(); return Results.Ok();
} }
logger.LogInformation("Video ID {VideoId} is a live stream.", videoId); logger.LogInformation(
"Video ID {VideoId} is a live stream. Creating discord message in channel {ChannelId}",
videoId,
discordNotificationOptions.CurrentValue.ChannelId
);
var msg = string.Format(
CultureInfo.InvariantCulture,
discordNotificationOptions.CurrentValue.MessageFormat,
videoId
);
await discordRestClient.CreateMessageAsync( await discordRestClient.CreateMessageAsync(
channelId: "1372680179121524778", channelId: discordNotificationOptions.CurrentValue.ChannelId,
request: new CreateMessageRequest( request: new CreateMessageRequest(
Content: "@everyone Stevan is trying to be a streamer again! " + Content: msg,
$"Check out the stream here: https://www.youtube.com/watch?v={videoId}",
MessageReference: null MessageReference: null
), ),
cancellationToken: cancellationToken cancellationToken: cancellationToken
@@ -1,4 +1,4 @@
namespace StevesBot.Webhook.Handlers; namespace StevesBot.Webhook.YouTube.Handlers;
internal static class VerifySubscriptionHandler internal static class VerifySubscriptionHandler
{ {
@@ -16,5 +16,14 @@
}, },
"PubSubClientOptions": { "PubSubClientOptions": {
"BaseUrl": "BaseUrl" "BaseUrl": "BaseUrl"
},
"DiscordClientOptions": {
"ApiUrl": "ApiUrl",
"AppToken": "AppToken",
"Intents": 0
},
"DiscordNotificationOptions": {
"ChannelId": "ChannelId",
"MessageFormat": "MessageFormat"
} }
} }