Parcourir la source

自聊命令

develop
H Vs il y a 2 mois
Parent
révision
c6468b9285
3 fichiers modifiés avec 40 ajouts et 5 suppressions
  1. +4
    -2
      app.py
  2. +35
    -2
      resources/messages_resource.py
  3. +1
    -1
      wechat/gewe_chat.py

+ 4
- 2
app.py Voir le fichier

@@ -109,14 +109,14 @@ def auto_add_contacts_from_chatrooms():
status=r.get('status')

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

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

def process_add_contacts_from_chatrooms(wxchat:gewe_chat.GeWeChatCom,status, nickname, wxid, token_id, app_id):
def process_add_contacts_from_chatrooms(wxchat:gewe_chat.GeWeChatCom,status, wxid, token_id, app_id):
if status == '1':
c = wxchat.get_wxchat_config_from_cache(wxid)
@@ -145,6 +145,8 @@ def process_add_contacts_from_chatrooms(wxchat:gewe_chat.GeWeChatCom,status, nic
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]
nickname = next((member['nickName'] for member in chatroot_member_list if member['nickName'] == wxid), None)

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}')


+ 35
- 2
resources/messages_resource.py Voir le fichier

@@ -35,6 +35,17 @@ class MessagesResource(Resource):
wxid = msg.get("Wxid",'')
wx_config = gewe_chat.wxchat.get_wxchat_config_from_cache(wxid)

# 自发命令
# if type_name=='AddMsg':
# from_wxid = msg_data["FromUserName"]["string"]
# if wxid=='from_wxid':
# pass
if type_name == 'AddMsg':
handle_self_cmd(wxid,msg)

if not bool(wx_config.get("agentEnabled",False)):
logger.info(f'微信ID {wxid} 未托管,不处理')
return jsonify({"message": "收到微信回调消息"})
@@ -110,9 +121,28 @@ class MessagesResource(Resource):
logger.warning(f"未知消息类型")

return jsonify({"message": "收到微信回调消息"})

def handle_self_cmd(wxid,msg):
msg_data=msg.get("Data")
from_wxid=msg_data["FromUserName"]["string"]
msg_content=msg_data["Content"]["string"]
if wxid == from_wxid:
commands = {
'启用托管': True,
'关闭托管': False
}
if msg_content in commands:
c_dict = gewe_chat.wxchat.get_wxchat_config_from_cache(wxid)
if c_dict:
from model import Models
agent_config=Models.AgentConfig.model_validate(c_dict)
agent_config.agentEnabled=commands[msg_content]
gewe_chat.wxchat.save_wxchat_config(wxid, agent_config.model_dump())
logger.info(f'{wxid} {"启动" if commands[msg_content] else "关闭"}托管')

def handle_text(token_id,app_id, wxid,msg_data,from_wxid, to_wxid):

'''
私聊文本消息
'''
@@ -237,7 +267,7 @@ def handle_text(token_id,app_id, wxid,msg_data,from_wxid, to_wxid):
# 取消定时器
timeout_timer.cancel()

def check_timeout( task_thread:threading.Thread, token_id,wxid, app_id, callback_to_user):
def check_timeout(task_thread:threading.Thread, token_id,wxid, app_id, callback_to_user):
if task_thread.is_alive():
print(f"任务运行时间超过{timeout_duration}秒,token_id={token_id}, app_id={app_id}, callback_to_user={callback_to_user}")
wx_config = gewe_chat.wxchat.get_wxchat_config_from_cache(wxid)
@@ -873,6 +903,9 @@ def check_chatroom(userName):
return True
return False




# def remove_markdown_symbol(text: str):
# # 移除markdown格式,目前先移除**
# if not text or not isinstance(text, str):


+ 1
- 1
wechat/gewe_chat.py Voir le fichier

@@ -953,7 +953,7 @@ class GeWeChatCom:
config = redis_helper.redis_helper.get_hash_field(hash_key, wxid)
return json.loads(config) if config else {}
def save_wxchat_config(self, wxid, config):
def save_wxchat_config(self, wxid, config:dict):
"""
保存配置信息
"""


Chargement…
Annuler
Enregistrer