This commit is contained in:
2025-02-05 13:34:19 +08:00
parent 9a2a5009a3
commit 451f885f2d
4 changed files with 18 additions and 20 deletions

View File

@@ -57,6 +57,10 @@ def main(chat_type: int):
# epic
robot.onEveryTime("10:30", robot.sendEpicFreeGames)
# message report
robot.onEveryTime("00:30", robot.messageCountToDB)
# 让机器人一直跑
robot.keepRunningAndBlockProcess()

View File

@@ -1,28 +1,20 @@
import redis
from datetime import datetime
import re
from wcferry import WxMsg
# 连接到Redis
r = redis.Redis(host='192.168.2.32', port=6379, db=0)
def process_message(message):
def process_message(message:WxMsg):
# 示例message字符串
current_date = datetime.now().strftime('%Y-%m-%d')
# 生成Redis key
key = f"{message.roomid}:{message.sender}:{current_date}:count"
# 使用Redis哈希或字符串增加发言次数
r.hincrby(key, 'count', 1) # 这里使用哈希但也可以考虑用字符串的INCR操作
# 或者使用字符串r.incr(key) # 如果只存储一个整数值,字符串类型可能更简单
# 使用正则表达式提取用户名称和聊天室标识
user_info_regex = re.compile(r"(\w+)\[([^\]]+)\]")
match = user_info_regex.search(message)
if match:
user_name = match.group(1)
chatroom_id = match.group(2)
print(f"用户名称: {user_name}")
print(f"聊天室标识: {chatroom_id}")
current_date = datetime.now().strftime('%Y-%m-%d')
# 生成Redis key
key = f"{chatroom_id}:{user_name}:{current_date}:count"
# 使用Redis哈希或字符串增加发言次数
print(key)
r.hincrby(key, 'count', 1) # 这里使用哈希但也可以考虑用字符串的INCR操作
# 或者使用字符串r.incr(key) # 如果只存储一个整数值,字符串类型可能更简单
else:
print("Failed to extract user name or chatroom ID.")

View File

@@ -25,6 +25,7 @@ def write_to_db():
# 遍历Redis中所有与昨天日期相关的key并写入数据库
for key in r.keys(f"*:*:{yesterday}:count"):
print('user key{key}')
parts = key.decode('utf-8').split(':')
group_id, wx_id, _date = parts[0], parts[1], parts[2] # _date应该是yesterday但这里为了完整性还是保留
count = int(r.hget(key, 'count')) if isinstance(r.hget(key, 'count'), bytes) else 0 # 处理可能的None或空值情况
@@ -39,7 +40,7 @@ def write_to_db():
def generate_and_send_ranking(groupId,allContacts: dict):
# 连接到SQLite数据库假设数据库文件名为database.db
conn = sqlite3.connect('database.db')
conn = sqlite3.connect('message_stats.db')
cursor = conn.cursor()
# 编写SQL查询来获取发言数量前20的用户

View File

@@ -19,4 +19,5 @@ zhipuai~=2.1.5.20241204
protobuf~=5.29.1
beautifulsoup4~=4.12.3
httpx~=0.28.1
pyttsx3~=2.98
pyttsx3~=2.98
redis~=5.2.1