filestream-ts / src /bot /handlers.ts
vickydmt's picture
Upload folder using huggingface_hub
f1ae502 verified
import { Api } from "telegram";
import { Button } from "telegram/tl/custom/button";
import { client } from "./client";
import { config, getBaseUrl } from "../config";
import { NewMessage, NewMessageEvent } from "telegram/events";
export const setupHandlers = () => {
client.addEventHandler(async (event: NewMessageEvent) => {
const message = event.message;
// Reject non-media messages
if (!message.media) return;
if (message.isPrivate) {
if (!message.document && !message.photo && !message.video) return;
try {
// Forward the message to the BIN channel
const forwarded = await client.invoke(new Api.messages.ForwardMessages({
fromPeer: message.peerId,
id: [message.id],
toPeer: config.binChannel,
randomId: [require("big-integer")(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER))]
}));
const newMsgId = 'updates' in forwarded
? (forwarded.updates as any[]).find((u: any) => u.message)?.message?.id
: null;
if (!newMsgId) throw new Error("Could not get stored message ID in Bin Channel");
// Stateless URL Encoding: Base64 encode the precise Message ID in the Bin Channel
// We use base64url to ensure it is URL-safe
const fileId = Buffer.from(newMsgId.toString()).toString("base64url");
const downloadUrl = `${getBaseUrl()}/download/${fileId}`;
const watchUrl = `${getBaseUrl()}/watch/${fileId}`;
await message.reply({
message: `**Your Stream Links Are Ready!**\n\n**File ID**: \`${fileId}\`\n\nClick the buttons below to Watch online or Download directly.`,
buttons: [
[
Button.url("📥 Download", downloadUrl),
Button.url("🍿 Watch", watchUrl)
]
]
});
} catch (err: any) {
console.error("Error archiving file: ", err);
await message.reply({ message: `Error processing file: ${err.message}` });
}
}
}, new NewMessage({ incoming: true }));
};