-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.ts
67 lines (51 loc) · 1.69 KB
/
bot.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { spawn } from "node:child_process";
import { accessSync } from "node:fs";
import { Readable } from "node:stream";
import { Telegraf } from "telegraf";
import { message } from "telegraf/filters";
try {
accessSync("yt-dlp");
} catch {
throw new Error("Please run `npm run download` first");
}
const allowList = process.env.TELEGRAM_BOT_ALLOW_LIST?.split(",") || [];
const token = process.env.TELEGRAM_BOT_TOKEN;
if (!token) {
throw new Error("Missing TELEGRAM_BOT_TOKEN");
}
const bot = new Telegraf(token);
bot.start((ctx) => ctx.reply("hi. send me a link."));
bot.on(message("text"), (ctx) => {
const fromName =
ctx.from.username || ctx.from.first_name || String(ctx.from.id);
if (allowList.length > 0 && !allowList.includes(fromName)) {
ctx.reply("Get out");
return;
}
console.log(
`[${new Date().toISOString()}] @${fromName}: ${ctx.message.text}`,
);
if (!/https?:\/\//i.test(ctx.message.text)) {
ctx.reply("Not sure what you want me to do!");
return;
}
ctx.reply("👀");
const ytdlp = spawn("./yt-dlp", ["-o", "-", ctx.message.text]);
const readableStream = new Readable({
read() {}, // No implementation needed, data is pushed from the child process
});
ytdlp.stdout.on("data", (data) => readableStream.push(data));
ytdlp.stderr.on("data", (data) => console.log(`yt-dlp stderr: ${data}`));
ytdlp.on("close", (code) => {
if (code !== 0) {
ctx.reply(`yt-dlp process exited with code: ${code}`);
}
readableStream.push(null);
});
ctx
.replyWithVideo({ source: readableStream })
.catch((err) => ctx.reply(`Something went wrong: ${err}`));
});
bot.launch();
process.once("SIGINT", () => bot.stop("SIGINT"));
process.once("SIGTERM", () => bot.stop("SIGTERM"));