Skip to content

Commit

Permalink
Added support for custom webhook URLs for notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Tzahi12345 committed Apr 12, 2023
1 parent 7593a23 commit fb27264
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
3 changes: 2 additions & 1 deletion backend/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ const DEFAULT_CONFIG = {
"gotify_app_token": "",
"use_telegram_API": false,
"telegram_bot_token": "",
"telegram_chat_id": ""
"telegram_chat_id": "",
"webhook_URL": ""
},
"Themes": {
"default_theme": "default",
Expand Down
4 changes: 4 additions & 0 deletions backend/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ exports.CONFIG_ITEMS = {
'key': 'ytdl_telegram_chat_id',
'path': 'YoutubeDLMaterial.API.telegram_chat_id'
},
'ytdl_webhook_url': {
'key': 'ytdl_webhook_url',
'path': 'YoutubeDLMaterial.API.webhook_URL'
},


// Themes
Expand Down
40 changes: 30 additions & 10 deletions backend/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,28 @@ const NOTIFICATION_TYPE_TO_THUMBNAIL = {
exports.sendNotification = async (notification) => {
// info necessary if we are using 3rd party APIs
const type = notification['type'];
const title = NOTIFICATION_TYPE_TO_TITLE[type];
const body = NOTIFICATION_TYPE_TO_BODY[type](notification);
const url = NOTIFICATION_TYPE_TO_URL[type](notification);
const thumbnail = NOTIFICATION_TYPE_TO_THUMBNAIL[type](notification);

const data = {
title: NOTIFICATION_TYPE_TO_TITLE[type],
body: NOTIFICATION_TYPE_TO_BODY[type](notification),
type: type,
url: NOTIFICATION_TYPE_TO_URL[type](notification),
thumbnail: NOTIFICATION_TYPE_TO_THUMBNAIL[type](notification)
}

if (config_api.getConfigItem('ytdl_use_ntfy_API') && config_api.getConfigItem('ytdl_ntfy_topic_url')) {
sendNtfyNotification(body, title, type, url, thumbnail);
sendNtfyNotification(data);
}
if (config_api.getConfigItem('ytdl_use_gotify_API') && config_api.getConfigItem('ytdl_gotify_server_url') && config_api.getConfigItem('ytdl_gotify_app_token')) {
sendGotifyNotification(body, title, type, url, thumbnail);
sendGotifyNotification(data);
}
if (config_api.getConfigItem('ytdl_use_telegram_API') && config_api.getConfigItem('ytdl_telegram_bot_token') && config_api.getConfigItem('ytdl_telegram_chat_id')) {
sendTelegramNotification(body, title, type, url, thumbnail);
sendTelegramNotification(data);
}
if (config_api.getConfigItem('ytdl_webhook_url')) {
sendGenericNotification(data);
}

await db_api.insertRecordIntoTable('notifications', notification);
return notification;
}
Expand Down Expand Up @@ -95,7 +103,7 @@ function notificationEnabled(type) {
return config_api.getConfigItem('ytdl_enable_notifications') && (config_api.getConfigItem('ytdl_enable_all_notifications') || config_api.getConfigItem('ytdl_allowed_notification_types').includes(type));
}

function sendNtfyNotification(body, title, type, url, thumbnail) {
function sendNtfyNotification({body, title, type, url, thumbnail}) {
logger.verbose('Sending notification to ntfy');
fetch(config_api.getConfigItem('ytdl_ntfy_topic_url'), {
method: 'POST',
Expand All @@ -109,7 +117,7 @@ function sendNtfyNotification(body, title, type, url, thumbnail) {
});
}

async function sendGotifyNotification(body, title, type, url, thumbnail) {
async function sendGotifyNotification({body, title, type, url, thumbnail}) {
logger.verbose('Sending notification to gotify');
await gotify({
server: config_api.getConfigItem('ytdl_gotify_server_url'),
Expand All @@ -127,11 +135,23 @@ async function sendGotifyNotification(body, title, type, url, thumbnail) {
});
}

async function sendTelegramNotification(body, title, type, url, thumbnail) {
async function sendTelegramNotification({body, title, type, url, thumbnail}) {
logger.verbose('Sending notification to Telegram');
const bot_token = config_api.getConfigItem('ytdl_telegram_bot_token');
const chat_id = config_api.getConfigItem('ytdl_telegram_chat_id');
const bot = new TelegramBot(bot_token);
if (thumbnail) await bot.sendPhoto(chat_id, thumbnail);
bot.sendMessage(chat_id, `<b>${title}</b>\n\n${body}\n<a href="${url}">${url}</a>`, {parse_mode: 'HTML'});
}

function sendGenericNotification(data) {
const webhook_url = config_api.getConfigItem('ytdl_webhook_url');
logger.verbose(`Sending generic notification to ${webhook_url}`);
fetch(webhook_url, {
method: 'POST',
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data),
});
}
7 changes: 7 additions & 0 deletions src/app/settings/settings.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,13 @@ <h6 i18n="Records per table label">Records per table</h6>
</mat-select>
</mat-form-field>
</div>
<div class="col-12 mb-2 mt-2">
<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-form-field>
</div>
<div class="col-12 mt-3">
<mat-checkbox color="accent" [disabled]="!new_config['Extra']['enable_notifications']" [(ngModel)]="new_config['API']['use_ntfy_API']"><ng-container i18n="Use ntfy API setting">Use ntfy API</ng-container></mat-checkbox>
</div>
Expand Down

0 comments on commit fb27264

Please sign in to comment.