签到功能初始化

消息计数定时任务入库
This commit is contained in:
liuwei
2025-02-05 14:48:05 +08:00
parent f0a5cfe092
commit cfe495e201
6 changed files with 23 additions and 20 deletions

View File

@@ -58,8 +58,10 @@ def main(chat_type: int):
robot.onEveryTime("10:30", robot.sendEpicFreeGames)
# message report
# message report 1:数据自动从redis 转到sqllite
robot.onEveryTime("00:30", robot.messageCountToDB)
# 从db中提取并发送给相关群
robot.onEveryTime("09:30", robot.generateAndSendRanking)
# 让机器人一直跑
robot.keepRunningAndBlockProcess()

View File

@@ -1,7 +1,9 @@
CREATE TABLE IF NOT EXISTS speech_counts (
group_id TEXT,
wx_id TEXT,
date TEXT,
count INTEGER,
PRIMARY KEY (group_id, wx_id, date)
)
CREATE TABLE IF NOT EXISTS speech_counts
(
group_id TEXT,
wx_id TEXT,
date TEXT,
count INTEGER,
PRIMARY KEY (group_id, wx_id, date)
)

View File

@@ -8,7 +8,7 @@ from wcferry import WxMsg
r = redis.Redis(host='192.168.2.32', port=6379, db=0)
def process_message(message:WxMsg):
def process_message(message: WxMsg):
# 示例message字符串
current_date = datetime.now().strftime('%Y-%m-%d')
# 生成Redis key
@@ -17,7 +17,5 @@ def process_message(message:WxMsg):
# 使用Redis哈希或字符串增加发言次数
r.hincrby(key, 'count', 1) # 这里使用哈希但也可以考虑用字符串的INCR操作
# 设置时效为48小时
r.expire(key,86400*2)
r.expire(key, 86400 * 2)
# 或者使用字符串r.incr(key) # 如果只存储一个整数值,字符串类型可能更简单

View File

@@ -3,9 +3,11 @@ import time
import sqlite3
from datetime import datetime, timedelta
import redis
# 连接到Redis
r = redis.Redis(host='192.168.2.32', port=6379, db=0)
def write_to_db():
# 连接到SQLite数据库
conn = sqlite3.connect('message_stats.db')
@@ -25,7 +27,7 @@ def write_to_db():
# 遍历Redis中所有与昨天日期相关的key并写入数据库
for key in r.keys(f"*:*:{yesterday}:count"):
print('user key'+ key)
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或空值情况
@@ -38,7 +40,7 @@ def write_to_db():
conn.close()
def generate_and_send_ranking(groupId,allContacts: dict):
def generate_and_send_ranking(groupId, allContacts: dict):
# 连接到SQLite数据库假设数据库文件名为database.db
conn = sqlite3.connect('message_stats.db')
cursor = conn.cursor()
@@ -56,19 +58,16 @@ def generate_and_send_ranking(groupId,allContacts: dict):
"""
# 执行查询并获取结果
cursor.execute(query,(groupId,))
cursor.execute(query, (groupId,))
results = cursor.fetchall()
# 格式化输出字符串
ranking_str = yesterday + "发言数量前20的用户排名:\n"
for rank, (username, speech_count) in enumerate(results, start=1):
ranking_str += f"{rank}. {allContacts[username]}: {speech_count} 次发言\n"
ranking_str += f"{rank}. {allContacts[username]}: {speech_count} 次发言\n"
# 关闭数据库连接
conn.close()
# 这里我们没有实际“发送”排名,只是返回字符串
# 如果需要发送,可以在此添加发送逻辑
return ranking_str

2
message_sign/readme.md Normal file
View File

@@ -0,0 +1,2 @@
# 签到功能
## 用户每日签到,可以汇报签到次数

View File

@@ -24,7 +24,7 @@ def add_task(description, time_str):
conn = sqlite3.connect(DB_FILE)
c = conn.cursor()
time_obj = datetime.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
c.execute("INSERT INTO tasks (description, reminder_time) VALUES (?, ?)",
c.execute("INSERT INTO tasks (des cription, reminder_time) VALUES (?, ?)",
(description, time_obj.isoformat()))
conn.commit()
conn.close()