Add Apprise-compatible webhook payload handling

pull/1163/head
voc0der 2 months ago
parent 02cbd6520f
commit c52e079453

@ -38,6 +38,12 @@ const NOTIFICATION_TYPE_TO_THUMBNAIL = {
download_error: () => null
}
const NOTIFICATION_TYPE_TO_APPRISE_TYPE = {
task_finished: 'success',
download_complete: 'success',
download_error: 'failure'
}
exports.sendNotification = async (notification) => {
// info necessary if we are using 3rd party APIs
const type = notification['type'];
@ -313,14 +319,44 @@ function sendSlackNotification({body, title, type, url, thumbnail}) {
// Generic
function isLikelyAppriseWebhookURL(webhook_url) {
try {
const url = new URL(webhook_url);
const path = url.pathname.toLowerCase();
const host = url.hostname.toLowerCase();
// Apprise commonly exposes /notify[/<key>], while reverse proxies often include /apprise.
return /(^|\/)notify(\/[\w-]{1,128})?\/?$/.test(path) || /(^|\/)apprise(\/|$)/.test(path) || host.includes('apprise');
} catch {
return false;
}
}
function mapNotificationTypeToAppriseType(type) {
return NOTIFICATION_TYPE_TO_APPRISE_TYPE[type] ? NOTIFICATION_TYPE_TO_APPRISE_TYPE[type] : 'info';
}
function getWebhookPayload(webhook_url, data) {
if (!isLikelyAppriseWebhookURL(webhook_url)) return data;
return {
title: data['title'],
body: data['body'],
type: mapNotificationTypeToAppriseType(data['type']),
event_type: data['type'],
url: data['url'],
thumbnail: data['thumbnail']
};
}
function sendGenericNotification(data) {
const webhook_url = config_api.getConfigItem('ytdl_webhook_url');
logger.verbose(`Sending generic notification to ${webhook_url}`);
const payload = getWebhookPayload(webhook_url, data);
fetch(webhook_url, {
method: 'POST',
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data),
body: JSON.stringify(payload),
});
}

@ -400,7 +400,7 @@
<mat-form-field class="text-field" color="accent">
<mat-label i18n="webhook URL">Webhook URL</mat-label>
<input placeholder="https://example.com/endpoint/12345" [(ngModel)]="new_config['API']['webhook_URL']" matInput>
<mat-hint>Place endpoint URL here to integrate with services like Zapier and Automatisch.</mat-hint>
<mat-hint>Place endpoint URL here to integrate with services like Zapier, Automatisch, and Apprise (/notify/&lt;key&gt;).</mat-hint>
</mat-form-field>
</div>
<div class="col-12 mb-2 mt-3">

Loading…
Cancel
Save