@@ -11,6 +11,9 @@ from common.interceptors import before_request, after_request, handle_exception | |||
import threading | |||
from common import kafka_helper, redis_helper,utils | |||
from model import Models | |||
import logging | |||
from config import load_config | |||
@@ -19,7 +22,7 @@ from wechat.biz import start_kafka_consumer_thread | |||
from wechat import gewe_chat | |||
import os,time,json | |||
import os,time,json,time | |||
from voice.ali.ali_voice import AliVoice | |||
@@ -135,14 +138,28 @@ def process_add_contacts_from_chatrooms(wxchat:gewe_chat.GeWeChatCom,status, nic | |||
contact_wxids_set.add(chatroom_owner_wxid) | |||
contact_wxids_set.add(wxid) | |||
unavailable_wixds=wxchat.check_wixd_group_add_contacts_history(wxid,chatroom_id) | |||
contact_wxids_set.update(unavailable_wixds) | |||
chatroot_member_list = chatroom.get('memberList', []) | |||
remaining_chatroot_members = [x for x in chatroot_member_list if x.get('wxid') not in contact_wxids_set] | |||
logger.info(f'{nickname} 在 {chatroom_nickname} 群里还没添加的好友有:{[x.get("nickName") for x in remaining_chatroot_members]}') | |||
logger.info(f'{nickname} 在 {chatroom_nickname} 群里还可以邀请的好友有:{[x.get("nickName") for x in remaining_chatroot_members]}') | |||
for m in remaining_chatroot_members: | |||
ret, msg, data = wxchat.add_group_member_as_friend(token_id, app_id, chatroom_id, m.get('wxid'), f'我是群聊"{chatroom_nickname}"群的{nickname}') | |||
logger.info(f'{nickname} 向 {chatroom_nickname} 群的 {m.get("nickName")} 发送好友邀请 {msg}') | |||
if ret==200: | |||
contact_wxids= m.get('wxid') | |||
history=Models.AddGroupContactsHistory.model_validate({ | |||
"chatroomId":chatroom_id, | |||
"wxid":wxid, | |||
"contactWixd":contact_wxids, | |||
"addTime":int(time.time()) | |||
}) | |||
wxchat.save_group_add_contacts_history(wxid,chatroom_id,contact_wxids,history) | |||
else: | |||
logger.info(f'群好友邀请失败原因:{data}') | |||
logger.info(f'{nickname} 向 {chatroom_nickname}-{chatroom_id} 群的 {m.get("nickName")}-{m.get("wxid")} 发送好友邀请 {msg}') | |||
time.sleep(10) | |||
time.sleep(20) | |||
else: | |||
@@ -42,11 +42,11 @@ def download_image_msg(xml: str): | |||
if response.ok: | |||
data = response.json() | |||
if data['ret'] == 200: | |||
print("Gewe download audio msg successfully.") | |||
print("Gewe download Image msg successfully.") | |||
print(data['data']['fileUrl']) | |||
return data['data']['fileUrl'] | |||
else: | |||
print("Gewe download audio msg in error.") | |||
print("Gewe download Image msg in error.") | |||
return False | |||
else: | |||
return False | |||
@@ -9,4 +9,12 @@ class AgentConfig(BaseModel): | |||
agentTokenId: str | |||
agentEnabled: bool | |||
addContactsFromChatroomIdWhiteList: List[str] = [] | |||
chatWaitingMsgEnabled: bool | |||
chatWaitingMsgEnabled: bool | |||
@dataclass | |||
class AddGroupContactsHistory(BaseModel): | |||
chatroomId:str | |||
wxid:str | |||
contactWixd:str | |||
addTime:int |
@@ -370,7 +370,7 @@ def handle_text_group(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): | |||
return | |||
if from_wxid not in chatroom_id_white_list: | |||
logger.info('群ID不在白名单中,不处理') | |||
logger.info(f'群ID {from_wxid} 不在白名单中,不处理') | |||
return | |||
if '在群聊中@了你' in msg_push_content or '@'+nickname in msg_push_content: | |||
@@ -13,6 +13,10 @@ from io import BytesIO | |||
from PIL import Image | |||
from common import redis_helper | |||
from common.log import logger | |||
from model import Models | |||
from pydantic import TypeAdapter | |||
from typing import List | |||
wxchat=None | |||
@@ -923,7 +927,6 @@ class GeWeChatCom: | |||
ret, msg, data = self.get_chatroom_info(token_id, app_id, chatroom_id) | |||
redis_helper.redis_helper.update_hash_field(hash_key, chatroom_id, json.dumps(data, ensure_ascii=False)) | |||
def get_groups_info_from_cache(self, wxid)->list: | |||
""" | |||
获取群信息保存到 Redis 缓存。 | |||
@@ -983,6 +986,35 @@ class GeWeChatCom: | |||
hash_key = f"__AI_OPS_WX__:LOGINLOCK:{token_id}" | |||
redis_helper.redis_helper.client.delete(hash_key) | |||
def save_group_add_contacts_history(self,wxid,chatroom_id,contact_wxid,history:Models.AddGroupContactsHistory): | |||
''' | |||
保存群加好友历史 | |||
''' | |||
hash_key = f"__AI_OPS_WX__:GROUPS_ADD_CONTACT_HISTORY:{wxid}:{chatroom_id}" | |||
data_str=redis_helper.redis_helper.get_hash_field(hash_key,contact_wxid) | |||
data=json.loads(data_str) if data_str else [] | |||
data.append(history.model_dump()) | |||
redis_helper.redis_helper.update_hash_field(hash_key, contact_wxid, json.dumps(data, ensure_ascii=False)) | |||
def get_group_add_contacts_history(self,wxid,chatroom_id,contact_wxid): | |||
''' | |||
获取群加好友历史 | |||
''' | |||
hash_key = f"__AI_OPS_WX__:GROUPS_ADD_CONTACT_HISTORY:{wxid}:{chatroom_id}" | |||
data_str=redis_helper.redis_helper.get_hash_field(hash_key,contact_wxid) | |||
data=json.loads(data_str) if data_str else [] | |||
#TypeAdapter.validate_python(List[Models.AddGroupContactsHistory], data) | |||
return [Models.AddGroupContactsHistory.model_validate(item) for item in data] | |||
def check_wixd_group_add_contacts_history(self,wxid,chatroom_id): | |||
''' | |||
返回群发送好友达到2次的wxid | |||
''' | |||
hash_key = f"__AI_OPS_WX__:GROUPS_ADD_CONTACT_HISTORY:{wxid}:{chatroom_id}" | |||
cache = redis_helper.redis_helper.get_hash(hash_key) | |||
wxids = [key for key, value in cache.items() if len(json.loads(value)) == 2] | |||
return wxids | |||
def start(): | |||
global wxchat | |||