@gonzih/cc-tg 0.2.3 → 0.2.5

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/README.md CHANGED
@@ -53,6 +53,8 @@ Message [@userinfobot](https://t.me/userinfobot) on Telegram — it replies with
53
53
  | `/cron clear` | Remove all cron jobs |
54
54
  | Any text | Sent directly to Claude Code |
55
55
  | Voice message | Transcribed via whisper.cpp and sent to Claude |
56
+ | Photo | Sent as native image input to Claude (base64 content block) |
57
+ | Document / file | Downloaded to `<CWD>/.cc-tg/uploads/`, path passed to Claude as `ATTACHMENTS: [name](path)` |
56
58
 
57
59
  ## Features
58
60
 
@@ -62,6 +64,12 @@ Each Telegram chat ID gets its own isolated Claude Code subprocess. Sessions sur
62
64
  ### Voice messages
63
65
  Send a voice message → automatically transcribed via whisper.cpp → fed into Claude as text. Requires `whisper-cpp` and `ffmpeg` installed on the host.
64
66
 
67
+ ### Images
68
+ Send a photo → downloaded and base64-encoded → sent to Claude as a native image content block via the stream-JSON protocol. Claude sees the full image, no intermediate vision step. Caption (if any) is included as text alongside the image.
69
+
70
+ ### Documents
71
+ Send any file as a document → downloaded to `<CWD>/.cc-tg/uploads/<filename>` → Claude receives the path as `ATTACHMENTS: [filename](path)` and can read/process it directly. Works for PDFs, CSVs, code files, etc.
72
+
65
73
  ### File delivery
66
74
  When Claude writes a file and mentions it in the response, the bot automatically uploads it to Telegram. Hybrid detection: tracks `Write`/`Edit` tool calls during the session, cross-references with filenames mentioned in the final response.
67
75
 
package/dist/bot.d.ts CHANGED
@@ -18,12 +18,14 @@ export declare class CcTgBot {
18
18
  private handleTelegram;
19
19
  private handleVoice;
20
20
  private handlePhoto;
21
+ private handleDocument;
21
22
  private getOrCreateSession;
22
23
  private handleClaudeMessage;
23
24
  private startTyping;
24
25
  private stopTyping;
25
26
  private flushPending;
26
27
  private trackWrittenFiles;
28
+ private isSensitiveFile;
27
29
  private uploadMentionedFiles;
28
30
  private extractToolName;
29
31
  private handleCron;
package/dist/bot.js CHANGED
@@ -3,8 +3,8 @@
3
3
  * One ClaudeProcess per chat_id — sessions are isolated per user.
4
4
  */
5
5
  import TelegramBot from "node-telegram-bot-api";
6
- import { existsSync } from "fs";
7
- import { resolve, basename } from "path";
6
+ import { existsSync, createWriteStream, mkdirSync } from "fs";
7
+ import { resolve, basename, join } from "path";
8
8
  import https from "https";
9
9
  import http from "http";
10
10
  import { ClaudeProcess, extractText } from "./claude.js";
@@ -60,6 +60,11 @@ export class CcTgBot {
60
60
  await this.handlePhoto(chatId, msg);
61
61
  return;
62
62
  }
63
+ // Document — download to CWD/.cc-tg/uploads/, tell Claude the path
64
+ if (msg.document) {
65
+ await this.handleDocument(chatId, msg);
66
+ return;
67
+ }
63
68
  const text = msg.text?.trim();
64
69
  if (!text)
65
70
  return;
@@ -147,6 +152,31 @@ export class CcTgBot {
147
152
  await this.bot.sendMessage(chatId, `Failed to process image: ${err.message}`);
148
153
  }
149
154
  }
155
+ async handleDocument(chatId, msg) {
156
+ const doc = msg.document;
157
+ const caption = msg.caption?.trim();
158
+ const fileName = doc.file_name ?? `file_${doc.file_id}`;
159
+ console.log(`[doc:${chatId}] received document file_name=${fileName} mime=${doc.mime_type}`);
160
+ this.bot.sendChatAction(chatId, "typing").catch(() => { });
161
+ try {
162
+ const uploadsDir = join(this.opts.cwd ?? process.cwd(), ".cc-tg", "uploads");
163
+ mkdirSync(uploadsDir, { recursive: true });
164
+ const destPath = join(uploadsDir, fileName);
165
+ const fileLink = await this.bot.getFileLink(doc.file_id);
166
+ await downloadToFile(fileLink, destPath);
167
+ console.log(`[doc:${chatId}] saved to ${destPath}`);
168
+ const prompt = caption
169
+ ? `${caption}\n\nATTACHMENTS: [${fileName}](${destPath})`
170
+ : `ATTACHMENTS: [${fileName}](${destPath})`;
171
+ const session = this.getOrCreateSession(chatId);
172
+ session.claude.sendPrompt(prompt);
173
+ this.startTyping(chatId, session);
174
+ }
175
+ catch (err) {
176
+ console.error(`[doc:${chatId}] error:`, err.message);
177
+ await this.bot.sendMessage(chatId, `Failed to receive document: ${err.message}`);
178
+ }
179
+ }
150
180
  getOrCreateSession(chatId) {
151
181
  const existing = this.sessions.get(chatId);
152
182
  if (existing && !existing.claude.exited)
@@ -276,6 +306,16 @@ export class CcTgBot {
276
306
  session.writtenFiles.add(resolved);
277
307
  }
278
308
  }
309
+ isSensitiveFile(filePath) {
310
+ const name = basename(filePath).toLowerCase();
311
+ const sensitivePatterns = [
312
+ /credential/i, /secret/i, /password/i, /passwd/i, /\.env/i,
313
+ /api[_-]?key/i, /token/i, /private[_-]?key/i, /id_rsa/i,
314
+ /\.pem$/i, /\.key$/i, /\.pfx$/i, /\.p12$/i,
315
+ /gmail/i, /oauth/i, /auth/i,
316
+ ];
317
+ return sensitivePatterns.some((p) => p.test(name));
318
+ }
279
319
  uploadMentionedFiles(chatId, resultText, session) {
280
320
  if (session.writtenFiles.size === 0)
281
321
  return;
@@ -306,9 +346,13 @@ export class CcTgBot {
306
346
  }
307
347
  }
308
348
  }
309
- // Deduplicate
349
+ // Deduplicate and filter sensitive files
310
350
  const unique = [...new Set(toUpload)];
311
351
  for (const filePath of unique) {
352
+ if (this.isSensitiveFile(filePath)) {
353
+ console.log(`[claude:files] skipping sensitive file: ${filePath}`);
354
+ continue;
355
+ }
312
356
  console.log(`[claude:files] uploading to telegram: ${filePath}`);
313
357
  this.bot.sendDocument(chatId, filePath).catch((err) => console.error(`[tg:${chatId}] sendDocument failed for ${filePath}:`, err.message));
314
358
  }
@@ -395,6 +439,18 @@ function fetchAsBase64(url) {
395
439
  }).on("error", reject);
396
440
  });
397
441
  }
442
+ /** Download a URL to a local file path */
443
+ function downloadToFile(url, destPath) {
444
+ return new Promise((resolve, reject) => {
445
+ const client = url.startsWith("https") ? https : http;
446
+ const file = createWriteStream(destPath);
447
+ client.get(url, (res) => {
448
+ res.pipe(file);
449
+ file.on("finish", () => file.close(() => resolve()));
450
+ file.on("error", reject);
451
+ }).on("error", reject);
452
+ });
453
+ }
398
454
  function splitMessage(text, maxLen = 4096) {
399
455
  if (text.length <= maxLen)
400
456
  return [text];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gonzih/cc-tg",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "description": "Claude Code Telegram bot — chat with Claude Code via Telegram",
5
5
  "type": "module",
6
6
  "bin": {