File size: 3,826 Bytes
5ff288f | 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | const { Telegraf } = require("telegraf");
const express = require("express");
const fs = require("fs");
const path = require("path");
const BOT_TOKEN = "7542813790:AAH7eocL5icQvVe8tRsDEqEUPGjgaAoqvQM";
const bot = new Telegraf(BOT_TOKEN);
const app = express();
const PORT = 7860;
// === Load DB (per-group settings and logs) ===
let db = {
groups: {} // { [groupId]: { settings, logs } }
};
const saveDb = () => fs.writeFileSync("db.json", JSON.stringify(db, null, 2));
try {
db = JSON.parse(fs.readFileSync("db.json"));
} catch { saveDb(); }
const initGroup = (groupId) => {
if (!db.groups[groupId]) {
db.groups[groupId] = {
settings: { antilink: true },
logs: []
};
saveDb();
}
};
const log = (groupId, msg) => {
initGroup(groupId);
db.groups[groupId].logs.unshift({ msg, time: new Date().toISOString() });
db.groups[groupId].logs = db.groups[groupId].logs.slice(0, 50);
saveDb();
};
// === BOT COMMAND: SEND DASHBOARD BUTTON ===
bot.command("menu", (ctx) => {
if (ctx.chat.type !== "supergroup" && ctx.chat.type !== "group") return;
initGroup(ctx.chat.id);
ctx.reply("π§ Open the admin dashboard:", {
reply_markup: {
inline_keyboard: [[
{
text: "βοΈ Open Dashboard",
web_app: { url: `https://yourdomain.com/dashboard?group=${ctx.chat.id}` }
}
]]
}
});
});
// === WELCOME + LINK FILTER ===
bot.on("new_chat_members", (ctx) => {
const groupId = ctx.chat.id;
initGroup(groupId);
const name = ctx.message.new_chat_members[0].first_name;
ctx.reply(`π Welcome, ${name}`);
log(groupId, `User joined: ${name}`);
});
bot.on("message", (ctx) => {
const groupId = ctx.chat.id;
if (ctx.chat.type !== "supergroup" && ctx.chat.type !== "group") return;
initGroup(groupId);
const msg = ctx.message.text || "";
if (db.groups[groupId].settings.antilink && msg.match(/https?:\/\/|t\.me/gi)) {
ctx.deleteMessage();
ctx.reply("π« Links are not allowed!");
log(groupId, `Deleted link from ${ctx.from.username || ctx.from.id}`);
}
});
// === EXPRESS CONFIG ===
app.use(express.static("public"));
app.use(express.json());
// === API: GET SETTINGS FOR GROUP ===
app.get("/api/settings", (req, res) => {
const groupId = req.query.group;
if (!groupId || !db.groups[groupId]) return res.status(404).json({ error: "Group not found" });
res.json(db.groups[groupId].settings);
});
// === API: TOGGLE ANTI-LINK FOR GROUP ===
app.post("/api/settings/antilink", (req, res) => {
const { group, enabled } = req.body;
if (!group || !db.groups[group]) return res.status(404).json({ error: "Group not found" });
db.groups[group].settings.antilink = !!enabled;
saveDb();
res.json({ success: true });
});
// === API: GET LOGS FOR GROUP ===
app.get("/api/logs", (req, res) => {
const groupId = req.query.group;
if (!groupId || !db.groups[groupId]) return res.status(404).json({ error: "Group not found" });
res.json(db.groups[groupId].logs);
});
// === API: CHECK IF USER IS ADMIN IN GROUP ===
app.post("/api/check-admin", async (req, res) => {
const { telegram_id, group } = req.body;
if (!group) return res.status(400).json({ error: "Missing group ID" });
try {
const admins = await bot.telegram.getChatAdministrators(group);
const isAdmin = admins.some(a => a.user.id === telegram_id);
res.json({ admin: isAdmin });
} catch (err) {
console.error("Admin check failed:", err);
res.status(500).json({ error: "Failed to check admin" });
}
});
// === FALLBACK ROUTE ===
app.get("/dashboard", (req, res) => {
res.sendFile(path.join(__dirname, "public", "dashboard.html"));
});
// === START ===
bot.launch().then(() => console.log("π€ Bot started"));
app.listen(PORT, () => console.log(`π Web server running on http://localhost:${PORT}`)); |