| 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; |
|
|
| |
| let db = { |
| groups: {} |
| }; |
|
|
| 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("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}` } |
| } |
| ]] |
| } |
| }); |
| }); |
|
|
| |
| 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}`); |
| } |
| }); |
|
|
| |
| app.use(express.static("public")); |
| app.use(express.json()); |
|
|
| |
| 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); |
| }); |
|
|
| |
| 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 }); |
| }); |
|
|
| |
| 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); |
| }); |
|
|
| |
| 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" }); |
| } |
| }); |
|
|
| |
| app.get("/dashboard", (req, res) => { |
| res.sendFile(path.join(__dirname, "public", "dashboard.html")); |
| }); |
|
|
| |
| bot.launch().then(() => console.log("π€ Bot started")); |
| app.listen(PORT, () => console.log(`π Web server running on http://localhost:${PORT}`)); |