Skip to content

Commit 7086ad4

Browse files
committed
feat: Webhook notifications via ntfy.sh
1 parent b3a6f9b commit 7086ad4

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

src/server/routes/domain-updater/lib/notify.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
// server/api/trigger-updates/lib/notifyIfEnabled.ts
2-
31
import { callPgExecutor } from './pgExecutor';
4-
// import { Logger } from './logger';
5-
6-
// const log = new Logger('notify');
2+
import { sendWebhookNotification } from './sendWebhookNotification';
73

84
/**
95
* Check if a notification should be sent for a changeType, and insert it if so.
@@ -31,6 +27,7 @@ export async function notifyUser(
3127
);
3228

3329
if (!isEnabled) {
30+
console.info(`Skipping notification for ${changeType}, because not enabled for this domain`);
3431
return;
3532
}
3633

@@ -44,13 +41,14 @@ export async function notifyUser(
4441
[userId, domainId, changeType, message || null]
4542
);
4643

47-
// Placeholder: Webhook / external dispatch
48-
// log.info(`📤 Notification queued: domain=${domainId}, type=${changeType}`);
49-
50-
console.log(`Notification queued: domain=${domainId}, type=${changeType}`);
44+
// Send webhook notification
45+
await sendWebhookNotification(
46+
message || `Change detected: ${changeType}`,
47+
'Domain Locker Update',
48+
[changeType]
49+
);
5150

52-
// TODO: Dispatch webhook or send notification to user
5351
} catch (err: any) {
54-
// log.error(`Failed to insert notification for ${changeType}: ${err.message}`);
52+
console.error(`Failed to insert notification for ${changeType}: ${err.message}`);
5553
}
5654
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { getEnvVar } from './utils';
2+
3+
export async function sendWebhookNotification(
4+
message: string,
5+
title = 'Domain Locker',
6+
tags?: string[]
7+
): Promise<void> {
8+
const base = getEnvVar('NOTIFY_WEBHOOK_BASE');
9+
const topic = getEnvVar('NOTIFY_WEBHOOK_TOPIC');
10+
const token = getEnvVar('NOTIFY_WEBHOOK_TOKEN', '');
11+
12+
if (!base || !topic) {
13+
console.log('Webhook notification skipped (missing config)');
14+
return;
15+
}
16+
17+
try {
18+
const res = await fetch(`${base.replace(/\/$/, '')}/${topic}`, {
19+
method: 'POST',
20+
headers: {
21+
'Content-Type': 'text/plain',
22+
...(token ? { Authorization: `Bearer ${token}` } : {})
23+
},
24+
body: message
25+
});
26+
27+
if (!res.ok) {
28+
throw new Error(`Failed with status ${res.status}`);
29+
}
30+
31+
console.info(`📨 Webhook sent: ${title} - ${message}`);
32+
} catch (err: any) {
33+
console.error(`❌ Webhook failed: ${err.message}`);
34+
}
35+
}

0 commit comments

Comments
 (0)