-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnotify-hook.mjs
More file actions
executable file
·69 lines (57 loc) · 1.89 KB
/
Copy pathnotify-hook.mjs
File metadata and controls
executable file
·69 lines (57 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env node
/**
* Claude Code Stop Hook — Telegram Notification
*
* Sends notification when Claude finishes, based on output mode:
* terminal → no TG notification
* hybrid → notify TG that task is done
* telegram → full response to TG
*/
import { readFileSync } from "fs";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
import { t, loadLang } from "./worker/locale.mjs";
loadLang();
const __dirname = dirname(fileURLToPath(import.meta.url));
const config = JSON.parse(readFileSync(join(__dirname, "config.json"), "utf-8"));
const { botToken, chatId } = config;
const API = `https://api.telegram.org/bot${botToken}`;
const MODE_FILE = "/tmp/claude-output-channel";
function getMode() {
if (process.env.CLAUDE_SOURCE === "telegram") return "telegram";
try { return readFileSync(MODE_FILE, "utf-8").trim(); }
catch { return "terminal"; }
}
async function sendTg(text) {
await fetch(`${API}/sendMessage`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ chat_id: chatId, text, parse_mode: "HTML" }),
});
}
async function main() {
const mode = getMode();
if (mode === "terminal") process.exit(0);
let input;
try {
input = JSON.parse(readFileSync("/dev/stdin", "utf-8"));
} catch {
process.exit(0);
}
const stopReason = input.stop_hook_reason || "unknown";
const transcript = input.transcript_summary || "";
if (mode === "hybrid") {
// Just notify that task is done
await sendTg(`🏁 <b>${t("hook.task_done")}</b>\n<i>${stopReason}</i>`);
} else {
// Full output to TG
const text = [
`🏁 <b>${t("hook.task_done")}</b>`,
``,
`<b>${t("hook.reason")}</b> ${stopReason}`,
transcript ? `\n<code>${transcript.slice(0, 3500)}</code>` : "",
].filter(Boolean).join("\n");
await sendTg(text);
}
}
main().catch(() => process.exit(0));