入库处理

This commit is contained in:
liuwei
2025-04-28 10:24:10 +08:00
parent c9c6208eda
commit c367b2ac2e
2 changed files with 78 additions and 70 deletions

View File

@@ -3,9 +3,7 @@ import logging
import os.path
import random
import time
from typing import Optional
from gewechat_client import GewechatClient
from gewechat.client import gewe_client
from utils.wechat.contact_manager import ContactManager

146
robot.py
View File

@@ -11,6 +11,7 @@ from base.func_news import News
from configuration import Config
from gewechat.call_back_message.message import WxMessage, MessageType
from gewechat.client import gewe_client
from gewechat.response.model.group.chatroom_info import ChatroomInfo
from gewechat.response.model.personal.profile import Profile
from utils.json_converter import json_to_object
from utils.wechat.message_to_db import MessageStorage
@@ -162,6 +163,15 @@ class Robot(Job):
GroupBotManager.set_group_permission(msg.roomid, Feature.ROBOT, PermissionStatus.ENABLED)
# 更新联系人信息
# 群第一次加入机器人管理,自动添加并开启机器人功能,需要进行群成员信息初始化。请完成写入数据库,并更新联系人信息
try:
chatroom_info = self.client.get_chatroom_info(self.app_id, msg.roomid)
resp_obj = json_to_object(chatroom_info)
infos = resp_obj.data
self.contacts_db.save_chatroom_info(infos)
self.LOG.info(f"添加新的群信息到数据库成功:{infos}")
except Exception as e:
self.LOG.error(f"chatroom_info save error: {e}")
return
except Exception as e:
self.LOG.error(f"加入新群,自动添加并开启机器人功能 error: {e}")
@@ -180,83 +190,83 @@ class Robot(Job):
for info in infos:
self.LOG.info(f"已添加新用户信息到数据库: {wxid}")
# 更新缓存
self.allContacts[wxid] = info.get("nickName", "wxid")
self.allContacts[wxid] = info.get("nickName", "nickName")
self.contact_manager.set_contacts(self.allContacts)
self.LOG.info(f"已维护新用户信息到缓存: {wxid}")
self.contacts_db.save_chatroom_member_detail(msg.roomid, infos)
except Exception as e:
self.LOG.error(f"添加新用户信息到数据库失败: {e}")
# 发布消息接收事件
self.event_system.publish(EventType.MESSAGE_RECEIVED, {"message": msg})
# 标记插件是否处理了消息
plugin_processed = False
# 尝试使用插件处理消息
if self.process_plugin_message(msg):
plugin_processed = True
# 群聊消息处理 - 无论插件是否处理过,都执行数据存储
if msg.from_group():
# 调用统计逻辑进行聊天数据统计:
try:
self.message_storage.process_message(msg)
except Exception as e:
self.LOG.error(f"process_message error: {e}")
# 聊天记录入库动作:
try:
self.message_storage.archive_message(msg)
# 单独处理图片消息
if msg.msg_type == 3: # 图片消息类型
self.message_storage.process_image(msg)
except Exception as e:
self.LOG.error(f"archive_message error: {e}")
# 如果插件已处理消息,则不再执行后续的业务逻辑
if plugin_processed:
return
# 记录在群里发的最新消息,可以通过撤回指令撤回
try:
if msg.from_self():
rsp = self.gbm.handle_command(msg.roomid, msg.content)
# 不在群里发送,防止被骚扰
if rsp is not None:
self.message_util.send_text(rsp, msg.roomid, msg.sender)
return
except Exception as e:
self.LOG.error(f"revoke_receive_message error: {e}")
return # 处理完群聊信息,后面就不需要处理了
# 如果插件已处理消息,则不再执行后续的业务逻辑
if plugin_processed:
return
elif msg.msg_type == MessageType.TEXT: # 文本消息
# 让配置加载更灵活,自己可以更新配置。也可以利用定时任务更新。
if msg.from_self():
if msg.content.clean_content == "^更新$":
self.config.reload()
self.gbm.load_local_cache()
self.LOG.info("已更新")
if msg.content.clean_content == "今日百度新闻":
self.news_baidu_report()
if msg.content.clean_content == "TO_DB":
self.message_count_to_db()
if msg.content.clean_content == "PDF":
self.generate_sehuatang_pdf()
if msg.content.raw_content.startswith("清除群-"):
self.gbm.handle_command(msg.roomid, msg.content.clean_content)
else:
self.toChitchat(msg) # 闲聊
#
# # 发布消息接收事件
# self.event_system.publish(EventType.MESSAGE_RECEIVED, {"message": msg})
#
# # 标记插件是否处理了消息
# plugin_processed = False
#
# # 尝试使用插件处理消息
# if self.process_plugin_message(msg):
# plugin_processed = True
#
# # 群聊消息处理 - 无论插件是否处理过,都执行数据存储
# if msg.from_group():
# # 调用统计逻辑进行聊天数据统计:
# try:
# self.message_storage.process_message(msg)
# except Exception as e:
# self.LOG.error(f"process_message error: {e}")
#
# # 聊天记录入库动作:
# try:
# self.message_storage.archive_message(msg)
# # 单独处理图片消息
# if msg.msg_type == 3: # 图片消息类型
# self.message_storage.process_image(msg)
# except Exception as e:
# self.LOG.error(f"archive_message error: {e}")
#
# # 如果插件已处理消息,则不再执行后续的业务逻辑
# if plugin_processed:
# return
#
# # 记录在群里发的最新消息,可以通过撤回指令撤回
# try:
# if msg.from_self():
# rsp = self.gbm.handle_command(msg.roomid, msg.content)
# # 不在群里发送,防止被骚扰
# if rsp is not None:
# self.message_util.send_text(rsp, msg.roomid, msg.sender)
# return
# except Exception as e:
# self.LOG.error(f"revoke_receive_message error: {e}")
#
# return # 处理完群聊信息,后面就不需要处理了
#
# # 如果插件已处理消息,则不再执行后续的业务逻辑
# if plugin_processed:
# return
#
# elif msg.msg_type == MessageType.TEXT: # 文本消息
# # 让配置加载更灵活,自己可以更新配置。也可以利用定时任务更新。
# if msg.from_self():
# if msg.content.clean_content == "^更新$":
# self.config.reload()
# self.gbm.load_local_cache()
# self.LOG.info("已更新")
# if msg.content.clean_content == "今日百度新闻":
# self.news_baidu_report()
# if msg.content.clean_content == "TO_DB":
# self.message_count_to_db()
# if msg.content.clean_content == "PDF":
# self.generate_sehuatang_pdf()
# if msg.content.raw_content.startswith("清除群-"):
# self.gbm.handle_command(msg.roomid, msg.content.clean_content)
# else:
# self.toChitchat(msg) # 闲聊
def onMsg(self, msg: WxMessage) -> int:
try:
self.LOG.info(msg) # 打印信息
# self.processMsg(msg)
self.processMsg(msg)
except Exception as e:
self.LOG.error(e)