Production-ready TypeScript library for parsing Telegram Desktop's data exports with complete type safety.
- π― Full TypeScript Support - Complete type safety for all Telegram entities
- β‘ Functional Programming - Pure functions, immutable data structures
- π¨ Rich Text Processing - Convert entities to Markdown/HTML with methods
- π Memory Efficient - Generator-based processing for large exports
- π‘οΈ Battle Tested - Used in production for 10+ years of chat data
import { parseFromFile } from '@stack.thefennec.dev/telegram-export-parser'
const exportData = parseFromFile('./my-chat.json')
console.log(`Found ${exportData.totalMessages} messages`)
// Access typed message data
exportData.messages.forEach(msg => {
if (msg.textEntities) {
msg.textEntities.forEach(entity => {
console.log('Markdown:', entity.toMarkdown())
console.log('HTML:', entity.toHTML())
})
}
})
import { parseFromFile, parseFromData, parseFromString } from '@stack.thefennec.dev/telegram-export-parser'
// From file
const exportData = parseFromFile('./chat.json')
// From parsed JSON object
const rawData = JSON.parse(jsonString)
const exportData = parseFromData(rawData)
// From JSON string
const exportData = parseFromString(jsonString)
// Rich text entities with built-in rendering methods
exportData.messages.forEach(msg => {
msg.textEntities?.forEach(entity => {
switch (entity.type) {
case 'bold':
console.log('Bold text:', entity.toMarkdown()) // **text**
console.log('HTML:', entity.toHTML()) // <strong>text</strong>
break
case 'text_link':
console.log('Link:', entity.url)
console.log('Markdown:', entity.toMarkdown()) // [text](url)
break
case 'mention':
console.log('User mention:', entity.toHTML()) // <a href="https://t.me/username">@username</a>
break
}
})
})
import { isEvent, hasReactions, isForwarded } from '@stack.thefennec.dev/telegram-export-parser'
const stats = {
totalMessages: exportData.totalMessages,
textMessages: exportData.messages.filter(isTextMessage).length,
mediaMessages: exportData.messages.filter(isMediaMessage).length,
serviceEvents: exportData.messages.filter(isEvent).length,
messagesWithReactions: exportData.messages.filter(hasReactions).length,
forwardedMessages: exportData.messages.filter(isForwarded).length
}
console.log('Chat Statistics:', stats)
// Participants analysis
console.log(`Total participants: ${exportData.participants.size}`)
exportData.participants.forEach((sender, id) => {
console.log(`${sender.displayName} (${sender.type}): ID ${id}`)
})
Function | Description | Returns |
---|---|---|
parseFromFile(filePath) |
Parse Telegram export from file | TelegramChatExport |
parseFromData(data) |
Parse from raw JSON object | TelegramChatExport |
parseFromString(jsonString) |
Parse from JSON string | TelegramChatExport |
Function | Description |
---|---|
isTextMessage(msg) |
Check if message is text |
isMediaMessage(msg) |
Check if message has media |
isPhotoMessage(msg) |
Check if message is photo |
isEvent(msg) |
Check if message is service event |
hasReactions(msg) |
Check if message has reactions |
isForwarded(msg) |
Check if message is forwarded |
Every text entity has built-in rendering methods:
entity.toMarkdown() // Convert to Markdown format
entity.toHTML() // Convert to HTML format
interface TelegramChatExport {
conversation: Conversation
participants: Map<number, MessageSender>
messages: (TelegramMessage | TelegramEvent)[]
totalMessages: number
dateRange: {
earliest: Date
latest: Date
}
}
- β Plain text with rich formatting
- β Bold, italic, underline, strikethrough
- β Code blocks with syntax highlighting
- β Links, mentions, hashtags, bot commands
- β Spoilers and blockquotes
- β Photos with captions and dimensions
- β Videos, animations, and video notes
- β Audio files and voice messages
- β Documents and stickers
- β File metadata (size, name, MIME type)
- β Locations with coordinates and addresses
- β Contacts with phone numbers
- β Polls with voting results
- β Games and invoices
- β Forwarded and saved messages
- β Group creation and management
- β Member additions and removals
- β Phone and video calls
- β Message pins and edits
- β Premium gifts and payments
Optimized for large chat exports:
- Memory efficient: Generator-based processing
- Type safe: Full TypeScript coverage with strict checks
- Fast parsing: Functional programming with pure functions
- Scalable: Handles exports with 100k+ messages
- Node.js 18+
- TypeScript 5.0+ (for development)
Contributions are welcomed! Please see Contributing Guide for details.
git clone https://github.com/stackthefennec/telegram-export-parser.git
cd telegram-export-parser
npm install
npm run dev # Start development mode
npm run build
- Build the packagenpm run dev
- Watch mode developmentnpm run lint
- Check code stylenpm run test
- Run testsnpm run type-check
- Check TypeScript types
MIT License
Copyright (c) 2025 Sam Stack
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Made with π by Stack@theFennec.dev π¦π‘