Explorar el Código

调整自动群加好友

develop
H Vs hace 2 meses
padre
commit
c7a02e5b27
Se han modificado 3 ficheros con 43 adiciones y 27 borrados
  1. +40
    -24
      app.py
  2. +2
    -2
      resources/messages_resource.py
  3. +1
    -1
      wechat/gewe_chat.py

+ 40
- 24
app.py Ver fichero

@@ -86,14 +86,13 @@ def fetch_and_save_contacts2():
#time.sleep(60*10)
time.sleep(3600*1)


def auto_contacts_from_chatroom():
logger.info('自动从群添加好友')
wxchat=gewe_chat.wxchat
while True:
login_keys = list(redis_helper.redis_helper.client.scan_iter(match='__AI_OPS_WX__:LOGININFO:*'))
logger.info(f"Fetching login keys: {login_keys}")
#logger.info(f"Fetching login keys: {login_keys}")

# 遍历每一个获取到的登录键
for k in login_keys:
@@ -104,38 +103,55 @@ def auto_contacts_from_chatroom():
nickname=r.get('nickName')
wxid = r.get('wxid')
status=r.get('status')
if status=='1':
c = wxchat.get_wxchat_config_from_cache(app_id)
contacts = wxchat.get_contacts_brief_from_cache(wxid)
contact_wxids = [c.get('userName') for c in contacts]

chatrooms = c.get('addContactsFromChatroomIdWhiteList',[])
for chatroom_id in chatrooms:
chatroom = wxchat.get_group_info_from_cache(wxid,chatroom_id)
chatroom_nickname=chatroom.get('nickName')

chatroot_member_list=chatroom.get('memberList',[])
contact_set = set(contact_wxids)
remaining_chatroot_members = [x for x in chatroot_member_list if x.get('wxid') not in contact_set]
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}')
time.sleep(3)
time.sleep(3)
else:
logger.info(f'微信ID {wxid} 未登录 {app_id} ,群成员不能定时定时')

# 启动线程处理登录信息
thread = threading.Thread(target=process_add_contacts_from_groups, args=(wxchat,status, nickname, wxid, token_id, app_id))
thread.start()
time.sleep(3)

#time.sleep(60*10)
time.sleep(3600*24)

def process_add_contacts_from_groups(wxchat:gewe_chat.GeWeChatCom,status, nickname, wxid, token_id, app_id):
if status == '1':
c = wxchat.get_wxchat_config_from_cache(wxid)
contacts = wxchat.get_contacts_brief_from_cache(wxid)
contact_wxids = [c.get('userName') for c in contacts]
chatrooms = c.get('addContactsFromChatroomIdWhiteList', [])

for chatroom_id in chatrooms:
chatroom = wxchat.get_group_info_from_cache(wxid, chatroom_id)
chatroom_nickname = chatroom.get('nickName')
chatroom_owner_wxid = chatroom.get('chatroomOwner', None)
admin_wxid = chatroom.get('adminWxid', None)

contact_wxids_set = set(contact_wxids)
if admin_wxid is not None:
contact_wxids_set.add(admin_wxid)
if chatroom_owner_wxid is not None:
contact_wxids_set.add(chatroom_owner_wxid)
contact_wxids_set.add(wxid)
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]}')
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}')
time.sleep(10)
time.sleep(20)
else:
logger.info(f'微信ID {wxid} 未登录 {app_id} ,群成员不能定时定时')

def start_wxchat_thread():
# gewe_chat.start()
scan_wx_login_info()
# 启动同步联系人线程
threading.Thread(target=fetch_and_save_contacts2).start()
#threading.Thread(target=auto_contacts_from_chatroom).start()
threading.Thread(target=auto_contacts_from_chatroom).start()





+ 2
- 2
resources/messages_resource.py Ver fichero

@@ -64,7 +64,7 @@ class MessagesResource(Resource):
chatroom_id=from_wxid
ret,msg,data=gewe_chat.wxchat.save_contract_list(token_id,app_id,chatroom_id,3)
logger.info(f'保存到通讯录 chatroom_id {chatroom_id} {msg}')
gewe_chat.wxchat.update_groups_info_to_cache(token_id,app_id,wxid,chatroom_id)
gewe_chat.wxchat.update_group_info_to_cache(token_id,app_id,wxid,chatroom_id)
handlers[1]=handle_text_group
handlers[3]=handle_image_group
handlers[34]=handle_voice_group
@@ -694,7 +694,7 @@ def handle_10002_msg(token_id,app_id, wxid,msg_data,from_wxid, to_wxid):
chatroom_id=msg_data["FromUserName"]["string"]
ret,msg,data=gewe_chat.wxchat.save_contract_list(token_id,app_id,chatroom_id,3)
logger.info(f'群聊邀请,保存到通讯录 chatroom_id {chatroom_id} {msg}')
gewe_chat.wxchat.update_groups_info_to_cache(token_id,app_id,wxid,chatroom_id)
gewe_chat.wxchat.update_group_info_to_cache(token_id,app_id,wxid,chatroom_id)

if '移出了群聊' in msg_content_xml and 'sysmsgtemplate' in msg_content_xml :
chatroom_id=msg_data["FromUserName"]["string"]


+ 1
- 1
wechat/gewe_chat.py Ver fichero

@@ -876,7 +876,7 @@ class GeWeChatCom:
redis_helper.redis_helper.update_hash_field(hash_key, chatroom_id, json.dumps(data, ensure_ascii=False))
time.sleep(1)
def update_groups_info_to_cache(self, token_id, app_id, wxid, chatroom_id: str):
def update_group_info_to_cache(self, token_id, app_id, wxid, chatroom_id: str):
"""
更新将群信息保存到 Redis 缓存。
"""


Cargando…
Cancelar
Guardar