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