Spaces:
Sleeping
Sleeping
File size: 4,282 Bytes
8004a59 | 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 | from typing import Optional, Dict, Any
from aiogram import Bot
from aiogram.types import Message
from loguru import logger
import asyncio
import time
class ProgressTracker:
"""
Tracks and sends progress updates to Telegram
Makes the AI feel alive and transparent
"""
def __init__(self, bot: Bot, chat_id: int):
self.bot = bot
self.chat_id = chat_id
self.current_message: Optional[Message] = None
self.start_time = time.time()
self.updates_sent = 0
logger.info(f"Progress tracker initialized for chat {chat_id}")
async def send_update(
self,
message: str,
progress: Optional[int] = None,
edit: bool = True
):
"""Send progress update to Telegram"""
self.updates_sent += 1
# Format message with emoji and progress
formatted = self._format_message(message, progress)
try:
if edit and self.current_message:
# Edit existing message
await self.bot.edit_message_text(
chat_id=self.chat_id,
message_id=self.current_message.message_id,
text=formatted,
parse_mode="HTML"
)
else:
# Send new message
self.current_message = await self.bot.send_message(
chat_id=self.chat_id,
text=formatted,
parse_mode="HTML"
) except Exception as e:
logger.error(f"Failed to send update: {e}")
def _format_message(self, message: str, progress: Optional[int]) -> str:
"""Format message with progress bar"""
elapsed = time.time() - self.start_time
header = f"β‘ <b>Shazam AI</b> - Active\n"
header += f"β± Runtime: {elapsed:.1f}s | Updates: {self.updates_sent}\n\n"
if progress is not None:
# Create progress bar
bar_length = 20
filled = int(bar_length * progress / 100)
bar = "β" * filled + "β" * (bar_length - filled)
header += f"<code>[{bar}]</code> {progress}%\n\n"
return header + message
async def send_file(
self,
file_path: str,
caption: str = "",
file_type: str = "document"
):
"""Send file to Telegram"""
try:
if file_type == "photo":
await self.bot.send_photo(
chat_id=self.chat_id,
photo=open(file_path, 'rb'),
caption=caption
)
elif file_type == "video":
await self.bot.send_video(
chat_id=self.chat_id,
video=open(file_path, 'rb'),
caption=caption
)
else:
await self.bot.send_document(
chat_id=self.chat_id,
document=open(file_path, 'rb'),
caption=caption
)
except Exception as e:
logger.error(f"Failed to send file: {e}")
async def send_thinking_message(self, action: str):
"""Send a thinking/action message""" thinking_messages = [
f"π€ Analyzing: {action}",
f"π Processing: {action}",
f"π§ Thinking through: {action}",
f"βοΈ Working on: {action}",
f"π¨ Creating: {action}"
]
import random
await self.send_update(random.choice(thinking_messages))
async def complete_task(self, message: str = "Task completed successfully!"):
"""Mark task as complete"""
elapsed = time.time() - self.start_time
final_message = (
f"β
<b>Task Complete!</b>\n\n"
f"β± Total time: {elapsed:.1f}s\n"
f"π Updates sent: {self.updates_sent}\n\n"
f"{message}"
)
await self.send_update(final_message, progress=100)
def reset(self):
"""Reset tracker for new task"""
self.start_time = time.time()
self.updates_sent = 0
self.current_message = None |