聊天记录统计功能
This commit is contained in:
@@ -37,29 +37,30 @@ def write_to_db():
|
||||
conn.close()
|
||||
|
||||
|
||||
def generate_and_send_ranking():
|
||||
def generate_and_send_ranking(groupId,allContacts: dict):
|
||||
# 连接到SQLite数据库(假设数据库文件名为database.db)
|
||||
conn = sqlite3.connect('database.db')
|
||||
cursor = conn.cursor()
|
||||
|
||||
# 编写SQL查询来获取发言数量前20的用户
|
||||
query = """
|
||||
SELECT group_id,wx_id, COUNT(*) AS speech_count
|
||||
FROM speech_counts
|
||||
WHERE DATE(date) = DATE('now', '-1 day')
|
||||
GROUP BY group_id,wx_id
|
||||
ORDER BY count DESC
|
||||
LIMIT 20
|
||||
SELECT group_id,wx_id, COUNT(*) AS speech_count
|
||||
FROM speech_counts
|
||||
WHERE DATE(date) = DATE('now', '-1 day')
|
||||
AND group_id = ?
|
||||
GROUP BY group_id,wx_id
|
||||
ORDER BY count DESC
|
||||
LIMIT 20
|
||||
"""
|
||||
|
||||
# 执行查询并获取结果
|
||||
cursor.execute(query)
|
||||
cursor.execute(query,(groupId,))
|
||||
results = cursor.fetchall()
|
||||
|
||||
# 格式化输出字符串
|
||||
ranking_str = "发言数量前20的用户排名:\n"
|
||||
for rank, (username, speech_count) in enumerate(results, start=1):
|
||||
ranking_str += f"{rank}. {username}: {speech_count} 次发言\n"
|
||||
ranking_str += f"{rank}. {allContacts[username]}: {speech_count} 次发言\n"
|
||||
|
||||
# 关闭数据库连接
|
||||
conn.close()
|
||||
|
||||
8
robot.py
8
robot.py
@@ -185,6 +185,8 @@ class Robot(Job):
|
||||
self.LOG.info("已更新")
|
||||
if msg.content == "今日36氪新闻" :
|
||||
self.newsReport()
|
||||
if msg.content =='聊天排行榜':
|
||||
self.generateAndSendRanking()
|
||||
else:
|
||||
self.toChitchat(msg) # 闲聊
|
||||
|
||||
@@ -322,4 +324,8 @@ class Robot(Job):
|
||||
write_to_db()
|
||||
|
||||
def generateAndSendRanking(self):
|
||||
generate_and_send_ranking()
|
||||
receivers = self.config.NEWS
|
||||
if not receivers:
|
||||
return
|
||||
for r in receivers:
|
||||
self.sendTextMsg(generate_and_send_ranking(r,self.allContacts), r)
|
||||
|
||||
Reference in New Issue
Block a user