tgxto / server.js
NexusV1's picture
Create server.js
5ff288f verified
Raw
History Blame Contribute Delete
3.83 kB
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}`));