| | |
| | | from app.services.ecloud_client import ecloud_client |
| | | from app.services.dify_client import dify_client |
| | | from app.services.friend_ignore_service import friend_ignore_service |
| | | from app.services.silence_service import silence_service |
| | | from app.services.group_stats_service import group_stats_service |
| | | from config import settings |
| | | |
| | | |
| | |
| | | logger.warning(f"消息缺少必要字段: data={data}") |
| | | return False |
| | | |
| | | # 检查发送者是否在好友忽略列表中 |
| | | # 获取用户和群组信息 |
| | | from_user = data.get("fromUser") |
| | | if friend_ignore_service.is_friend_ignored(from_user): |
| | | logger.info(f"忽略好友发送的消息: fromUser={from_user}") |
| | | from_group = data.get("fromGroup") |
| | | |
| | | # 检查发送者是否在好友忽略列表中 |
| | | is_friend_ignored = friend_ignore_service.is_friend_ignored(from_user) |
| | | |
| | | if is_friend_ignored: |
| | | logger.info(f"忽略好友发送的消息: fromUser={from_user}, fromGroup={from_group}") |
| | | # 统计被忽略的好友发言次数(确保被忽略的好友消息也纳入统计) |
| | | group_stats_service.increment_user_message_count(from_group, from_user) |
| | | # 激活或延长该群组的静默模式 |
| | | if silence_service.is_silence_active(from_group): |
| | | # 如果该群组静默模式已激活,延长时间 |
| | | silence_service.extend_silence_mode(from_group) |
| | | logger.info(f"好友消息被忽略,群组静默模式时间已刷新: fromUser={from_user}, fromGroup={from_group}") |
| | | else: |
| | | # 如果该群组静默模式未激活,激活静默模式 |
| | | silence_service.activate_silence_mode(from_group) |
| | | logger.info(f"好友消息被忽略,群组静默模式已激活: fromUser={from_user}, fromGroup={from_group}") |
| | | return False |
| | | |
| | | # # 统计正常处理的好友发言次数 |
| | | # group_stats_service.increment_user_message_count(from_group, from_user) |
| | | |
| | | # 检查该群组的静默模式是否激活(在好友忽略检查之后) |
| | | if silence_service.is_silence_active(from_group): |
| | | logger.info(f"群组静默模式激活中,忽略消息: fromGroup={from_group}") |
| | | return False |
| | | |
| | | return True |
| | |
| | | logger.error(f"联系人信息处理失败: from_group={from_group}") |
| | | return False |
| | | |
| | | # 3.2 获取用户在当前群组的conversation_id |
| | | # 3.2 获取群组中发言次数最多的用户昵称 |
| | | most_active_nickname = group_stats_service.get_most_active_user_nickname(from_group) |
| | | logger.info(f"群组最活跃用户昵称: group={from_group}, nickname={most_active_nickname}") |
| | | |
| | | # 3.3 获取用户在当前群组的conversation_id |
| | | conversation_id = redis_queue.get_conversation_id(from_user, from_group) |
| | | |
| | | # 调用Dify接口发送消息(根据配置选择模式) |
| | | dify_response = dify_client.send_message( |
| | | query=content, user=from_user, conversation_id=conversation_id |
| | | query=content, |
| | | user=from_user, |
| | | conversation_id=conversation_id, |
| | | nick_name=most_active_nickname |
| | | ) |
| | | |
| | | if not dify_response: |
| | |
| | | |
| | | # 更新Redis中的conversation_id(基于用户+群组) |
| | | if new_conversation_id: |
| | | redis_queue.set_conversation_id(from_user, new_conversation_id, from_group) |
| | | redis_queue.set_conversation_id(from_user, new_conversation_id, from_group, 1800) |
| | | |
| | | # 3.3 保存对话记录到数据库 |
| | | # 3.4 保存对话记录到数据库 |
| | | # 按用户、群组和小时分组对话记录 |
| | | current_time = datetime.now() |
| | | hour_key = current_time.strftime("%Y%m%d_%H") |