A powerful CLI tool to analyze Telegram result.json exports using AI. It identifies relevant messages based on your custom criteria and generates actionable output (like draft emails or summaries) in both CSV and visual HTML formats.
Note: Currently, this tool exclusively supports Google Gemini models.
- Dynamic Analysis: Fully customizable analysis criteria and output format via natural language prompts.
- Multimodal Support: Automatically uploads and analyzes photos attached to messages.
- Batch Processing: Groups messages into batches (e.g., 10 at a time) to maximize API throughput and stay within free tier limits.
- Dual Logging:
- analysis_log.csv: Machine-readable data for further processing or spreadsheet analysis.
- analysis_log.html: A professional, visual dashboard with image previews, color-coded results, and built-in filtering/sorting capabilities.
- Optimized Rate Limiting: Built-in retry logic and smart handling of
429 Too Many Requestserrors, including automatic waits based on API feedback. - Progress Tracking: Persistent state via
resume.jsonallows the script to pick up exactly where it left off. - Modular Architecture: Clean, extensible TypeScript codebase.
βββ src/
β βββ services/
β β βββ Analyzer.ts # Core AI logic and batch processing
β β βββ Logger.ts # CSV and HTML report generation
β βββ types.ts # Centralized TypeScript interfaces
β βββ utils.ts # Text parsing and escaping helpers
βββ index.ts # CLI entry point and orchestration
βββ .env.example # Template for environment variables
βββ package.json # Dependencies and scripts
βββ tsconfig.json # TypeScript configuration
-
Clone and Install:
npm install
-
Configure Environment: Create a
.envfile in the root directory. You can use the following template:GOOGLE_GENAI_API_KEY=your_api_key_here TELEGRAM_EXPORT_PATH=./DataExport_Chat ANALYSIS_PROMPT="Your custom analysis prompt" MODEL_NAME=gemini-2.5-flash-lite BATCH_SIZE=10 WAIT_TIME=5000 CONCURRENCY_LIMIT=1
To use this tool, you must export your chat data in JSON format from Telegram Desktop:
- Open Telegram Desktop (not available on mobile/web).
- Go to Settings > Advanced > Export Telegram data.
- In the export settings:
- Select Personal chats or Groups.
- Photos: Ensure "Photos" is selected if you want to analyze images.
- Format (CRITICAL): Change "Format" from "HTML" to JSON.
- Click Export.
- The export folder (e.g.,
ChatExport_2024-01-27) containsresult.json. Use this folder path as yourTELEGRAM_EXPORT_PATH.
The tool accepts configuration via command-line arguments or .env variables. Command-line arguments always take priority.
npx tsx index.ts [path] "[prompt]" [api_key] [model_name] [batch_size] [wait_time] [concurrency_limit]Option 1: Full CLI Arguments
npx tsx index.ts ./MyChat "Analyze for job openings" MY_API_KEY gemini-2.5-flash-lite 10 5000 2Option 2: Using .env (Recommended)
npm run analyzeOption 3: Resetting Progress (Start Over)
If you want to ignore resume.json and recreate your analysis logs from scratch:
npm run analyze:resetNote: This will delete your existing resume.json, analysis_log.csv, and analysis_log.html.
| Parameter | Env Variable | Mandatory | Description |
|---|---|---|---|
path |
TELEGRAM_EXPORT_PATH |
Yes | Path to the Telegram export folder. |
prompt |
ANALYSIS_PROMPT |
Yes | Instructions for the AI. |
api_key |
GOOGLE_GENAI_API_KEY |
Yes | Your Google AI Studio API Key. |
model_name |
MODEL_NAME |
Yes | The Gemini model (e.g., gemini-1.5-flash). |
batch_size |
BATCH_SIZE |
No | Messages per request (Default: 10). |
wait_time |
WAIT_TIME |
No | Delay in ms between batches (Default: 5000). |
concurrency_limit |
CONCURRENCY_LIMIT |
No | Simultaneous batch requests (Default: 1). |
--reset |
- | No | Ignore progress and recreate log files. |
"Analyze if the message contains job openings for Full Stack Engineer roles. Requirements: Mid-level or higher; Remote only. Generate a draft application email for each role found."
"Check if the message contains important technical announcements. If yes, provide a one-sentence summary for a weekly report."
"Identify if the message mentions any price or cost. If it does, extract the amount and the item purchased in a format like 'Item: [Item] | Cost: [Amount]'."
- Rate Limits (429 Error): If you hit a rate limit, the script will automatically pause and retry. If you hit your daily quota, the script will save progress and exit.
- Resuming: Progress is saved in
resume.json. If you want to restart the analysis from the beginning, delete this file. - Image Errors: Ensure the
photos/directory is present in your export folder if your prompt requires image analysis. - JSON Parsing: The script uses robust regex to extract JSON from AI responses, but very complex or malformed AI output might occasionally fail. These failures are logged and the batch is retried up to 3 times.
This project is licensed under the ISC License.