Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send thumbnail as attachment #247

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions Jellyfin.Plugin.Webhook/Destinations/Discord/DiscordClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,39 @@ public async Task SendAsync(DiscordOption option, Dictionary<string, object> dat
return;
}

_logger.LogDebug("SendAsync Body: {@Body}", body);
using var content = new StringContent(body, Encoding.UTF8, MediaTypeNames.Application.Json);
using var response = await _httpClientFactory
.CreateClient(NamedClient.Default)
.PostAsync(new Uri(option.WebhookUri), content)
.ConfigureAwait(false);
await response.LogIfFailedAsync(_logger).ConfigureAwait(false);
var thumbnailUrl = data.GetValueOrDefault("thumbnail_url") as string;
if (!string.IsNullOrWhiteSpace(thumbnailUrl))
{
_logger.LogDebug("SendAsync Body: {@Body}", body);

// get the image file
var imageBytes = await _httpClientFactory
.CreateClient(NamedClient.Default)
.GetByteArrayAsync(new Uri(thumbnailUrl))
.ConfigureAwait(true);

// send the image file and the body as a multipart form
using var content = new MultipartFormDataContent
{
{ new ByteArrayContent(imageBytes), "files[0]", "thumbnail.jpg" },
{ new StringContent(body, Encoding.UTF8, MediaTypeNames.Application.Json), "payload_json" }
};
using var response = await _httpClientFactory
.CreateClient(NamedClient.Default)
.PostAsync(new Uri(option.WebhookUri), content)
.ConfigureAwait(false);
await response.LogIfFailedAsync(_logger).ConfigureAwait(false);
}
else
{
_logger.LogDebug("SendAsync Body: {@Body}", body);
using var content = new StringContent(body, Encoding.UTF8, MediaTypeNames.Application.Json);
using var response = await _httpClientFactory
.CreateClient(NamedClient.Default)
.PostAsync(new Uri(option.WebhookUri), content)
.ConfigureAwait(false);
await response.LogIfFailedAsync(_logger).ConfigureAwait(false);
}
}
catch (HttpRequestException e)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"thumbnail_url": "{{ServerUrl}}/Items/{{ItemId}}/Images/Primary",
"content": "{{MentionType}}",
"avatar_url": "{{AvatarUrl}}",
"username": "{{BotUsername}}",
Expand All @@ -22,7 +23,7 @@


"thumbnail":{
"url": "{{ServerUrl}}/Items/{{ItemId}}/Images/Primary"
"url": "attachment://thumbnail.jpg"
},

"description": "
Expand Down