Skip to content

Commit 290d560

Browse files
authored
Merge pull request #236 from Lyliya/feat/telegram-notification
feat: Add telegram & discord notification
2 parents 211ff9e + 939fc0b commit 290d560

File tree

6 files changed

+126
-1
lines changed

6 files changed

+126
-1
lines changed

src/lib/Video.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { updatePlex } from "./helpers/updatePlex.js";
2323

2424
import { ProgressHeadless } from "./logging/ProgressConsole.js";
2525
import { ProgressBars } from "./logging/ProgressBars.js";
26+
import { notifyDownloaded, notifyError } from "./notifications/notify.js";
2627
import { nll, withContext } from "./logging/ProgressLogger.js";
2728

2829
import { ffmpegPath } from "./helpers/fetchFFMPEG.js";
@@ -66,7 +67,7 @@ const byteToMbits = 131072;
6667

6768
export class Video extends Attachment {
6869
private readonly description: string;
69-
private readonly artworkUrl?: string;
70+
public readonly artworkUrl?: string;
7071

7172
public static State = VideoState;
7273

@@ -136,9 +137,11 @@ export class Video extends Attachment {
136137
}
137138
this.logger.done(chalk`{cyan Downloaded!}`);
138139
promDownloadedTotal.inc();
140+
notifyDownloaded(this);
139141
break;
140142
} catch (err) {
141143
this.onError(err);
144+
notifyError(message, this);
142145
if (retries < Video.MaxRetries) await sleep(5000);
143146
}
144147
}

src/lib/defaults.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,15 @@ export const defaultSettings: Settings = {
6868
prometheusExporterPort: null,
6969
contributeMetrics: true,
7070
},
71+
notifications: {
72+
telegram: {
73+
enabled: false,
74+
token: "",
75+
chatId: "",
76+
},
77+
discord: {
78+
enabled: false,
79+
webhookUrl: "",
80+
},
81+
},
7182
};

src/lib/notifications/discord.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { settings } from "../helpers/index.js";
2+
import { Video } from "../Video.js";
3+
4+
export const discordSendEmbed = async (title: string, video: Video) => {
5+
const discord = settings.notifications.discord;
6+
if (!discord || !discord.enabled || !discord.webhookUrl || !video) return;
7+
8+
try {
9+
fetch(discord.webhookUrl, {
10+
method: "POST",
11+
headers: {
12+
"Content-Type": "application/json",
13+
},
14+
body: JSON.stringify({
15+
embeds: [
16+
{
17+
title: title,
18+
author: {
19+
name: video.channelTitle,
20+
},
21+
image: {
22+
url: video.artworkUrl,
23+
},
24+
fields: [
25+
{
26+
name: "Title",
27+
value: video.videoTitle,
28+
inline: true,
29+
},
30+
{
31+
name: "Channel",
32+
value: video.channelTitle,
33+
inline: true,
34+
},
35+
],
36+
},
37+
],
38+
}),
39+
});
40+
} catch {
41+
console.error("Failed to send discord notification");
42+
}
43+
};
44+
45+
export const discordSendMessage = async (message: string) => {
46+
const discord = settings.notifications.discord;
47+
if (!discord || !discord.enabled || !discord.webhookUrl || !message) return;
48+
49+
try {
50+
fetch(discord.webhookUrl, {
51+
method: "POST",
52+
headers: {
53+
"Content-Type": "application/json",
54+
},
55+
body: JSON.stringify({
56+
content: message,
57+
}),
58+
});
59+
} catch {
60+
console.error("Failed to send discord notification");
61+
}
62+
};

src/lib/notifications/notify.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Video } from "../Video.js";
2+
import { discordSendEmbed, discordSendMessage } from "./discord.js";
3+
import telegramSendMessage from "./telegram.js";
4+
5+
export const notifyDownloaded = (video: Video) => {
6+
const message = `Downloaded ${video.videoTitle} from ${video.channelTitle}`;
7+
telegramSendMessage(message);
8+
discordSendEmbed("New video downloaded", video);
9+
};
10+
11+
export const notifyError = (error: string, video: Video) => {
12+
const message = `Error downloading ${video.videoTitle} from ${video.channelTitle} : ${error}`;
13+
telegramSendMessage(message);
14+
discordSendMessage(message);
15+
};

src/lib/notifications/telegram.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { settings } from "../helpers/index.js";
2+
3+
export default async function telegramSendMessage(message: string) {
4+
const telegram = settings.notifications.telegram;
5+
if (!telegram || !telegram.enabled || !telegram.token || !telegram.chatId || !message) return;
6+
7+
try {
8+
fetch(`https://api.telegram.org/bot${telegram.token}/sendMessage`, {
9+
method: "POST",
10+
headers: {
11+
"Content-Type": "application/json",
12+
},
13+
body: JSON.stringify({
14+
chat_id: telegram.chatId,
15+
text: message,
16+
}),
17+
});
18+
} catch {
19+
console.error("Failed to send telegram notification");
20+
}
21+
}

src/lib/types.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ export interface Extras extends Record<string, boolean> {
4949

5050
export type Resolution = ValueOfA<Resolutions>;
5151

52+
export type Notification = {
53+
telegram?: {
54+
token: string;
55+
chatId: string;
56+
enabled: boolean;
57+
};
58+
discord?: {
59+
enabled: boolean;
60+
webhookUrl: string;
61+
};
62+
};
63+
5264
export type Settings = {
5365
__SettingsWiki: string;
5466
runQuickstartPrompts: boolean;
@@ -71,4 +83,5 @@ export type Settings = {
7183
prometheusExporterPort: number | null;
7284
contributeMetrics: boolean;
7385
};
86+
notifications: Notification;
7487
};

0 commit comments

Comments
 (0)