The bot now uses Telegram's context storage system instead of a local SQLite database for user-specific data. This ensures:
✅ Per-user isolation - Each user's data is completely separate
✅ Easy data management - Users can clear their data with /cleardata
✅ No data mixing - Multiple users' data never overlaps
✅ Persistent storage - Data is preserved between bot restarts
- Uses
context.user_datadictionary for each user - Automatically persists to
telegram_user_data/bot_persistence.pkl - Each user's data is indexed by their Telegram user ID
- Bot-wide stats (total users) stored in
context.bot_data
context.user_data = {
'initialized': True,
'bookmarks': [
{
'url': 'https://example.com',
'title': 'Example Site',
'created_at': '2026-01-24T...',
'last_scan': '2026-01-24T...',
'last_status': 'AVAILABLE'
}
],
'scan_history': [
{
'url': 'https://example.com',
'results': {...},
'timestamp': '2026-01-24T...'
}
],
'preferences': {
'show_detailed': True,
'auto_bookmark': False,
'notification_enabled': True
},
'first_seen': '2026-01-24T...',
'last_seen': '2026-01-24T...'
}- Initializes user data structure if first time
- Increments total user count
- Shows real-time status with:
- Bot uptime
- Total registered users
- Scanner count
- Shows confirmation dialog
- Permanently deletes all user data:
- Bookmarks
- Scan history
- Preferences
- Reinitializes fresh data structure
- Lists user's bookmarks
- Shows status indicators (🟢/🔴)
- Data retrieved from
context.user_data['bookmarks']
- Shows user statistics
- Calculates from
context.user_data - No database queries needed
- Manages user preferences
- Stored in
context.user_data['preferences']
- Each user's data is completely separate
- No cross-user data access possible
- User IDs are the only identifier
When user runs /cleardata:
- All their data is deleted from memory
- Fresh data structure is created
- No trace of previous data remains
- Persistence file is updated automatically
- File:
telegram_user_data/bot_persistence.pkl - Excluded from git via
.gitignore - Only accessible by bot process
- Not shared between bot instances
persistence = PicklePersistence(
filepath='telegram_user_data/bot_persistence.pkl',
store_data={
'user_data': True, # Per-user data
'bot_data': True, # Bot-wide stats
'chat_data': False, # Not used
'callback_data': False
}
)- Data is saved automatically by python-telegram-bot
- No manual save operations needed
- Updates persist immediately
- Scan history limited to last 100 scans per user
- Prevents memory bloat
- Automatically trims oldest entries
- ❌ Local SQLite database for user data
- ❌
userstable - ❌
bookmarkstable - ❌
scan_historytable - ❌
user_preferencestable
- ✅ Database class (for future bot-wide stats if needed)
- ✅ Bot initialization and shutdown
- Simpler architecture - No database management
- Better isolation - Built-in per-user separation
- User control - Easy data deletion
- Portable - Single pickle file
- No SQL - Pure Python data structures
- Real-time user count in
/start - Personal data accessible only to them
- Clear data control with
/cleardata - Fast operations (no database queries)
- Their bookmarks
- Their scan history
- Their preferences
- Complete data deletion
- Only bot has access to persistence file
- User data indexed by Telegram user ID
- No external database access needed
- No SQL injection risks
- Data persists until user clears it
- No automatic deletion
- User controls their data lifecycle
- Bot restart preserves all data
To backup user data:
# Copy persistence file
cp telegram_user_data/bot_persistence.pkl telegram_user_data/backup_$(date +%Y%m%d).pklTo restore:
# Restore from backup
cp telegram_user_data/backup_20260124.pkl telegram_user_data/bot_persistence.pkl- Check
telegram_user_data/directory exists - Verify write permissions
- Check bot shutdown gracefully
- Verify persistence file exists
- Check file not corrupted
- User may have run
/cleardata
- Each user limited to 100 scans
- Old scans automatically trimmed
- Consider periodic cleanup task
The new storage system provides:
- ✅ Complete user data isolation
- ✅ Easy data management for users
- ✅ No data mixing between users
- ✅ Simple architecture
- ✅ Better privacy control
- ✅ Persistent storage via Telegram