mirror of
				https://github.com/cdryzun/tg_bot_collections.git
				synced 2025-11-04 16:56:43 +08:00 
			
		
		
		
	Merge pull request #59 from kemingy/user_stat
feat: get user stats in the group
This commit is contained in:
		@ -87,9 +87,16 @@ def stats_command(message: Message, bot: TeleBot):
 | 
				
			|||||||
    stats_text = "\n".join(
 | 
					    stats_text = "\n".join(
 | 
				
			||||||
        f"{entry.date}: {entry.message_count} messages" for entry in stats
 | 
					        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(
 | 
					    bot.reply_to(
 | 
				
			||||||
        message,
 | 
					        message,
 | 
				
			||||||
        f"📊 群组消息统计信息:\n```\n{stats_text}\n```",
 | 
					        (
 | 
				
			||||||
 | 
					            f"📊 群组消息统计信息:\n```\n{stats_text}\n```\n",
 | 
				
			||||||
 | 
					            f"👤 用户消息统计信息:\n```\n{user_text}\n```",
 | 
				
			||||||
 | 
					        ),
 | 
				
			||||||
        parse_mode="MarkdownV2",
 | 
					        parse_mode="MarkdownV2",
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -20,6 +20,13 @@ class StatsEntry:
 | 
				
			|||||||
    message_count: int
 | 
					    message_count: int
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@dataclass(frozen=True)
 | 
				
			||||||
 | 
					class UserStatsEntry:
 | 
				
			||||||
 | 
					    user_id: int
 | 
				
			||||||
 | 
					    user_name: str
 | 
				
			||||||
 | 
					    message_count: int
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class MessageStore:
 | 
					class MessageStore:
 | 
				
			||||||
    def __init__(self, db_file: str):
 | 
					    def __init__(self, db_file: str):
 | 
				
			||||||
        parent_folder = os.path.dirname(db_file)
 | 
					        parent_folder = os.path.dirname(db_file)
 | 
				
			||||||
@ -124,6 +131,25 @@ class MessageStore:
 | 
				
			|||||||
            rows = cursor.fetchall()
 | 
					            rows = cursor.fetchall()
 | 
				
			||||||
        return [StatsEntry(date=row[0], message_count=row[1]) for row in rows]
 | 
					        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(
 | 
					    def search_messages(
 | 
				
			||||||
        self, chat_id: int, keyword: str, limit: int = 10
 | 
					        self, chat_id: int, keyword: str, limit: int = 10
 | 
				
			||||||
    ) -> list[ChatMessage]:
 | 
					    ) -> list[ChatMessage]:
 | 
				
			||||||
 | 
				
			|||||||
@ -14,7 +14,6 @@ dependencies = [
 | 
				
			|||||||
    "openai",
 | 
					    "openai",
 | 
				
			||||||
    "requests",
 | 
					    "requests",
 | 
				
			||||||
    "urlextract",
 | 
					    "urlextract",
 | 
				
			||||||
    "requests",
 | 
					 | 
				
			||||||
    "groq",
 | 
					    "groq",
 | 
				
			||||||
    "together>=1.1.5",
 | 
					    "together>=1.1.5",
 | 
				
			||||||
    "dify-client>=0.1.10",
 | 
					    "dify-client>=0.1.10",
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user