diff --git a/handlers/summary/__init__.py b/handlers/summary/__init__.py index 7e96b37..0641584 100644 --- a/handlers/summary/__init__.py +++ b/handlers/summary/__init__.py @@ -87,9 +87,16 @@ def stats_command(message: Message, bot: TeleBot): stats_text = "\n".join( f"{entry.date}: {entry.message_count} messages" for entry in stats ) + user_stats = store.get_user_stats(message.chat.id) + user_text = "\n".join( + f"{entry.user_name}: {entry.message_count}" for entry in user_stats + ) bot.reply_to( message, - f"📊 群组消息统计信息:\n```\n{stats_text}\n```", + ( + f"📊 群组消息统计信息:\n```\n{stats_text}\n```\n", + f"👤 用户消息统计信息:\n```\n{user_text}\n```", + ), parse_mode="MarkdownV2", ) diff --git a/handlers/summary/messages.py b/handlers/summary/messages.py index c2970e5..c9e960d 100644 --- a/handlers/summary/messages.py +++ b/handlers/summary/messages.py @@ -20,6 +20,13 @@ class StatsEntry: message_count: int +@dataclass(frozen=True) +class UserStatsEntry: + user_id: int + user_name: str + message_count: int + + class MessageStore: def __init__(self, db_file: str): parent_folder = os.path.dirname(db_file) @@ -124,6 +131,25 @@ class MessageStore: rows = cursor.fetchall() return [StatsEntry(date=row[0], message_count=row[1]) for row in rows] + def get_user_stats(self, chat_id: int, topk: int = 10) -> list[UserStatsEntry]: + with self.connect() as conn: + self._clean_old_messages(chat_id, conn) + cursor = conn.cursor() + cursor.execute( + """ + SELECT user_id, + (SELECT user_name FROM messages m0 WHERE m0.user_id = m1.user_id LIMIT 1) AS name, + COUNT(*) AS num + FROM messages m1 + WHERE chat_id = ? + GROUP BY user_id + ORDER BY num DESC + LIMIT ?;""", + (chat_id, topk), + ) + rows = cursor.fetchall() + return [UserStatsEntry(*row) for row in rows] + def search_messages( self, chat_id: int, keyword: str, limit: int = 10 ) -> list[ChatMessage]: diff --git a/pyproject.toml b/pyproject.toml index ad669c7..2dfa530 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,6 @@ dependencies = [ "openai", "requests", "urlextract", - "requests", "groq", "together>=1.1.5", "dify-client>=0.1.10",