调整内容
This commit is contained in:
106
robot.py
106
robot.py
@@ -406,46 +406,63 @@ class Robot(Job):
|
||||
"""获取所有联系人信息并保存到数据库"""
|
||||
from db.contacts_db import ContactsDBOperator
|
||||
|
||||
contacts_dict = {}
|
||||
contacts_wxids = self.client.fetch_contacts_list(self.app_id)
|
||||
try:
|
||||
contacts_dict = {}
|
||||
# 获取所有联系人的wxid列表
|
||||
contacts_wxids = self.client.fetch_contacts_list(self.app_id)
|
||||
if not contacts_wxids:
|
||||
self.LOG.warning("获取联系人列表为空")
|
||||
return contacts_dict
|
||||
|
||||
# 初始化联系人数据库操作类
|
||||
contacts_db = ContactsDBOperator()
|
||||
|
||||
for wxid in contacts_wxids:
|
||||
# 获取联系人详细信息
|
||||
contact_info = self.client.get_detail_info(self.app_id, wxid)
|
||||
|
||||
# 将联系人信息添加到字典中
|
||||
if contact_info and contact_info.get("ret") == 200 and "data" in contact_info:
|
||||
contact_data = contact_info.get("data", [])
|
||||
|
||||
# 保存联系人信息到数据库
|
||||
if contact_data:
|
||||
try:
|
||||
# 判断联系人类型
|
||||
contact_type = "friends" # 默认为好友类型
|
||||
if wxid.endswith("@chatroom"):
|
||||
contact_type = "chatrooms"
|
||||
# 如果是这个类型,则提取群成员信息
|
||||
# 提取群成员信息
|
||||
self.update_chatroom_member_details(wxid)
|
||||
elif wxid.startswith("gh_"):
|
||||
contact_type = "ghs"
|
||||
|
||||
# 保存到数据库
|
||||
contacts_db.save_contacts(contact_data, contact_type)
|
||||
|
||||
# 添加到返回字典
|
||||
# 初始化联系人数据库操作类
|
||||
contacts_db = ContactsDBOperator()
|
||||
|
||||
# 将wxid列表分批处理,每批50个
|
||||
batch_size = 50
|
||||
for i in range(0, len(contacts_wxids), batch_size):
|
||||
batch_wxids = contacts_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 user_name:
|
||||
if not user_name:
|
||||
continue
|
||||
|
||||
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
|
||||
except Exception as e:
|
||||
self.LOG.error(f"保存联系人信息到数据库失败: {e}")
|
||||
|
||||
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
|
||||
self.LOG.info(f"成功获取并保存{len(contacts_dict)}个联系人信息")
|
||||
return contacts_dict
|
||||
|
||||
except Exception as e:
|
||||
self.LOG.error(f"获取联系人信息失败: {e}")
|
||||
return {}
|
||||
|
||||
def update_chatroom_member_details(self, chatroom_id):
|
||||
"""更新群成员详细信息"""
|
||||
@@ -454,24 +471,28 @@ class Robot(Job):
|
||||
members_response = self.client.get_chatroom_member_list(self.app_id, chatroom_id)
|
||||
if members_response and members_response.get('ret') == 200:
|
||||
member_list = members_response.get('data', {}).get('memberList', [])
|
||||
|
||||
|
||||
# 提取成员wxid列表
|
||||
member_wxids = [member.get('wxid') for member in member_list if member.get('wxid')]
|
||||
|
||||
|
||||
if member_wxids:
|
||||
# 获取成员详细信息
|
||||
details_response = self.client.get_chatroom_member_detail(self.app_id, chatroom_id, member_wxids)
|
||||
|
||||
# 修改参数顺序:先传入app_id,再传入wxids列表,最后传入chatroom_id
|
||||
details_response = self.client.get_chatroom_member_detail(
|
||||
self.app_id,
|
||||
wxids=member_wxids,
|
||||
chatroom_id=chatroom_id
|
||||
)
|
||||
|
||||
# 使用ContactsDBOperator处理响应
|
||||
from db.contacts_db import ContactsDBOperator
|
||||
contacts_db = ContactsDBOperator()
|
||||
success = contacts_db.process_chatroom_member_detail_response(chatroom_id, details_response)
|
||||
|
||||
|
||||
if success:
|
||||
self.LOG.info(f"成功更新群聊{chatroom_id}的成员详细信息")
|
||||
else:
|
||||
self.LOG.error(f"更新群聊{chatroom_id}的成员详细信息失败")
|
||||
|
||||
|
||||
return success
|
||||
else:
|
||||
self.LOG.warning(f"群聊{chatroom_id}没有成员")
|
||||
@@ -481,4 +502,3 @@ class Robot(Job):
|
||||
return False
|
||||
except Exception as e:
|
||||
self.LOG.error(f"更新群聊成员详细信息出错: {e}")
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user