claude-telegram-bot 0.3.6 → 0.3.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bot.mjs +4 -6
- package/ctb.mjs +39 -1
- package/package.json +1 -1
package/bot.mjs
CHANGED
|
@@ -518,12 +518,10 @@ function runClaude(prompt, sessionId, opts = {}) {
|
|
|
518
518
|
currentChild = null;
|
|
519
519
|
try {
|
|
520
520
|
const j = JSON.parse(out);
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
cost: j.total_cost_usd,
|
|
526
|
-
});
|
|
521
|
+
const text = j.is_error
|
|
522
|
+
? classifyClaudeError(j.result ?? "", code)
|
|
523
|
+
: (j.result ?? "(empty response)");
|
|
524
|
+
resolve({ ok: !j.is_error, text, sessionId: j.session_id, cost: j.total_cost_usd });
|
|
527
525
|
} catch {
|
|
528
526
|
const raw = (err || out || "no output").slice(0, 3500);
|
|
529
527
|
resolve({ ok: false, text: classifyClaudeError(raw, code) });
|
package/ctb.mjs
CHANGED
|
@@ -121,7 +121,45 @@ function main() {
|
|
|
121
121
|
if (sessionId) process.stderr.write(`Resuming session: ${sessionId}\n`);
|
|
122
122
|
|
|
123
123
|
const child = spawn("claude", finalArgs, { stdio: "inherit" });
|
|
124
|
-
child.on("close", (code) =>
|
|
124
|
+
child.on("close", async (code) => {
|
|
125
|
+
cleanup();
|
|
126
|
+
if (sessionId) await notifyTelegram(configPath, sessionId);
|
|
127
|
+
process.exit(code ?? 0);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async function summarizeSession(sid) {
|
|
132
|
+
return new Promise((resolve) => {
|
|
133
|
+
const child = spawn("claude", [
|
|
134
|
+
"--resume", sid,
|
|
135
|
+
"-p", "Summarize what was accomplished in this session in one short sentence. Plain text only, no markdown, no filler. If nothing significant happened, reply with exactly: SKIP",
|
|
136
|
+
"--output-format", "json",
|
|
137
|
+
], { stdio: ["ignore", "pipe", "ignore"] });
|
|
138
|
+
let out = "";
|
|
139
|
+
child.stdout.on("data", (d) => { out += d; });
|
|
140
|
+
child.on("close", () => {
|
|
141
|
+
try {
|
|
142
|
+
const text = (JSON.parse(out).result || "").trim();
|
|
143
|
+
resolve(/^skip$/i.test(text) ? null : text);
|
|
144
|
+
} catch { resolve(null); }
|
|
145
|
+
});
|
|
146
|
+
child.on("error", () => resolve(null));
|
|
147
|
+
setTimeout(() => { child.kill(); resolve(null); }, 30000);
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async function notifyTelegram(configPath, sessionId) {
|
|
152
|
+
try {
|
|
153
|
+
const cfg = JSON.parse(readFileSync(configPath, "utf8"));
|
|
154
|
+
if (!cfg.token || !cfg.allowedChatId || cfg.ctbNotify === false) return;
|
|
155
|
+
const summary = await summarizeSession(sessionId);
|
|
156
|
+
if (!summary) return;
|
|
157
|
+
await fetch(`https://api.telegram.org/bot${cfg.token}/sendMessage`, {
|
|
158
|
+
method: "POST",
|
|
159
|
+
headers: { "content-type": "application/json" },
|
|
160
|
+
body: JSON.stringify({ chat_id: cfg.allowedChatId, text: `💻 ${summary}` }),
|
|
161
|
+
});
|
|
162
|
+
} catch {}
|
|
125
163
|
}
|
|
126
164
|
|
|
127
165
|
main();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "claude-telegram-bot",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.8",
|
|
4
4
|
"description": "Drive Claude Code from Telegram — messages run headless `claude -p` in a project dir and replies come back to the chat. Zero dependencies.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|