···
8
-
from datetime import datetime
10
-
from typing import Any, Dict, List, Optional, Set, Tuple
9
+
from typing import Any, Optional
from zulip_bots.lib import BotHandler
# Handle imports for both direct execution and package import
15
+
from ..cli.commands.sync import sync_feed
from ..core.git_store import GitStore
from ..models import AtomEntry, ThicketConfig
18
-
from ..cli.commands.sync import sync_feed
# When run directly by zulip-bots, add the package to path
src_dir = Path(__file__).parent.parent.parent
if str(src_dir) not in sys.path:
sys.path.insert(0, str(src_dir))
25
+
from thicket.cli.commands.sync import sync_feed
from thicket.core.git_store import GitStore
from thicket.models import AtomEntry, ThicketConfig
28
-
from thicket.cli.commands.sync import sync_feed
···
self.logger = logging.getLogger(__name__)
self.git_store: Optional[GitStore] = None
self.config: Optional[ThicketConfig] = None
39
-
self.posted_entries: Set[str] = set()
38
+
self.posted_entries: set[str] = set()
# Bot configuration from storage
self.stream_name: Optional[str] = None
self.topic_name: Optional[str] = None
self.sync_interval: int = 300 # 5 minutes default
self.max_entries_per_sync: int = 10
self.config_path: Optional[Path] = None
47
+
# Bot behavior settings (loaded from botrc)
48
+
self.rate_limit_delay: int = 5
49
+
self.posts_per_batch: int = 5
50
+
self.catchup_entries: int = 5
51
+
self.config_change_notifications: bool = True
52
+
self.username_claim_notifications: bool = True
54
+
# Track last sync time for schedule queries
55
+
self.last_sync_time: Optional[float] = None
# Debug mode configuration
self.debug_user: Optional[str] = None
self.debug_zulip_user_id: Optional[str] = None
"""Return bot usage instructions."""
This bot automatically monitors thicket feeds and posts new articles.
60
-
- `@mention status` - Show current bot status and configuration
69
+
- `@mention status` - Show current bot status and configuration
- `@mention sync now` - Force an immediate sync
- `@mention reset` - Clear posting history (will repost recent entries)
- `@mention config stream <stream_name>` - Set target stream
64
-
- `@mention config topic <topic_name>` - Set target topic
73
+
- `@mention config topic <topic_name>` - Set target topic
- `@mention config interval <seconds>` - Set sync interval
75
+
- `@mention schedule` - Show sync schedule and next run time
76
+
- `@mention claim <username>` - Claim a thicket username for your Zulip account
- `@mention help` - Show this help message
def initialize(self, bot_handler: BotHandler) -> None:
"""Initialize the bot with persistent storage."""
self.logger.info("Initializing ThicketBot")
# Get configuration from environment (set by CLI)
self.debug_user = os.getenv("THICKET_DEBUG_USER")
config_path_env = os.getenv("THICKET_CONFIG_PATH")
self.config_path = Path(config_path_env)
self.logger.info(f"Using thicket config: {self.config_path}")
91
+
# Load default configuration from botrc file
92
+
self._load_botrc_defaults()
# Load bot configuration from persistent storage
self._load_bot_config(bot_handler)
# Initialize thicket components
self._initialize_thicket()
self._load_posted_entries(bot_handler)
# Validate debug mode if enabled
self._validate_debug_mode(bot_handler)
self.logger.error(f"Failed to initialize thicket: {e}")
# Start background sync loop
self._schedule_sync(bot_handler)
99
-
def handle_message(self, message: Dict[str, Any], bot_handler: BotHandler) -> None:
113
+
def handle_message(self, message: dict[str, Any], bot_handler: BotHandler) -> None:
"""Handle incoming Zulip messages."""
content = message["content"].strip()
sender = message["sender_full_name"]
# Only respond to mentions
if not self._is_mentioned(content, bot_handler):
cleaned_content = self._clean_mention(content, bot_handler)
command_parts = cleaned_content.split()
self._send_help(message, bot_handler)
command = command_parts[0].lower()
self._send_help(message, bot_handler)
···
self._handle_reset_command(message, bot_handler, sender)
elif command == "config":
self._handle_config_command(message, bot_handler, command_parts[1:], sender)
143
+
elif command == "schedule":
144
+
self._handle_schedule_command(message, bot_handler, sender)
145
+
elif command == "claim":
146
+
self._handle_claim_command(message, bot_handler, command_parts[1:], sender)
bot_handler.send_reply(message, f"Unknown command: {command}. Type `@mention help` for usage.")
···
return f"@{bot_name}" in content.lower() or f"@**{bot_name}**" in content.lower()
self.logger.debug(f"Could not get bot profile: {e}")
# Fallback to generic check
return "@thicket" in content.lower()
def _clean_mention(self, content: str, bot_handler: BotHandler) -> str:
"""Remove bot mention from message content."""
# Get bot's actual name from Zulip
bot_info = bot_handler._client.get_profile()
···
self.logger.debug(f"Could not get bot profile for mention cleaning: {e}")
# Fallback to removing @thicket
content = re.sub(r'@(?:\*\*)?thicket(?:\*\*)?', '', content, flags=re.IGNORECASE).strip()
171
-
def _send_help(self, message: Dict[str, Any], bot_handler: BotHandler) -> None:
189
+
def _send_help(self, message: dict[str, Any], bot_handler: BotHandler) -> None:
bot_handler.send_reply(message, self.usage())
175
-
def _send_status(self, message: Dict[str, Any], bot_handler: BotHandler, sender: str) -> None:
193
+
def _send_status(self, message: dict[str, Any], bot_handler: BotHandler, sender: str) -> None:
"""Send bot status information."""
f"**Thicket Bot Status** (requested by {sender})",
185
-
f"🐛 **Debug Mode:** ENABLED",
203
+
"🐛 **Debug Mode:** ENABLED",
f"🎯 **Debug User:** {self.debug_user}",
···
f"📝 **Topic:** {self.topic_name or 'Not configured'}",
f"⏱️ **Sync Interval:** {self.sync_interval}s ({self.sync_interval // 60}m {self.sync_interval % 60}s)",
f"📊 **Max Entries/Sync:** {self.max_entries_per_sync}",
···
f"📄 **Tracked Entries:** {len(self.posted_entries)}",
f"🔄 **Catchup Mode:** {'Active (first run)' if len(self.posted_entries) == 0 else 'Inactive'}",
f"✅ **Thicket Initialized:** {'Yes' if self.git_store else 'No'}",
223
+
self._get_schedule_info(),
bot_handler.send_reply(message, "\n".join(status_lines))
208
-
def _handle_force_sync(self, message: Dict[str, Any], bot_handler: BotHandler, sender: str) -> None:
228
+
def _handle_force_sync(self, message: dict[str, Any], bot_handler: BotHandler, sender: str) -> None:
"""Handle immediate sync request."""
if not self._check_initialization(message, bot_handler):
bot_handler.send_reply(message, f"🔄 Starting immediate sync... (requested by {sender})")
new_entries = self._perform_sync(bot_handler)
f"✅ Sync completed! Found {len(new_entries)} new entries."
self.logger.error(f"Force sync failed: {e}")
bot_handler.send_reply(message, f"❌ Sync failed: {str(e)}")
225
-
def _handle_reset_command(self, message: Dict[str, Any], bot_handler: BotHandler, sender: str) -> None:
245
+
def _handle_reset_command(self, message: dict[str, Any], bot_handler: BotHandler, sender: str) -> None:
"""Handle reset command to clear posted entries tracking."""
self.posted_entries.clear()
···
self.logger.error(f"Reset failed: {e}")
bot_handler.send_reply(message, f"❌ Reset failed: {str(e)}")
259
+
def _handle_schedule_command(self, message: dict[str, Any], bot_handler: BotHandler, sender: str) -> None:
260
+
"""Handle schedule query command."""
261
+
schedule_info = self._get_schedule_info()
262
+
bot_handler.send_reply(
264
+
f"**Thicket Bot Schedule** (requested by {sender})\n\n{schedule_info}"
267
+
def _handle_claim_command(
269
+
message: dict[str, Any],
270
+
bot_handler: BotHandler,
274
+
"""Handle username claiming command."""
276
+
bot_handler.send_reply(message, "Usage: `@mention claim <username>`")
279
+
if not self._check_initialization(message, bot_handler):
282
+
username = args[0].strip()
284
+
# Get sender's Zulip user info
285
+
sender_user_id = message.get("sender_id")
286
+
sender_email = message.get("sender_email")
288
+
if not sender_user_id or not sender_email:
289
+
bot_handler.send_reply(message, "❌ Could not determine your Zulip user information.")
293
+
# Get current Zulip server from environment
294
+
zulip_site_url = os.getenv("THICKET_ZULIP_SITE_URL", "")
295
+
server_url = zulip_site_url.replace("https://", "").replace("http://", "")
298
+
bot_handler.send_reply(message, "❌ Could not determine Zulip server URL.")
301
+
# Check if username exists in thicket
302
+
user = self.git_store.get_user(username)
304
+
bot_handler.send_reply(
306
+
f"❌ Username `{username}` not found in thicket. Available users: {', '.join(self.git_store.list_users())}"
310
+
# Check if username is already claimed for this server
311
+
existing_zulip_id = user.get_zulip_mention(server_url)
312
+
if existing_zulip_id:
313
+
# Check if it's claimed by the same user
314
+
if existing_zulip_id == sender_email or str(existing_zulip_id) == str(sender_user_id):
315
+
bot_handler.send_reply(
317
+
f"✅ Username `{username}` is already claimed by you on {server_url}!"
320
+
bot_handler.send_reply(
322
+
f"❌ Username `{username}` is already claimed by another user on {server_url}."
326
+
# Claim the username - prefer email for consistency
327
+
success = self.git_store.add_zulip_association(username, server_url, sender_email)
330
+
reply_msg = f"🎉 Successfully claimed username `{username}` for **{sender}** on {server_url}!\n" + \
331
+
"You will now be mentioned when new articles are posted from this user's feeds."
332
+
bot_handler.send_reply(message, reply_msg)
334
+
# Send notification to configured stream if enabled and not in debug mode
335
+
if (self.username_claim_notifications and
336
+
not self.debug_user and
337
+
self.stream_name and self.topic_name):
339
+
notification_msg = f"👋 **{sender}** claimed thicket username `{username}` on {server_url}"
340
+
bot_handler.send_message({
342
+
"to": self.stream_name,
343
+
"subject": self.topic_name,
344
+
"content": notification_msg
346
+
except Exception as e:
347
+
self.logger.error(f"Failed to send username claim notification: {e}")
349
+
self.logger.info(f"User {sender} ({sender_email}) claimed username {username} on {server_url}")
351
+
bot_handler.send_reply(
353
+
f"❌ Failed to claim username `{username}`. This shouldn't happen - please contact an administrator."
356
+
except Exception as e:
357
+
self.logger.error(f"Error processing claim for {username} by {sender}: {e}")
358
+
bot_handler.send_reply(message, f"❌ Error processing claim: {str(e)}")
def _handle_config_command(
241
-
message: Dict[str, Any],
242
-
bot_handler: BotHandler,
362
+
message: dict[str, Any],
363
+
bot_handler: BotHandler,
"""Handle configuration commands."""
bot_handler.send_reply(message, "Usage: `@mention config <setting> <value>`")
setting = args[0].lower()
value = " ".join(args[1:])
376
+
old_value = self.stream_name
self._save_bot_config(bot_handler)
bot_handler.send_reply(message, f"✅ Stream set to: **{value}** (by {sender})")
380
+
self._send_config_change_notification(bot_handler, sender, "stream", old_value, value)
383
+
old_value = self.topic_name
self._save_bot_config(bot_handler)
bot_handler.send_reply(message, f"✅ Topic set to: **{value}** (by {sender})")
387
+
self._send_config_change_notification(bot_handler, sender, "topic", old_value, value)
elif setting == "interval":
bot_handler.send_reply(message, "❌ Interval must be at least 60 seconds")
395
+
old_value = self.sync_interval
self.sync_interval = interval
self._save_bot_config(bot_handler)
bot_handler.send_reply(message, f"✅ Sync interval set to: **{interval}s** (by {sender})")
399
+
self._send_config_change_notification(bot_handler, sender, "sync interval", f"{old_value}s", f"{interval}s")
bot_handler.send_reply(message, "❌ Invalid interval value. Must be a number of seconds.")
403
+
elif setting == "max_entries":
405
+
max_entries = int(value)
406
+
if max_entries < 1 or max_entries > 50:
407
+
bot_handler.send_reply(message, "❌ Max entries must be between 1 and 50")
409
+
old_value = self.max_entries_per_sync
410
+
self.max_entries_per_sync = max_entries
411
+
self._save_bot_config(bot_handler)
412
+
bot_handler.send_reply(message, f"✅ Max entries per sync set to: **{max_entries}** (by {sender})")
413
+
self._send_config_change_notification(bot_handler, sender, "max entries per sync", str(old_value), str(max_entries))
415
+
bot_handler.send_reply(message, "❌ Invalid max entries value. Must be a number.")
279
-
f"❌ Unknown setting: {setting}. Available: stream, topic, interval"
420
+
f"❌ Unknown setting: {setting}. Available: stream, topic, interval, max_entries"
def _load_bot_config(self, bot_handler: BotHandler) -> None:
···
config = json.loads(config_data)
self.stream_name = config.get("stream_name")
289
-
self.topic_name = config.get("topic_name")
430
+
self.topic_name = config.get("topic_name")
self.sync_interval = config.get("sync_interval", 300)
self.max_entries_per_sync = config.get("max_entries_per_sync", 10)
292
-
except Exception as e:
433
+
self.last_sync_time = config.get("last_sync_time")
# Bot config not found on first run is expected
···
"topic_name": self.topic_name,
"sync_interval": self.sync_interval,
"max_entries_per_sync": self.max_entries_per_sync,
446
+
"last_sync_time": self.last_sync_time,
bot_handler.storage.put("bot_config", json.dumps(config_data))
self.logger.error(f"Error saving bot config: {e}")
452
+
def _load_botrc_defaults(self) -> None:
453
+
"""Load default configuration from botrc file."""
455
+
import configparser
456
+
from pathlib import Path
458
+
botrc_path = Path("bot-config/botrc")
459
+
if not botrc_path.exists():
460
+
self.logger.info("No botrc file found, using hardcoded defaults")
463
+
config = configparser.ConfigParser()
464
+
config.read(botrc_path)
466
+
if "bot" in config:
467
+
bot_section = config["bot"]
468
+
self.sync_interval = bot_section.getint("sync_interval", 300)
469
+
self.max_entries_per_sync = bot_section.getint("max_entries_per_sync", 10)
470
+
self.rate_limit_delay = bot_section.getint("rate_limit_delay", 5)
471
+
self.posts_per_batch = bot_section.getint("posts_per_batch", 5)
473
+
# Set defaults only if not already configured
474
+
default_stream = bot_section.get("default_stream", "").strip()
475
+
default_topic = bot_section.get("default_topic", "").strip()
477
+
self.stream_name = default_stream
479
+
self.topic_name = default_topic
481
+
if "catchup" in config:
482
+
catchup_section = config["catchup"]
483
+
self.catchup_entries = catchup_section.getint("catchup_entries", 5)
485
+
if "notifications" in config:
486
+
notifications_section = config["notifications"]
487
+
self.config_change_notifications = notifications_section.getboolean("config_change_notifications", True)
488
+
self.username_claim_notifications = notifications_section.getboolean("username_claim_notifications", True)
490
+
self.logger.info(f"Loaded configuration from {botrc_path}")
492
+
except Exception as e:
493
+
self.logger.error(f"Error loading botrc defaults: {e}")
494
+
self.logger.info("Using hardcoded defaults")
def _initialize_thicket(self) -> None:
"""Initialize thicket components."""
if not self.config_path or not self.config_path.exists():
raise ValueError("Thicket config file not found")
# Load thicket configuration
with open(self.config_path) as f:
config_data = yaml.safe_load(f)
self.config = ThicketConfig(**config_data)
self.git_store = GitStore(self.config.git_store)
self.logger.info("Thicket components initialized successfully")
def _validate_debug_mode(self, bot_handler: BotHandler) -> None:
"""Validate debug mode configuration."""
if not self.debug_user or not self.git_store:
# Get current Zulip server from environment
zulip_site_url = os.getenv("THICKET_ZULIP_SITE_URL", "")
server_url = zulip_site_url.replace("https://", "").replace("http://", "")
# Check if debug user exists in thicket
user = self.git_store.get_user(self.debug_user)
raise ValueError(f"Debug user '{self.debug_user}' not found in thicket")
# Check if user has Zulip association for this server
raise ValueError("Could not determine Zulip server URL")
zulip_user_id = user.get_zulip_mention(server_url)
raise ValueError(f"User '{self.debug_user}' has no Zulip association for server '{server_url}'")
# Try to look up the actual Zulip user ID from the email address
# But don't fail if we can't - we'll try again when sending messages
actual_user_id = self._lookup_zulip_user_id(bot_handler, zulip_user_id)
···
# If it's already a numeric user ID, return it
if email_or_id.isdigit():
client = bot_handler._client
self.logger.error("No Zulip client available for user lookup")
# First try the get_user_by_email API if available
user_result = client.get_user_by_email(email_or_id)
···
except (AttributeError, Exception):
# Fallback: Get all users and search through them
users_result = client.get_users()
if users_result.get('result') == 'success':
for user in users_result['members']:
user_email = user.get('email', '')
delivery_email = user.get('delivery_email', '')
390
-
if (user_email == email_or_id or
577
+
if (user_email == email_or_id or
delivery_email == email_or_id or
str(user.get('user_id')) == email_or_id):
user_id = user.get('user_id')
self.logger.error(f"No user found with identifier '{email_or_id}'. Searched {len(users_result['members'])} users.")
self.logger.error(f"Failed to get users: {users_result.get('msg', 'Unknown error')}")
self.logger.error(f"Error looking up user ID for '{email_or_id}': {e}")
406
-
def _lookup_zulip_user_info(self, bot_handler: BotHandler, email_or_id: str) -> Tuple[Optional[str], Optional[str]]:
593
+
def _lookup_zulip_user_info(self, bot_handler: BotHandler, email_or_id: str) -> tuple[Optional[str], Optional[str]]:
"""Look up both Zulip user ID and full name from email address."""
if email_or_id.isdigit():
client = bot_handler._client
# Try get_user_by_email API first
user_result = client.get_user_by_email(email_or_id)
···
return str(user_id), full_name
# Fallback: search all users
users_result = client.get_users()
if users_result.get('result') == 'success':
for user in users_result['members']:
432
-
if (user.get('email') == email_or_id or
619
+
if (user.get('email') == email_or_id or
user.get('delivery_email') == email_or_id):
return str(user.get('user_id')), user.get('full_name', '')
self.logger.error(f"Error looking up user info for '{email_or_id}': {e}")
···
self.logger.error(f"Error saving posted entries: {e}")
459
-
def _check_initialization(self, message: Dict[str, Any], bot_handler: BotHandler) -> bool:
646
+
def _check_initialization(self, message: dict[str, Any], bot_handler: BotHandler) -> bool:
"""Check if thicket is properly initialized."""
if not self.git_store or not self.config:
"❌ Thicket not initialized. Please check configuration."
# In debug mode, we don't need stream/topic configuration
if not self.stream_name or not self.topic_name:
"❌ Stream and topic must be configured first. Use `@mention config stream <name>` and `@mention config topic <name>`"
def _schedule_sync(self, bot_handler: BotHandler) -> None:
···
487
-
can_sync = (self.git_store and
488
-
((self.stream_name and self.topic_name) or
674
+
can_sync = (self.git_store and
675
+
((self.stream_name and self.topic_name) or
self._perform_sync(bot_handler)
time.sleep(self.sync_interval)
self.logger.error(f"Error in sync loop: {e}")
time.sleep(60) # Wait before retrying
# Start background thread
sync_thread = threading.Thread(target=sync_loop, daemon=True)
504
-
def _perform_sync(self, bot_handler: BotHandler) -> List[AtomEntry]:
691
+
def _perform_sync(self, bot_handler: BotHandler) -> list[AtomEntry]:
"""Perform thicket sync and return new entries."""
if not self.config or not self.git_store:
509
-
new_entries: List[Tuple[AtomEntry, str]] = [] # (entry, username) pairs
696
+
new_entries: list[tuple[AtomEntry, str]] = [] # (entry, username) pairs
is_first_run = len(self.posted_entries) == 0
# Get all users and their feeds from git store
users_with_feeds = self.git_store.list_all_users_with_feeds()
for username, feed_urls in users_with_feeds:
for feed_url in feed_urls:
···
new_count, _ = loop.run_until_complete(
sync_feed(self.git_store, username, str(feed_url), dry_run=False)
# Get the newly added entries
entries_to_check = self.git_store.list_entries(username, limit=new_count)
# Always check for catchup mode on first run
535
-
# Catchup mode: get last 5 entries on first run
536
-
catchup_entries = self.git_store.list_entries(username, limit=5)
722
+
# Catchup mode: get configured number of entries on first run
723
+
catchup_entries = self.git_store.list_entries(username, limit=self.catchup_entries)
entries_to_check = catchup_entries if not entries_to_check else entries_to_check
for entry in entries_to_check:
entry_key = f"{username}:{entry.id}"
if entry_key not in self.posted_entries:
new_entries.append((entry, username))
if len(new_entries) >= self.max_entries_per_sync:
self.logger.error(f"Error syncing feed {feed_url} for user {username}: {e}")
if len(new_entries) >= self.max_entries_per_sync:
# Post new entries to Zulip with rate limiting
for i, (entry, username) in enumerate(new_entries):
self._post_entry_to_zulip(entry, bot_handler, username)
self.posted_entries.add(f"{username}:{entry.id}")
564
-
# Rate limiting: pause after every 5 messages
565
-
if posted_count % 5 == 0 and i < len(new_entries) - 1:
751
+
# Rate limiting: pause after configured number of messages
752
+
if posted_count % self.posts_per_batch == 0 and i < len(new_entries) - 1:
753
+
time.sleep(self.rate_limit_delay)
self._save_posted_entries(bot_handler)
757
+
# Update last sync time
758
+
self.last_sync_time = time.time()
return [entry for entry, _ in new_entries]
def _post_entry_to_zulip(self, entry: AtomEntry, bot_handler: BotHandler, username: str) -> None:
···
# Get current Zulip server from environment
zulip_site_url = os.getenv("THICKET_ZULIP_SITE_URL", "")
server_url = zulip_site_url.replace("https://", "").replace("http://", "")
# Build author/date info consistently
if server_url and self.git_store:
···
# Look up the actual Zulip full name for proper @mention
_, zulip_full_name = self._lookup_zulip_user_info(bot_handler, zulip_user_id)
display_name = zulip_full_name or user.display_name or username
# Check if author is different from the user - avoid redundancy
author_name = entry.author and entry.author.get("name")
if author_name and author_name.lower() != display_name.lower():
author_info = f" (by {author_name})"
published_info = f" • {entry.published.strftime('%Y-%m-%d')}"
mention_info = f"@**{display_name}** posted{author_info}{published_info}:\n\n"
# If no Zulip user found, use consistent format without @mention
user = self.git_store.get_user(username) if self.git_store else None
display_name = user.display_name if user else username
author_name = entry.author and entry.author.get("name")
if author_name and author_name.lower() != display_name.lower():
author_info = f" (by {author_name})"
published_info = f" • {entry.published.strftime('%Y-%m-%d')}"
mention_info = f"**{display_name}** posted{author_info}{published_info}:\n\n"
# Format the message with HTML processing
# Process HTML in summary and truncate if needed
processed_summary = self._process_html_content(entry.summary)
if len(processed_summary) > 400:
processed_summary = processed_summary[:397] + "..."
message_lines.append(f"\n{processed_summary}")
message_content = mention_info + "\n".join(message_lines)
# Choose destination based on mode
if self.debug_user and self.debug_zulip_user_id:
debug_message = f"🐛 **DEBUG:** New article from thicket user `{username}`:\n\n{message_content}"
# Ensure we have the numeric user ID
user_id_to_use = self.debug_zulip_user_id
if not user_id_to_use.isdigit():
···
self.logger.error(f"Could not resolve user ID for {self.debug_zulip_user_id}")
# For private messages, user_id needs to be an integer, not string
user_id_int = int(user_id_to_use)
···
# If conversion to int fails, user_id_to_use might be an email
bot_handler.send_message({
"to": [user_id_to_use], # Try as string (email)
···
"content": message_content
self.logger.info(f"Posted entry to stream: {entry.title} (user: {username})")
self.logger.error(f"Error posting entry to Zulip: {e}")
···
"""Process HTML content from feeds to clean Zulip-compatible markdown."""
# Try to use markdownify for proper HTML to Markdown conversion
from markdownify import markdownify as md
# Convert HTML to Markdown with compact settings for summaries
···
bullets="-", # Use - for bullets
convert=['a', 'b', 'strong', 'i', 'em', 'code', 'pre', 'p', 'br', 'ul', 'ol', 'li', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']
# Post-process to convert headings to bold for compact summaries
# Convert markdown headers to bold with period
markdown = re.sub(r'^#{1,6}\s*(.+)$', r'**\1.**', markdown, flags=re.MULTILINE)
# Clean up excessive newlines and make more compact
markdown = re.sub(r'\n\s*\n\s*\n+', ' ', markdown) # Multiple newlines become space
markdown = re.sub(r'\n\s*\n', '. ', markdown) # Double newlines become sentence breaks
markdown = re.sub(r'\n', ' ', markdown) # Single newlines become spaces
# Clean up double periods and excessive whitespace
markdown = re.sub(r'\.\.+', '.', markdown)
markdown = re.sub(r'\s+', ' ', markdown)
# Fallback: manual HTML processing
# Convert headings to bold with periods for compact summaries
content = re.sub(r'<h[1-6](?:\s[^>]*)?>([^<]*)</h[1-6]>', r'**\1.** ', content, flags=re.IGNORECASE)
# Convert common HTML elements to Markdown
content = re.sub(r'<(?:strong|b)(?:\s[^>]*)?>([^<]*)</(?:strong|b)>', r'**\1**', content, flags=re.IGNORECASE)
content = re.sub(r'<(?:em|i)(?:\s[^>]*)?>([^<]*)</(?:em|i)>', r'*\1*', content, flags=re.IGNORECASE)
content = re.sub(r'<code(?:\s[^>]*)?>([^<]*)</code>', r'`\1`', content, flags=re.IGNORECASE)
content = re.sub(r'<a(?:\s[^>]*?)?\s*href=["\']([^"\']*)["\'](?:\s[^>]*)?>([^<]*)</a>', r'[\2](\1)', content, flags=re.IGNORECASE)
# Convert block elements to spaces instead of newlines for compactness
content = re.sub(r'<br\s*/?>', ' ', content, flags=re.IGNORECASE)
content = re.sub(r'</p>\s*<p>', '. ', content, flags=re.IGNORECASE)
content = re.sub(r'</?(?:p|div)(?:\s[^>]*)?>', ' ', content, flags=re.IGNORECASE)
# Remove remaining HTML tags
content = re.sub(r'<[^>]+>', '', content)
# Clean up whitespace and make compact
content = re.sub(r'\s+', ' ', content) # Multiple whitespace becomes single space
content = re.sub(r'\.\.+', '.', content) # Multiple periods become single period
self.logger.error(f"Error processing HTML content: {e}")
# Last resort: just strip HTML tags
return re.sub(r'<[^>]+>', '', html_content).strip()
943
+
def _get_schedule_info(self) -> str:
944
+
"""Get schedule information string."""
754
-
handler_class = ThicketBotHandler
947
+
if self.last_sync_time:
949
+
last_sync = datetime.datetime.fromtimestamp(self.last_sync_time)
950
+
next_sync = last_sync + datetime.timedelta(seconds=self.sync_interval)
951
+
now = datetime.datetime.now()
953
+
# Calculate time until next sync
954
+
time_until_next = next_sync - now
956
+
if time_until_next.total_seconds() > 0:
957
+
minutes, seconds = divmod(int(time_until_next.total_seconds()), 60)
958
+
hours, minutes = divmod(minutes, 60)
961
+
time_str = f"{hours}h {minutes}m {seconds}s"
963
+
time_str = f"{minutes}m {seconds}s"
965
+
time_str = f"{seconds}s"
968
+
f"🕐 **Last Sync:** {last_sync.strftime('%H:%M:%S')}",
969
+
f"⏰ **Next Sync:** {next_sync.strftime('%H:%M:%S')} (in {time_str})",
973
+
f"🕐 **Last Sync:** {last_sync.strftime('%H:%M:%S')}",
974
+
f"⏰ **Next Sync:** Due now (running every {self.sync_interval}s)",
977
+
lines.append("🕐 **Last Sync:** Never (bot starting up)")
979
+
# Add sync frequency info
980
+
if self.sync_interval >= 3600:
981
+
frequency_str = f"{self.sync_interval // 3600}h {(self.sync_interval % 3600) // 60}m"
982
+
elif self.sync_interval >= 60:
983
+
frequency_str = f"{self.sync_interval // 60}m {self.sync_interval % 60}s"
985
+
frequency_str = f"{self.sync_interval}s"
987
+
lines.append(f"🔄 **Sync Frequency:** Every {frequency_str}")
989
+
return "\n".join(lines)
991
+
def _send_config_change_notification(self, bot_handler: BotHandler, changer: str, setting: str, old_value: Optional[str], new_value: str) -> None:
992
+
"""Send configuration change notification if enabled."""
993
+
if not self.config_change_notifications or self.debug_user:
996
+
# Don't send notification if stream/topic aren't configured yet
997
+
if not self.stream_name or not self.topic_name:
1001
+
old_display = old_value if old_value else "(not set)"
1002
+
notification_msg = f"⚙️ **{changer}** changed {setting}: `{old_display}` → `{new_value}`"
1004
+
bot_handler.send_message({
1006
+
"to": self.stream_name,
1007
+
"subject": self.topic_name,
1008
+
"content": notification_msg
1010
+
except Exception as e:
1011
+
self.logger.error(f"Failed to send config change notification: {e}")
1014
+
handler_class = ThicketBotHandler