Implement option to skip sending webhooks with an empty message body (#202)

This commit is contained in:
Daniel Tolnai 2024-03-24 17:10:51 +01:00 committed by GitHub
parent 06c0c82b28
commit 01ce5b2041
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 78 additions and 3 deletions

View File

@ -116,6 +116,10 @@
<input is="emby-checkbox" type="checkbox" data-name="chkSendAllProperties"/>
<span>Send All Properties (ignores template)</span>
</label>
<label class="checkboxContainer">
<input is="emby-checkbox" type="checkbox" data-name="chkSkipEmptyMessageBody"/>
<span>Do not send when message body is empty</span>
</label>
</div>
<div class="inputContainer">
<label>Template:</label>

View File

@ -162,6 +162,7 @@
element.querySelector("[data-name=txtWebhookName]").value = config.WebhookName || "";
element.querySelector("[data-name=txtWebhookUri]").value = config.WebhookUri || "";
element.querySelector("[data-name=chkSendAllProperties]").checked = config.SendAllProperties || false;
element.querySelector("[data-name=chkSkipEmptyMessageBody]").checked = config.SkipEmptyMessageBody || false;
element.querySelector("[data-name=txtTemplate]").value = Webhook.atou(config.Template || "");
const notificationTypeContainer = element.querySelector("[data-name=notificationTypeContainer]");
@ -182,6 +183,7 @@
config.WebhookName = element.querySelector("[data-name=txtWebhookName]").value || "";
config.WebhookUri = element.querySelector("[data-name=txtWebhookUri]").value || "";
config.SendAllProperties = element.querySelector("[data-name=chkSendAllProperties]").checked || false;
config.SkipEmptyMessageBody = element.querySelector("[data-name=chkSkipEmptyMessageBody]").checked || false;
config.Template = Webhook.utoa(element.querySelector("[data-name=txtTemplate]").value || "");
config.NotificationTypes = [];

View File

@ -36,4 +36,25 @@ public class BaseClient
return true;
}
/// <summary>
/// Determines whether the client should send the webhook with the given message body.
/// </summary>
/// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
/// <param name="option">The sender option.</param>
/// <param name="body">The message body.</param>
/// <returns>Whether the client should send the webhook.</returns>
protected bool SendMessageBody(
ILogger logger,
BaseOption option,
string body)
{
if (option.SkipEmptyMessageBody && string.IsNullOrWhiteSpace(body))
{
logger.LogDebug("Skip sending empty message body");
return false;
}
return true;
}
}

View File

@ -67,6 +67,11 @@ public abstract class BaseOption
/// </summary>
public bool SendAllProperties { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to skip sending an empty message body.
/// </summary>
public bool SkipEmptyMessageBody { get; set; }
/// <summary>
/// Gets or sets the handlebars template.
/// </summary>

View File

@ -64,6 +64,11 @@ public class DiscordClient : BaseClient, IWebhookClient<DiscordOption>
}
var body = option.GetMessageBody(data);
if (!SendMessageBody(_logger, option, body))
{
return;
}
_logger.LogDebug("SendAsync Body: {@Body}", body);
using var content = new StringContent(body, Encoding.UTF8, MediaTypeNames.Application.Json);
using var response = await _httpClientFactory

View File

@ -53,6 +53,11 @@ public class GenericClient : BaseClient, IWebhookClient<GenericOption>
}
var body = option.GetMessageBody(data);
if (!SendMessageBody(_logger, option, body))
{
return;
}
_logger.LogDebug("SendAsync Body: {@Body}", body);
using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, option.WebhookUri);
var contentType = MediaTypeNames.Text.Plain;

View File

@ -50,6 +50,11 @@ public class GenericFormClient : BaseClient, IWebhookClient<GenericFormOption>
}
var body = option.GetMessageBody(data);
if (!SendMessageBody(_logger, option, body))
{
return;
}
var dictionaryBody = JsonSerializer.Deserialize<Dictionary<string, string>>(body);
if (dictionaryBody is null)
{

View File

@ -48,6 +48,11 @@ public class GotifyClient : BaseClient, IWebhookClient<GotifyOption>
data["Priority"] = option.Priority;
var body = option.GetMessageBody(data);
if (!SendMessageBody(_logger, option, body))
{
return;
}
_logger.LogDebug("SendAsync Body: {@Body}", body);
using var content = new StringContent(body, Encoding.UTF8, MediaTypeNames.Application.Json);
using var response = await _httpClientFactory

View File

@ -36,6 +36,11 @@ public class MqttClient : BaseClient, IWebhookClient<MqttOption>
}
var body = option.GetMessageBody(data);
if (!SendMessageBody(_logger, option, body))
{
return;
}
var client = _mqttClients.GetClient(option.Guid);
if (client?.IsConnected != true)
{

View File

@ -45,6 +45,11 @@ public class PushbulletClient : BaseClient, IWebhookClient<PushbulletOption>
data["PushbulletChannel"] = option.Channel;
var body = option.GetMessageBody(data);
if (!SendMessageBody(_logger, option, body))
{
return;
}
_logger.LogDebug("SendAsync Body: {@Body}", body);
using var requestOptions = new HttpRequestMessage(HttpMethod.Post, string.IsNullOrEmpty(option.WebhookUri) ? PushbulletOption.ApiUrl : option.WebhookUri);

View File

@ -72,6 +72,11 @@ public class PushoverClient : BaseClient, IWebhookClient<PushoverOption>
}
var body = option.GetMessageBody(data);
if (!SendMessageBody(_logger, option, body))
{
return;
}
_logger.LogDebug("SendAsync Body: {@Body}", body);
using var content = new StringContent(body, Encoding.UTF8, MediaTypeNames.Application.Json);
using var response = await _httpClientFactory

View File

@ -49,6 +49,11 @@ public class SlackClient : BaseClient, IWebhookClient<SlackOption>
data["SlackIconUrl"] = option.IconUrl;
var body = option.GetMessageBody(data);
if (!SendMessageBody(_logger, option, body))
{
return;
}
_logger.LogDebug("SendAsync Body: {@Body}", body);
using var content = new StringContent(body, Encoding.UTF8, MediaTypeNames.Application.Json);
using var response = await _httpClientFactory

View File

@ -37,10 +37,13 @@ public class SmtpClient : BaseClient, IWebhookClient<SmtpOption>
message.To.Add(new MailboxAddress(option.ReceiverAddress, option.ReceiverAddress));
message.Subject = option.GetCompiledSubjectTemplate()(data);
message.Body = new TextPart(option.IsHtml ? "html" : "plain")
var body = option.GetMessageBody(data);
if (!SendMessageBody(_logger, option, body))
{
Text = option.GetMessageBody(data)
};
return;
}
message.Body = new TextPart(option.IsHtml ? "html" : "plain") { Text = body };
using var smtpClient = new MailKit.Net.Smtp.SmtpClient();
await smtpClient.ConnectAsync(option.SmtpServer, option.SmtpPort, option.UseSsl)