TypeScript library for parsing Telegram Desktop's data export with full type safety
npm install @stack.thefennec.dev/telegram-export-parser> Production-ready TypeScript library for parsing Telegram Desktop's data exports with complete type safety.
!TypeScript
!License: MIT
!Standards
!Code Quality
!Clean Code
!Functional
- 🎯 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
``typescript
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())
})
}
})
`
`typescript
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)
`
`typescript`
// 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()) // text
break
case 'text_link':
console.log('Link:', entity.url)
console.log('Markdown:', entity.toMarkdown()) // text
break
case 'mention':
console.log('User mention:', entity.toHTML()) // @username
break
}
})
})
`typescript
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})${sender.displayName} (${sender.type}): ID ${id}
exportData.participants.forEach((sender, id) => {
console.log()`
})
| 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:
`typescript`
entity.toMarkdown() // Convert to Markdown format
entity.toHTML() // Convert to HTML format
`typescript`
interface TelegramChatExport {
conversation: Conversation
participants: Map
messages: (TelegramMessage | TelegramEvent)[]
totalMessages: number
dateRange: {
earliest: Date
latest: Date
}
}
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.
`bash`
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 development npm 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 🦊📡