-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add discord webhook handler * Add documentation * Add missing parameter to iac test
- Loading branch information
Showing
30 changed files
with
469 additions
and
98 deletions.
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
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
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,64 @@ | ||
# Discord event handling / commands | ||
|
||
Things that need to be done before this App can be commanded from Discord: | ||
|
||
1. Application needs to be up and running! | ||
2. `INTERACTIONS ENDPOINT URL` needs to be configured, this requires an | ||
endpoint which can handle `PING` from Discord. See | ||
[Discord API Documentation](https://discord.com/developers/docs/interactions/overview#preparing-for-interactions) for details. | ||
3. Command needs to be registed. This can be done with `ConsoleTester`. | ||
|
||
We are using Webhooks / `INTERACTIONS ENDPOINT URL`, not the socket, because we | ||
don't want to keep the Functions app running. | ||
|
||
## Registering command | ||
|
||
Slash command are given in Discord with `/<command-here>`, that triggers the webhook. | ||
|
||
Example interaction from slash command | ||
|
||
```json | ||
{ | ||
"type": 2, | ||
"token": "A_UNIQUE_TOKEN", | ||
"member": { | ||
"user": { | ||
"id": "53908232506183680", | ||
"username": "Mason", | ||
"avatar": "a_d5efa99b3eeaa7dd43acca82f5692432", | ||
"discriminator": "1337", | ||
"public_flags": 131141 | ||
}, | ||
"roles": ["539082325061836999"], | ||
"premium_since": null, | ||
"permissions": "2147483647", | ||
"pending": false, | ||
"nick": null, | ||
"mute": false, | ||
"joined_at": "2017-03-13T19:19:14.040000+00:00", | ||
"is_pending": false, | ||
"deaf": false | ||
}, | ||
"id": "786008729715212338", | ||
"guild_id": "290926798626357999", | ||
"app_permissions": "442368", | ||
"guild_locale": "en-US", | ||
"locale": "en-US", | ||
"data": { | ||
"options": [{ | ||
"type": 3, | ||
"name": "cardname", | ||
"value": "The Gitrog Monster" | ||
}], | ||
"type": 1, | ||
"name": "cardsearch", | ||
"id": "771825006014889984" | ||
}, | ||
"channel_id": "645027906669510667" | ||
} | ||
``` | ||
|
||
## Links | ||
|
||
* [Slash command handling](https://discord.com/developers/docs/interactions/application-commands#slash-commands) | ||
* [Command service from Discord.Net](https://docs.discordnet.dev/api/Discord.Commands.CommandService.html) |
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
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ type DiscordSettings = { | |
token: string | ||
guildId: int | ||
channelId: int | ||
publicKey: string | ||
} | ||
|
||
@export() | ||
|
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,14 @@ | ||
<# | ||
.SYNOPSIS | ||
Helper script for testing trigger interactions. | ||
#> | ||
param( | ||
[Parameter()][string]$JsonFile | ||
) | ||
|
||
$ErrorActionPreference = "Stop" | ||
Set-StrictMode -Version Latest | ||
|
||
$content = Get-Content -Raw -Path $JsonFile | ||
|
||
Invoke-RestMethod -Method Post -Uri 'http://localhost:8080/api/HandleDiscordWebHook?code=mock-secret-for-local-testing' -Body $content -ContentType 'application/json' |
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 |
---|---|---|
@@ -1,34 +1,41 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace DiscordImagePoster.Common.Discord; | ||
|
||
/// <summary> | ||
/// Represents the configuration for the Discord bot that is required for | ||
/// connecting to the Discord API. | ||
/// </summary> | ||
public class DiscordConfiguration | ||
{ | ||
/// <summary> | ||
/// The token of the bot. This is used to authenticate the bot with the | ||
/// Discord API. | ||
/// </summary> | ||
[Required] | ||
[MinLength(1)] | ||
public required string Token { get; set; } | ||
|
||
/// <summary> | ||
/// The ID of the guild / server where the bot will post images. | ||
/// This can be fetched from the Discord server url. | ||
/// For example, the ID of the guild in the url https://discord.com/channels/123123/666666 is 123123. | ||
/// </summary> | ||
[Required] | ||
public ulong GuildId { get; set; } | ||
|
||
/// <summary> | ||
/// The ID of the channel where the bot will post images. | ||
/// This can be fetched from the Discord server url. | ||
/// For example, the ID of the guild in the url https://discord.com/channels/123123/666666 is 666666. | ||
/// </summary> | ||
[Required] | ||
public ulong ChannelId { get; set; } | ||
} | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace DiscordImagePoster.Common.Discord; | ||
|
||
/// <summary> | ||
/// Represents the configuration for the Discord bot that is required for | ||
/// connecting to the Discord API. | ||
/// </summary> | ||
public class DiscordConfiguration | ||
{ | ||
/// <summary> | ||
/// The token of the bot. This is used to authenticate the bot with the | ||
/// Discord API. | ||
/// </summary> | ||
[Required] | ||
[MinLength(1)] | ||
public required string Token { get; set; } | ||
|
||
/// <summary> | ||
/// The ID of the guild / server where the bot will post images. | ||
/// This can be fetched from the Discord server url. | ||
/// For example, the ID of the guild in the url https://discord.com/channels/123123/666666 is 123123. | ||
/// </summary> | ||
[Required] | ||
public ulong GuildId { get; set; } | ||
|
||
/// <summary> | ||
/// The ID of the channel where the bot will post images. | ||
/// This can be fetched from the Discord server url. | ||
/// For example, the ID of the guild in the url https://discord.com/channels/123123/666666 is 666666. | ||
/// </summary> | ||
[Required] | ||
public ulong ChannelId { get; set; } | ||
|
||
/// <summary> | ||
/// The public key of the bot. This is used to verify the authenticity of | ||
/// the requests sent to the bot. | ||
/// </summary> | ||
[Required] | ||
public required string PublicKey { get; set; } | ||
} |
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
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,6 @@ | ||
namespace DiscordImagePoster.Common.Discord; | ||
|
||
public interface IDiscordCommandRegisterer | ||
{ | ||
Task RegisterCommandsAsync(); | ||
} |
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,10 @@ | ||
namespace DiscordImagePoster.Common.RandomizationService; | ||
|
||
/// <summary> | ||
/// Asbtraction for sending a random image. | ||
/// This is a separate service so this can be used from multiple functions or other places. | ||
/// </summary> | ||
public interface IRandomImagePoster | ||
{ | ||
Task PostRandomImageAsync(); | ||
} |
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,74 @@ | ||
using DiscordImagePoster.Common.BlobStorageImageService; | ||
using DiscordImagePoster.Common.Discord; | ||
using DiscordImagePoster.Common.ImageAnalysis; | ||
using DiscordImagePoster.Common.IndexService; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace DiscordImagePoster.Common.RandomizationService; | ||
|
||
public class RandomImagePoster : IRandomImagePoster | ||
{ | ||
private readonly ILogger<RandomImagePoster> _logger; | ||
private readonly IDiscordImagePoster _discordImagePoster; | ||
private readonly IBlobStorageImageService _imageService; | ||
private readonly IIndexService _indexService; | ||
private readonly IRandomizationService _randomizationService; | ||
private readonly IImageAnalysisService _imageAnalysisService; | ||
|
||
public RandomImagePoster( | ||
ILogger<RandomImagePoster> logger, | ||
IDiscordImagePoster discordImagePoster, | ||
IBlobStorageImageService imageService, | ||
IIndexService indexService, | ||
IRandomizationService randomizationService, | ||
IImageAnalysisService imageAnalysisService | ||
) | ||
{ | ||
_logger = logger; | ||
_discordImagePoster = discordImagePoster; | ||
_imageService = imageService; | ||
_indexService = indexService; | ||
_randomizationService = randomizationService; | ||
_imageAnalysisService = imageAnalysisService; | ||
} | ||
|
||
public async Task PostRandomImageAsync() | ||
{ | ||
var index = await _indexService.GetIndexOrCreateNew(); | ||
var randomImage = _randomizationService.GetRandomImage(index); | ||
|
||
if (randomImage is null) | ||
{ | ||
_logger.LogError("No images found in index."); | ||
return; | ||
} | ||
|
||
var result = await _imageService.GetImageStream(randomImage.Name); | ||
if (result is null) | ||
{ | ||
_logger.LogError("No image found."); | ||
return; | ||
} | ||
|
||
var binaryData = BinaryData.FromStream(result.Content); | ||
var analyzationResults = await _imageAnalysisService.AnalyzeImageAsync(binaryData); | ||
|
||
_logger.LogInformation("Sending image {ImageName} with caption {Caption} and tags {Tags}", randomImage.Name, analyzationResults?.Caption, string.Join(", ", analyzationResults?.Tags ?? Array.Empty<string>())); | ||
var imageMetadata = new ImageMetadataUpdate | ||
{ | ||
Name = randomImage.Name, | ||
Caption = analyzationResults?.Caption, | ||
Tags = analyzationResults?.Tags | ||
}; | ||
|
||
var parameters = new ImagePostingParameters | ||
{ | ||
ImageStream = binaryData.ToStream(), | ||
FileName = randomImage.Name, | ||
Description = analyzationResults?.Caption | ||
}; | ||
await _discordImagePoster.SendImageAsync(parameters); | ||
|
||
await _indexService.IncreasePostingCountAndUpdateMetadataAsync(imageMetadata); | ||
} | ||
} |
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,8 @@ | ||
using CommandLine; | ||
|
||
namespace DiscordImagePoster.ConsoleTester.Options; | ||
|
||
[Verb("register-command", HelpText = "Register Discord commands.")] | ||
public class RegisterCommandVerb | ||
{ | ||
} |
Oops, something went wrong.