snapshot before merge
This commit is contained in:
1 parent
2b3b2850f9
commit
21fc65b3ae
145 files changed
+9786
-59
No files matched your search
@@ -0,0 +1,54 @@
|
||||
// tg-core.js - Telegram API helpers
|
||||
const config = require("./config");
|
||||
|
||||
const API = config.TG_TOKEN ? "https://api.telegram.org/bot" + config.TG_TOKEN : "";
|
||||
|
||||
async function tg(method, body) {
|
||||
const res = await fetch(API + "/" + method, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
return res.json();
|
||||
}
|
||||
|
||||
function allowed(chatId) {
|
||||
if (config.ALLOWED_IDS.length === 0) return true;
|
||||
return config.ALLOWED_IDS.includes(chatId);
|
||||
}
|
||||
|
||||
async function sendMessage(chatId, text, opts) {
|
||||
const safeText = String(text || "");
|
||||
try {
|
||||
return await tg("sendMessage", Object.assign(
|
||||
{ chat_id: chatId, text: safeText, parse_mode: "HTML", disable_web_page_preview: true },
|
||||
opts || {}
|
||||
));
|
||||
} catch (e) {
|
||||
if (e.message && (e.message.includes("parse") || e.message.includes("HTML"))) {
|
||||
return await tg("sendMessage", Object.assign(
|
||||
{ chat_id: chatId, text: safeText },
|
||||
opts || {}
|
||||
));
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
async function handleCallback(chatId, queryId, data, handlers) {
|
||||
if (!data) return;
|
||||
const parts = data.split("_");
|
||||
const cmd = parts[0];
|
||||
const page = parseInt(parts[1]) || 1;
|
||||
|
||||
if (cmd === "list" && handlers.list) {
|
||||
await handlers.list(chatId, String(page));
|
||||
} else if (cmd === "links" && handlers.links) {
|
||||
await handlers.links(chatId, String(page));
|
||||
} else if (cmd === "feeds" && handlers.feeds) {
|
||||
await handlers.feeds(chatId, String(page));
|
||||
}
|
||||
try { await tg("answerCallbackQuery", { callback_query_id: queryId }); } catch {}
|
||||
}
|
||||
|
||||
module.exports = { tg, allowed, sendMessage, handleCallback };
|
||||
Reference in new issue
Block a user