chore: migrate telegram hook to typescript and update settings

This commit is contained in:
bipproduction
2026-02-10 09:51:35 +08:00
parent 4496094edd
commit 75bc7a65fb
4 changed files with 77 additions and 33 deletions

View File

@@ -1,30 +0,0 @@
#!/usr/bin/env bash
set -e
INPUT=$(cat)
BOT_TOKEN="${BOT_TOKEN:?BOT_TOKEN not set}"
CHAT_ID="${CHAT_ID:?CHAT_ID not set}"
SESSION_ID=$(echo "$INPUT" | jq -r '.session_id // "unknown"')
FINAL_TEXT=$(echo "$INPUT" | jq -r '.final_response.text // ""' | head -c 3500)
MESSAGE=$(cat <<EOF
✅ *Gemini Task Selesai*
🆔 Session:
\`$SESSION_ID\`
🧠 Output (ringkas):
$FINAL_TEXT
EOF
)
curl -s -X POST "https://api.telegram.org/bot${BOT_TOKEN}/sendMessage" \
-d chat_id="$CHAT_ID" \
-d parse_mode="Markdown" \
--data-urlencode text="$MESSAGE" \
> /dev/null
# WAJIB: response JSON ke Gemini
echo '{"decision":"allow"}'

View File

@@ -0,0 +1,62 @@
#!/usr/bin/env bun
import { readFileSync } from "fs";
// Fungsi untuk mencari string terpanjang dalam objek (biasanya balasan AI)
function findLongestString(obj: any): string {
let longest = "";
const search = (item: any) => {
if (typeof item === "string") {
if (item.length > longest.length) longest = item;
} else if (Array.isArray(item)) {
item.forEach(search);
} else if (item && typeof item === "object") {
Object.values(item).forEach(search);
}
};
search(obj);
return longest;
}
async function run() {
try {
const inputRaw = readFileSync(0, "utf-8");
if (!inputRaw) return;
const input = JSON.parse(inputRaw);
// DEBUG: Lihat struktur asli di console terminal (stderr)
console.error("DEBUG KEYS:", Object.keys(input));
const BOT_TOKEN = process.env.BOT_TOKEN;
const CHAT_ID = process.env.CHAT_ID;
const sessionId = input.session_id || "unknown";
// Cari teks secara otomatis di seluruh objek JSON
let finalText = findLongestString(input.response || input);
if (!finalText || finalText.length < 5) {
finalText = "Teks masih gagal diekstraksi. Struktur: " + Object.keys(input).join(", ");
}
const message = `✅ *Gemini Task Selesai*\n\n` +
`🆔 Session: \`${sessionId}\` \n\n` +
`🧠 Output:\n${finalText.substring(0, 3500)}`;
await fetch(`https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
chat_id: CHAT_ID,
text: message,
parse_mode: "Markdown",
}),
});
process.stdout.write(JSON.stringify({ status: "continue" }));
} catch (err) {
console.error("Hook Error:", err);
process.stdout.write(JSON.stringify({ status: "continue" }));
}
}
run();

View File

@@ -7,8 +7,8 @@
{
"name": "telegram-notify",
"type": "command",
"command": "$GEMINI_PROJECT_DIR/.gemini/hooks/telegram-notify.sh",
"timeout": 8000
"command": "bun $GEMINI_PROJECT_DIR/.gemini/hooks/telegram-notify.ts",
"timeout": 10000
}
]
}

12
CHANGELOG.md Normal file
View File

@@ -0,0 +1,12 @@
# Changelog
All notable changes to this project will be documented in this file.
## [Unreleased]
### Changed
- Migrated Telegram notification hook from shell script (`.sh`) to TypeScript (`.ts`) using Bun for better reliability and maintenance.
- Updated `.gemini/settings.json` to use the new TypeScript hook and increased timeout to 10 seconds.
### Removed
- Deleted `.gemini/hooks/telegram-notify.sh`.