提交代码调整。
This commit is contained in:
103
robot.py
103
robot.py
@@ -34,6 +34,8 @@ from xiuren.xiuren_pdf import generate_pdf_from_images
|
||||
from db.connection import DBConnectionManager
|
||||
from message_util import MessageUtil
|
||||
|
||||
from db.contacts_db import ContactsDBOperator
|
||||
|
||||
|
||||
class Robot(Job):
|
||||
"""个性化自己的机器人
|
||||
@@ -104,6 +106,7 @@ class Robot(Job):
|
||||
# 消息存档模块初始化,自动完成入库动作
|
||||
self.message_storage = MessageStorage(self.client)
|
||||
|
||||
self.contacts_db = ContactsDBOperator(self.db_manager)
|
||||
@staticmethod
|
||||
def value_check(args: dict) -> bool:
|
||||
if args:
|
||||
@@ -404,67 +407,92 @@ class Robot(Job):
|
||||
self.LOG.error(f"xiu_ren_pdf_send error:{e}")
|
||||
|
||||
def get_all_contacts(self) -> dict:
|
||||
"""获取所有联系人信息并保存到数据库"""
|
||||
from db.contacts_db import ContactsDBOperator
|
||||
|
||||
"""获取所有联系人信息并返回字典格式 {wxid: nickname}"""
|
||||
try:
|
||||
contacts_dict = {}
|
||||
# 获取所有联系人的wxid列表
|
||||
contacts_wxids = self.client.fetch_contacts_list(self.app_id)
|
||||
self.LOG.info(f"contacts_wxids: {contacts_wxids}")
|
||||
if not contacts_wxids:
|
||||
self.LOG.warning("获取联系人列表为空")
|
||||
# 获取所有联系人列表
|
||||
response = self.client.fetch_contacts_list(self.app_id)
|
||||
self.LOG.info(f"获取联系人列表响应: {response}")
|
||||
|
||||
if not response or response.get("ret") != 200:
|
||||
self.LOG.warning(f"获取联系人列表失败: {response}")
|
||||
return contacts_dict
|
||||
|
||||
# 初始化联系人数据库操作类
|
||||
contacts_db = ContactsDBOperator()
|
||||
|
||||
|
||||
# 从响应中提取联系人数据
|
||||
contact_data = response.get("data", {})
|
||||
|
||||
# 处理好友列表
|
||||
friends = contact_data.get("friends", [])
|
||||
for wxid in friends:
|
||||
contacts_dict[wxid] = wxid # 默认使用wxid作为昵称
|
||||
|
||||
# 处理群聊列表
|
||||
chatrooms = contact_data.get("chatrooms", [])
|
||||
for chatroom_id in chatrooms:
|
||||
contacts_dict[chatroom_id] = chatroom_id
|
||||
# 如果是群聊,则获取群成员信息
|
||||
self.update_chatroom_member_details(chatroom_id)
|
||||
|
||||
# 处理公众号列表
|
||||
ghs = contact_data.get("ghs", [])
|
||||
for gh_id in ghs:
|
||||
contacts_dict[gh_id] = gh_id
|
||||
|
||||
# 获取联系人详细信息(昵称等)
|
||||
self.update_contact_details(contacts_dict)
|
||||
|
||||
self.LOG.info(f"成功获取并保存{len(contacts_dict)}个联系人信息")
|
||||
return contacts_dict
|
||||
|
||||
except Exception as e:
|
||||
self.LOG.error(f"获取联系人信息失败: {e}")
|
||||
return {}
|
||||
|
||||
def update_contact_details(self, contacts_dict):
|
||||
"""更新联系人详细信息(昵称等)"""
|
||||
try:
|
||||
# 将wxid列表分批处理,每批50个
|
||||
batch_size = 50
|
||||
for i in range(0, len(contacts_wxids), batch_size):
|
||||
batch_wxids = contacts_wxids[i:i + batch_size]
|
||||
|
||||
wxids = list(contacts_dict.keys())
|
||||
|
||||
for i in range(0, len(wxids), batch_size):
|
||||
batch_wxids = wxids[i:i + batch_size]
|
||||
|
||||
# 批量获取联系人详细信息
|
||||
contact_info = self.client.get_detail_info(self.app_id, batch_wxids)
|
||||
|
||||
|
||||
# 处理返回的数据
|
||||
if contact_info and contact_info.get("ret") == 200 and "data" in contact_info:
|
||||
contact_data = contact_info.get("data", [])
|
||||
|
||||
|
||||
if contact_data:
|
||||
for contact in contact_data:
|
||||
user_name = contact.get("userName")
|
||||
if not user_name:
|
||||
if not user_name or user_name not in contacts_dict:
|
||||
continue
|
||||
|
||||
|
||||
# 更新昵称
|
||||
contacts_dict[user_name] = contact.get("nickName") or user_name
|
||||
|
||||
try:
|
||||
# 判断联系人类型
|
||||
contact_type = "friends" # 默认为好友类型
|
||||
if user_name.endswith("@chatroom"):
|
||||
contact_type = "chatrooms"
|
||||
# 如果是群聊,则获取群成员信息
|
||||
self.update_chatroom_member_details(user_name)
|
||||
elif user_name.startswith("gh_"):
|
||||
contact_type = "ghs"
|
||||
|
||||
|
||||
# 保存到数据库
|
||||
contacts_db.save_contacts([contact], contact_type)
|
||||
|
||||
# 添加到返回字典
|
||||
contacts_dict[user_name] = contact.get("nickName") or user_name
|
||||
|
||||
self.contacts_db.save_contacts([contact], contact_type)
|
||||
|
||||
except Exception as e:
|
||||
self.LOG.error(f"处理联系人 {user_name} 失败: {e}")
|
||||
continue
|
||||
else:
|
||||
self.LOG.error(f"获取联系人详情失败: {contact_info}")
|
||||
|
||||
self.LOG.info(f"成功获取并保存{len(contacts_dict)}个联系人信息")
|
||||
return contacts_dict
|
||||
|
||||
else:
|
||||
self.LOG.error(f"获取联系人详情失败: {contact_info}")
|
||||
|
||||
except Exception as e:
|
||||
self.LOG.error(f"获取联系人信息失败: {e}")
|
||||
return {}
|
||||
self.LOG.error(f"更新联系人详细信息失败: {e}")
|
||||
|
||||
def update_chatroom_member_details(self, chatroom_id):
|
||||
"""更新群成员详细信息"""
|
||||
@@ -485,10 +513,7 @@ class Robot(Job):
|
||||
member_wxids # 直接传递列表,不需要转换为集合
|
||||
)
|
||||
|
||||
# 使用ContactsDBOperator处理响应
|
||||
from db.contacts_db import ContactsDBOperator
|
||||
contacts_db = ContactsDBOperator()
|
||||
success = contacts_db.process_chatroom_member_detail_response(chatroom_id, details_response)
|
||||
success = self.contacts_db.process_chatroom_member_detail_response(chatroom_id, details_response)
|
||||
|
||||
if success:
|
||||
self.LOG.info(f"成功更新群聊{chatroom_id}的成员详细信息")
|
||||
|
||||
Reference in New Issue
Block a user