Files
abot/message_report/process_message.py
2025-02-05 12:43:51 +08:00

29 lines
1.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import redis
from datetime import datetime
import re
# 连接到Redis
r = redis.Redis(host='192.168.2.32', port=6379, db=0)
def process_message(message):
# 示例message字符串
# 使用正则表达式提取用户名称和聊天室标识
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.")