Spaces:
Sleeping
Sleeping
File size: 23,498 Bytes
4a21e7e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 | #!/usr/bin/env python3
"""
Daily Telegram Sync Script
===========================
Automatically syncs new messages from Telegram group to the analytics system.
What it does:
1. Connects to Telegram via Telethon
2. Fetches messages from the last 36 hours (12h overlap for safety)
3. Adds new messages to telegram.db (duplicates ignored)
4. Generates embeddings for new messages locally
5. Adds embeddings to embeddings.db (duplicates ignored)
Usage:
First time: python daily_sync.py --setup
Daily run: python daily_sync.py
Custom hours: python daily_sync.py --hours 48
Automation:
Windows Task Scheduler: python daily_sync.py
Linux cron: 0 3 * * * cd /path/to/telegram && python daily_sync.py >> sync.log 2>&1
"""
import os
import sys
import json
import time
import sqlite3
import asyncio
import argparse
import logging
from datetime import datetime, timedelta, timezone
from pathlib import Path
# Setup logging
LOG_FILE = Path(__file__).parent / 'sync.log'
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(message)s',
handlers=[
logging.StreamHandler(),
logging.FileHandler(LOG_FILE, encoding='utf-8')
]
)
log = logging.getLogger('daily_sync')
# Paths
BASE_DIR = Path(__file__).parent
DB_PATH = BASE_DIR / 'telegram.db'
EMBEDDINGS_DB_PATH = BASE_DIR / 'embeddings.db'
SESSION_FILE = BASE_DIR / 'telegram_session'
CONFIG_FILE = BASE_DIR / 'sync_config.json'
# ==========================================
# CONFIGURATION
# ==========================================
def load_config() -> dict:
"""Load configuration from sync_config.json."""
if CONFIG_FILE.exists():
with open(CONFIG_FILE, 'r') as f:
return json.load(f)
return {}
def save_config(config: dict):
"""Save configuration to sync_config.json."""
with open(CONFIG_FILE, 'w') as f:
json.dump(config, f, indent=2)
def setup_config():
"""Interactive setup for first time configuration."""
print("=" * 50)
print(" Telegram Daily Sync - Setup")
print("=" * 50)
print()
print("You need Telegram API credentials.")
print("Get them from: https://my.telegram.org/apps")
print()
api_id = input("API ID: ").strip()
api_hash = input("API Hash: ").strip()
group = input("Group username or ID (e.g., @mygroup or -1001234567890): ").strip()
config = {
'api_id': int(api_id),
'api_hash': api_hash,
'group': group,
'hours': 36
}
save_config(config)
print(f"\nConfiguration saved to {CONFIG_FILE}")
print("Now run: python daily_sync.py")
print("(First run will ask you to log in to Telegram)")
return config
# ==========================================
# TELETHON: FETCH MESSAGES
# ==========================================
def telethon_message_to_json(message) -> dict | None:
"""
Convert a Telethon message object to Telegram Desktop export JSON format.
This ensures compatibility with the existing parse_message() in indexer.py.
"""
from telethon.tl.types import (
MessageEntityUrl, MessageEntityTextUrl,
MessageEntityMention, MessageEntityMentionName,
MessageEntityBold, MessageEntityItalic,
MessageEntityCode, MessageEntityPre,
MessageEntityHashtag, MessageEntityEmail,
MessageEntityPhone, MessageEntityBotCommand,
)
if message.text is None and message.raw_text is None:
return None
text = message.raw_text or ''
if not text.strip():
# Skip empty messages (media-only, service messages, etc.)
return None
# Build text_entities in Telegram Desktop export format
text_entities = []
if message.entities:
for entity in message.entities:
start = entity.offset
end = entity.offset + entity.length
entity_text = text[start:end]
entity_type = 'plain'
if isinstance(entity, (MessageEntityUrl,)):
entity_type = 'link'
elif isinstance(entity, MessageEntityTextUrl):
entity_type = 'text_link'
entity_text = entity.url
elif isinstance(entity, (MessageEntityMention, MessageEntityMentionName)):
entity_type = 'mention'
elif isinstance(entity, MessageEntityBold):
entity_type = 'bold'
elif isinstance(entity, MessageEntityItalic):
entity_type = 'italic'
elif isinstance(entity, (MessageEntityCode, MessageEntityPre)):
entity_type = 'code'
elif isinstance(entity, MessageEntityHashtag):
entity_type = 'hashtag'
elif isinstance(entity, MessageEntityEmail):
entity_type = 'email'
elif isinstance(entity, MessageEntityPhone):
entity_type = 'phone'
elif isinstance(entity, MessageEntityBotCommand):
entity_type = 'bot_command'
if entity_type != 'plain':
text_entities.append({
'type': entity_type,
'text': entity_text
})
# Get sender info
sender = message.sender
from_name = ''
from_id = ''
if sender:
if hasattr(sender, 'first_name'):
# User
parts = [sender.first_name or '']
if sender.last_name:
parts.append(sender.last_name)
from_name = ' '.join(parts).strip()
from_id = f'user{sender.id}'
elif hasattr(sender, 'title'):
# Channel/Group
from_name = sender.title or ''
from_id = f'channel{sender.id}'
# Handle forwarded messages
forwarded_from = None
forwarded_from_id = None
if message.forward:
fwd = message.forward
try:
if fwd.sender:
if hasattr(fwd.sender, 'first_name'):
parts = [fwd.sender.first_name or '']
if fwd.sender.last_name:
parts.append(fwd.sender.last_name)
forwarded_from = ' '.join(parts).strip()
forwarded_from_id = f'user{fwd.sender.id}'
elif hasattr(fwd.sender, 'title'):
forwarded_from = fwd.sender.title
forwarded_from_id = f'channel{fwd.sender.id}'
elif getattr(fwd, 'sender_name', None):
forwarded_from = fwd.sender_name
elif getattr(fwd, 'from_name', None):
forwarded_from = fwd.from_name
except Exception:
pass # Skip forward info if any attribute is missing
# Photo info
photo_info = {}
has_photo = False
has_media = False
if message.photo:
has_photo = True
has_media = True
if hasattr(message.photo, 'sizes') and message.photo.sizes:
largest = message.photo.sizes[-1]
if hasattr(largest, 'w'):
photo_info['width'] = largest.w
photo_info['height'] = largest.h
elif message.document or message.video or message.audio:
has_media = True
# Reply info
reply_to = None
if message.reply_to:
reply_to = message.reply_to.reply_to_msg_id
# Date handling
msg_date = message.date.replace(tzinfo=None) if message.date else datetime.utcnow()
# Build the JSON in Telegram Desktop export format
msg_json = {
'id': message.id,
'type': 'message',
'date': msg_date.strftime('%Y-%m-%dT%H:%M:%S'),
'date_unixtime': str(int(msg_date.replace(tzinfo=timezone.utc).timestamp())),
'from': from_name,
'from_id': from_id,
'text': text,
'text_entities': text_entities,
}
if reply_to:
msg_json['reply_to_message_id'] = reply_to
if forwarded_from:
msg_json['forwarded_from'] = forwarded_from
if forwarded_from_id:
msg_json['forwarded_from_id'] = forwarded_from_id
if has_photo:
msg_json['photo'] = '(photo)'
msg_json.update(photo_info)
if has_media and not has_photo:
msg_json['media_type'] = 'document'
if message.edit_date:
edit_date = message.edit_date.replace(tzinfo=None)
msg_json['edited'] = edit_date.strftime('%Y-%m-%dT%H:%M:%S')
msg_json['edited_unixtime'] = str(int(edit_date.replace(tzinfo=timezone.utc).timestamp()))
return msg_json
async def fetch_messages(config: dict, hours: int = 36) -> list[dict]:
"""
Fetch messages from the last N hours using Telethon.
Returns messages in Telegram Desktop export JSON format.
"""
from telethon import TelegramClient
api_id = config['api_id']
api_hash = config['api_hash']
group = config['group']
client = TelegramClient(str(SESSION_FILE), api_id, api_hash)
await client.start()
log.info(f"Connected to Telegram")
# Resolve group - handle numeric IDs properly
if isinstance(group, str) and group.lstrip('-').isdigit():
group = int(group)
if isinstance(group, int) and group < 0:
# Convert Telegram Web format to Telethon PeerChannel
# -100XXXXXXXXXX → channel_id = XXXXXXXXXX
from telethon.tl.types import PeerChannel
channel_id = int(str(group).replace('-100', ''))
entity = await client.get_entity(PeerChannel(channel_id))
else:
entity = await client.get_entity(group)
log.info(f"Fetching from: {getattr(entity, 'title', group)}")
# Calculate time window
since = datetime.now(timezone.utc) - timedelta(hours=hours)
log.info(f"Fetching messages from last {hours} hours (since {since.strftime('%Y-%m-%d %H:%M')} UTC)")
# Fetch messages
messages_json = []
count = 0
async for message in client.iter_messages(entity, offset_date=None, reverse=False):
# Stop when we've gone past our time window
if message.date < since:
break
count += 1
msg_json = telethon_message_to_json(message)
if msg_json:
messages_json.append(msg_json)
await client.disconnect()
log.info(f"Fetched {count} messages, {len(messages_json)} with text content")
return messages_json
async def fetch_participants(config: dict) -> list[dict]:
"""
Fetch all group participants with metadata using Telethon.
Returns participant info: name, username, status, join date, admin, etc.
"""
from telethon import TelegramClient
from telethon.tl.types import (
UserStatusOnline, UserStatusOffline, UserStatusRecently,
UserStatusLastWeek, UserStatusLastMonth,
ChannelParticipantAdmin, ChannelParticipantCreator,
)
api_id = config['api_id']
api_hash = config['api_hash']
group = config['group']
client = TelegramClient(str(SESSION_FILE), api_id, api_hash)
await client.start()
# Resolve group
if isinstance(group, str) and group.lstrip('-').isdigit():
group = int(group)
if isinstance(group, int) and group < 0:
from telethon.tl.types import PeerChannel
channel_id = int(str(group).replace('-100', ''))
entity = await client.get_entity(PeerChannel(channel_id))
else:
entity = await client.get_entity(group)
log.info(f"Fetching participants from: {getattr(entity, 'title', group)}")
participants = []
now_ts = int(datetime.now(timezone.utc).timestamp())
async for user in client.iter_participants(entity):
# Determine status
status = 'unknown'
last_online = None
if isinstance(user.status, UserStatusOnline):
status = 'online'
last_online = now_ts
elif isinstance(user.status, UserStatusOffline):
status = 'offline'
if user.status.was_online:
last_online = int(user.status.was_online.timestamp())
elif isinstance(user.status, UserStatusRecently):
status = 'recently'
elif isinstance(user.status, UserStatusLastWeek):
status = 'last_week'
elif isinstance(user.status, UserStatusLastMonth):
status = 'last_month'
# Determine role
is_admin = False
is_creator = False
join_date = None
if hasattr(user, 'participant'):
p = user.participant
if isinstance(p, ChannelParticipantCreator):
is_creator = True
is_admin = True
elif isinstance(p, ChannelParticipantAdmin):
is_admin = True
if hasattr(p, 'date') and p.date:
join_date = int(p.date.timestamp())
participants.append({
'user_id': f'user{user.id}',
'first_name': user.first_name or '',
'last_name': user.last_name or '',
'username': user.username or '',
'phone': user.phone or '',
'is_bot': 1 if user.bot else 0,
'is_admin': 1 if is_admin else 0,
'is_creator': 1 if is_creator else 0,
'is_premium': 1 if getattr(user, 'premium', False) else 0,
'join_date': join_date,
'last_status': status,
'last_online': last_online,
'about': '', # Requires separate API call per user
'updated_at': now_ts,
})
await client.disconnect()
log.info(f"Fetched {len(participants)} participants")
return participants
def sync_participants(participants: list[dict]) -> dict:
"""Save participants to telegram.db."""
if not participants:
return {'synced': 0}
conn = sqlite3.connect(str(DB_PATH))
# Create table if not exists
conn.execute("""
CREATE TABLE IF NOT EXISTS participants (
user_id TEXT PRIMARY KEY,
first_name TEXT,
last_name TEXT,
username TEXT,
phone TEXT,
is_bot INTEGER DEFAULT 0,
is_admin INTEGER DEFAULT 0,
is_creator INTEGER DEFAULT 0,
is_premium INTEGER DEFAULT 0,
join_date INTEGER,
last_status TEXT DEFAULT 'unknown',
last_online INTEGER,
about TEXT,
updated_at INTEGER
)
""")
# Upsert participants
conn.executemany("""
INSERT OR REPLACE INTO participants
(user_id, first_name, last_name, username, phone, is_bot, is_admin,
is_creator, is_premium, join_date, last_status, last_online, about, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", [
(p['user_id'], p['first_name'], p['last_name'], p['username'],
p['phone'], p['is_bot'], p['is_admin'], p['is_creator'],
p['is_premium'], p['join_date'], p['last_status'],
p['last_online'], p['about'], p['updated_at'])
for p in participants
])
conn.commit()
conn.close()
log.info(f"Synced {len(participants)} participants to DB")
return {'synced': len(participants)}
# ==========================================
# DATABASE: INDEX NEW MESSAGES
# ==========================================
def index_messages(messages_json: list[dict]) -> dict:
"""
Add new messages to telegram.db using IncrementalIndexer.
Duplicates are automatically ignored.
"""
if not messages_json:
return {'new_messages': 0, 'duplicates': 0}
from indexer import IncrementalIndexer
log.info(f"Indexing {len(messages_json)} messages into telegram.db...")
indexer = IncrementalIndexer(str(DB_PATH))
stats = indexer.update_from_json_data({'messages': messages_json}, show_progress=True)
indexer.close()
log.info(f"Indexing done: {stats['new_messages']} new, {stats['duplicates']} duplicates")
return stats
# ==========================================
# EMBEDDINGS: GENERATE FOR NEW MESSAGES
# ==========================================
def generate_embeddings(messages_json: list[dict]) -> dict:
"""
Generate embeddings for new messages and add to embeddings.db.
Only processes messages that don't already have embeddings.
"""
if not os.path.exists(EMBEDDINGS_DB_PATH):
log.warning(f"embeddings.db not found at {EMBEDDINGS_DB_PATH}. Skipping embeddings.")
return {'new_embeddings': 0, 'skipped': 0}
import numpy as np
# Find which messages don't have embeddings yet
emb_conn = sqlite3.connect(str(EMBEDDINGS_DB_PATH))
existing_ids = set()
for row in emb_conn.execute("SELECT message_id FROM embeddings"):
existing_ids.add(row[0])
# Filter to messages that:
# 1. Have text content
# 2. Text is longer than 10 chars
# 3. Don't already have embeddings
new_messages = []
for msg in messages_json:
msg_id = msg.get('id')
text = msg.get('text', '')
if isinstance(text, list):
# Handle complex text format
text = ''.join(
part if isinstance(part, str) else part.get('text', '')
for part in text
)
if msg_id and msg_id not in existing_ids and text and len(text.strip()) > 10:
new_messages.append({
'id': msg_id,
'from_name': msg.get('from', ''),
'text': text
})
if not new_messages:
emb_conn.close()
log.info("No new messages need embeddings")
return {'new_embeddings': 0, 'skipped': len(messages_json)}
log.info(f"Generating embeddings for {len(new_messages)} new messages...")
# Load model
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
# Generate embeddings
texts = [m['text'][:500] for m in new_messages] # Max 500 chars per message
embeddings = model.encode(texts, show_progress_bar=True, convert_to_numpy=True,
batch_size=64)
# Insert into embeddings.db
data = []
for i, msg in enumerate(new_messages):
emb_blob = embeddings[i].astype(np.float32).tobytes()
data.append((
msg['id'],
msg['from_name'],
msg['text'][:100], # Preview
emb_blob
))
emb_conn.executemany(
"INSERT OR IGNORE INTO embeddings (message_id, from_name, text_preview, embedding) VALUES (?, ?, ?, ?)",
data
)
emb_conn.commit()
# Verify
total = emb_conn.execute("SELECT COUNT(*) FROM embeddings").fetchone()[0]
emb_conn.close()
log.info(f"Added {len(data)} new embeddings. Total embeddings: {total:,}")
return {'new_embeddings': len(data), 'total_embeddings': total}
# ==========================================
# MAIN
# ==========================================
def run_sync(hours: int = 36, skip_embeddings: bool = False):
"""Run the full sync pipeline."""
start_time = time.time()
log.info("=" * 50)
log.info("Starting daily sync")
log.info("=" * 50)
# Load config
config = load_config()
if not config:
log.error("No configuration found. Run: python daily_sync.py --setup")
sys.exit(1)
# Step 1: Fetch messages from Telegram
log.info("[1/4] Fetching messages from Telegram...")
messages_json = asyncio.run(fetch_messages(config, hours=hours))
if not messages_json:
log.info("No messages found in the time window.")
# Step 2: Index into telegram.db
index_stats = {'new_messages': 0, 'duplicates': 0}
if messages_json:
log.info("[2/4] Indexing new messages...")
index_stats = index_messages(messages_json)
# Step 3: Sync participants
log.info("[3/4] Syncing participants...")
try:
participants = asyncio.run(fetch_participants(config))
part_stats = sync_participants(participants)
except Exception as e:
log.warning(f"Failed to sync participants: {e}")
part_stats = {'synced': 0}
# Step 4: Generate embeddings
if skip_embeddings or not messages_json:
log.info("[4/4] Skipping embeddings")
emb_stats = {'new_embeddings': 0}
else:
log.info("[4/4] Generating embeddings for new messages...")
emb_stats = generate_embeddings(messages_json)
# Notify running server to invalidate caches and reload embeddings
has_changes = (index_stats.get('new_messages', 0) > 0
or part_stats.get('synced', 0) > 0
or emb_stats.get('new_embeddings', 0) > 0)
if has_changes:
try:
import urllib.request
urllib.request.urlopen('http://localhost:5000/api/cache/invalidate', timeout=5)
log.info("Server caches invalidated")
if emb_stats.get('new_embeddings', 0) > 0:
urllib.request.urlopen('http://localhost:5000/api/embeddings/reload', timeout=5)
log.info("Server notified to reload embeddings")
except Exception:
log.info("Server not running or unreachable - caches will refresh on next restart")
# Summary
elapsed = time.time() - start_time
log.info("=" * 50)
log.info("Sync complete!")
log.info(f" Messages fetched: {len(messages_json) if messages_json else 0}")
log.info(f" New to DB: {index_stats.get('new_messages', 0)}")
log.info(f" Duplicates skipped: {index_stats.get('duplicates', 0)}")
log.info(f" Participants synced: {part_stats.get('synced', 0)}")
log.info(f" New embeddings: {emb_stats.get('new_embeddings', 0)}")
log.info(f" Time: {elapsed:.1f}s")
log.info("=" * 50)
def main():
parser = argparse.ArgumentParser(description='Daily Telegram Sync')
parser.add_argument('--setup', action='store_true', help='First time setup')
parser.add_argument('--hours', type=int, default=36, help='Hours to look back (default: 36)')
parser.add_argument('--skip-embeddings', action='store_true', help='Skip embedding generation')
parser.add_argument('--fetch-only', action='store_true', help='Only fetch, do not index')
args = parser.parse_args()
if args.setup:
setup_config()
return
if args.fetch_only:
config = load_config()
if not config:
log.error("No configuration found. Run: python daily_sync.py --setup")
sys.exit(1)
messages = asyncio.run(fetch_messages(config, hours=args.hours))
# Save to file for inspection
output = BASE_DIR / 'fetched_messages.json'
with open(output, 'w', encoding='utf-8') as f:
json.dump(messages, f, ensure_ascii=False, indent=2)
log.info(f"Saved {len(messages)} messages to {output}")
return
run_sync(hours=args.hours, skip_embeddings=args.skip_embeddings)
if __name__ == '__main__':
main()
|