feat(apbdes): finalize modernization and update config
This commit is contained in:
4
.env
4
.env
@@ -15,5 +15,5 @@ BASE_SESSION_KEY=kp9sGx91as0Kj2Ls81nAsl2Kdj13KsxP
|
|||||||
BASE_TOKEN_KEY=Qm82JsA92lMnKw0291mxKaaP02KjslaA
|
BASE_TOKEN_KEY=Qm82JsA92lMnKw0291mxKaaP02KjslaA
|
||||||
|
|
||||||
# BOT-TELE
|
# BOT-TELE
|
||||||
BOT_TOKEN=8498428675:AAEQwAUjTqpvgyyC5C123nP1mAxhOg12Ph0
|
BOT_TOKEN=8479423145:AAE9ArrOgTD3DyVxYSVs3IXN40u_sL6c9sw
|
||||||
CHAT_ID=5251328671
|
CHAT_ID=-1003368982298
|
||||||
@@ -1,43 +1,52 @@
|
|||||||
#!/usr/bin/env bun
|
#!/usr/bin/env bun
|
||||||
import { readFileSync } from "node:fs";
|
import { readFileSync, existsSync } from "node:fs";
|
||||||
|
import { join } from "node:path";
|
||||||
|
|
||||||
// Fungsi untuk mencari string terpanjang dalam objek (biasanya balasan AI)
|
// Function to manually load .env from project root if process.env is missing keys
|
||||||
function findLongestString(obj: any): string {
|
function loadEnv() {
|
||||||
let longest = "";
|
const envPath = join(process.cwd(), ".env");
|
||||||
const search = (item: any) => {
|
if (existsSync(envPath)) {
|
||||||
if (typeof item === "string") {
|
const envContent = readFileSync(envPath, "utf-8");
|
||||||
if (item.length > longest.length) longest = item;
|
const lines = envContent.split("\n");
|
||||||
} else if (Array.isArray(item)) {
|
for (const line of lines) {
|
||||||
item.forEach(search);
|
if (line && !line.startsWith("#")) {
|
||||||
} else if (item && typeof item === "object") {
|
const [key, ...valueParts] = line.split("=");
|
||||||
Object.values(item).forEach(search);
|
if (key && valueParts.length > 0) {
|
||||||
|
const value = valueParts.join("=").trim().replace(/^["']|["']$/g, "");
|
||||||
|
process.env[key.trim()] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
search(obj);
|
|
||||||
return longest;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function run() {
|
async function run() {
|
||||||
try {
|
try {
|
||||||
|
// Ensure environment variables are loaded
|
||||||
|
loadEnv();
|
||||||
|
|
||||||
const inputRaw = readFileSync(0, "utf-8");
|
const inputRaw = readFileSync(0, "utf-8");
|
||||||
if (!inputRaw) return;
|
if (!inputRaw) return;
|
||||||
const input = JSON.parse(inputRaw);
|
|
||||||
|
|
||||||
// DEBUG: Lihat struktur asli di console terminal (stderr)
|
let finalText = "";
|
||||||
console.error("DEBUG KEYS:", Object.keys(input));
|
let sessionId = "web-desa-darmasaba";
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Try parsing as JSON first
|
||||||
|
const input = JSON.parse(inputRaw);
|
||||||
|
sessionId = input.session_id || "web-desa-darmasaba";
|
||||||
|
finalText = typeof input === "string" ? input : (input.response || input.text || JSON.stringify(input));
|
||||||
|
} catch {
|
||||||
|
// If not JSON, use raw text
|
||||||
|
finalText = inputRaw;
|
||||||
|
}
|
||||||
|
|
||||||
const BOT_TOKEN = process.env.BOT_TOKEN;
|
const BOT_TOKEN = process.env.BOT_TOKEN;
|
||||||
const CHAT_ID = process.env.CHAT_ID;
|
const CHAT_ID = process.env.CHAT_ID;
|
||||||
|
|
||||||
const sessionId = input.session_id || "unknown";
|
if (!BOT_TOKEN || !CHAT_ID) {
|
||||||
|
console.error("Missing BOT_TOKEN or CHAT_ID in environment variables");
|
||||||
// Cari teks secara otomatis di seluruh objek JSON
|
return;
|
||||||
let finalText = findLongestString(input.response || input);
|
|
||||||
|
|
||||||
if (!finalText || finalText.length < 5) {
|
|
||||||
finalText =
|
|
||||||
"Teks masih gagal diekstraksi. Struktur: " +
|
|
||||||
Object.keys(input).join(", ");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const message =
|
const message =
|
||||||
@@ -45,7 +54,7 @@ async function run() {
|
|||||||
`🆔 Session: \`${sessionId}\` \n\n` +
|
`🆔 Session: \`${sessionId}\` \n\n` +
|
||||||
`🧠 Output:\n${finalText.substring(0, 3500)}`;
|
`🧠 Output:\n${finalText.substring(0, 3500)}`;
|
||||||
|
|
||||||
await fetch(`https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`, {
|
const res = await fetch(`https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
@@ -55,6 +64,13 @@ async function run() {
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
const errorData = await res.json();
|
||||||
|
console.error("Telegram API Error:", errorData);
|
||||||
|
} else {
|
||||||
|
console.log("Notification sent successfully!");
|
||||||
|
}
|
||||||
|
|
||||||
process.stdout.write(JSON.stringify({ status: "continue" }));
|
process.stdout.write(JSON.stringify({ status: "continue" }));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Hook Error:", err);
|
console.error("Hook Error:", err);
|
||||||
|
|||||||
Reference in New Issue
Block a user