@@ -108,7 +108,7 @@ def fetch_and_save_contacts2(): | |||
wxid = r.get('wxid') | |||
status=r.get('status') | |||
if status=='1': | |||
contacts_list = wxchat.fetch_contacts_list(token_id, app_id) | |||
ret,msg,contacts_list = wxchat.fetch_contacts_list(token_id, app_id) | |||
friend_wxids = contacts_list['friends'][3:] # 可以调整截取范围 | |||
#friend_wxids.remove('weixin') | |||
wxchat.save_contacts_brief_to_cache(token_id, app_id, wxid, friend_wxids) | |||
@@ -194,7 +194,7 @@ def app_run(): | |||
flask_api.add_resource(MessagesResource, '/messages') | |||
flask_api.add_resource(DeleteFriendResource, '/api/contacts/deletefriend') | |||
flask_api.add_resource(GetFriendsInfoResource, '/api/contacts/getfriends') | |||
@@ -27,6 +27,7 @@ class SaveWxchatConfigResource(Resource): | |||
req = request.get_json() | |||
wxid = req.get("wxid") | |||
data = req.get("data") | |||
hash_key="__AI_OPS_WX__:WXCHAT_CONFIG" | |||
redis_helper.redis_helper.update_hash_field(hash_key, wxid, json.dumps(data,ensure_ascii=False)) | |||
# hash_key="__AI_OPS_WX__:WXCHAT_CONFIG" | |||
# redis_helper.redis_helper.update_hash_field(hash_key, wxid, json.dumps(data,ensure_ascii=False)) | |||
self.wxchat.save_wxchat_config(wxid, data) | |||
return jsonify({'wxid': wxid, 'config': data}) |
@@ -68,8 +68,9 @@ class MessagesResource(Resource): | |||
''' | |||
好友通过验证及好友资料变更的通知消息 | |||
''' | |||
wxid = msg.get("Wxid") | |||
msg_data = msg.get("Data") | |||
handle_mod_contacts(token_id,app_id,msg_data) | |||
handle_mod_contacts(token_id,app_id,wxid,msg_data) | |||
elif type_name=="DelContacts": | |||
''' | |||
删除好友通知/退出群聊 | |||
@@ -563,39 +564,55 @@ def handle_10002_msg(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): | |||
print('撤回消息,拍一拍消息,地理位置') | |||
def handle_mod_contacts(token_id,app_id,msg_data): | |||
def handle_mod_contacts(token_id,app_id,wxid,msg_data): | |||
''' | |||
好友通过验证及好友资料变更的通知消息 | |||
''' | |||
logger.info('好友通过验证及好友资料变更的通知消息') | |||
if not check_chatroom(msg_data["UserName"]["string"]): | |||
contact_wxid = msg_data["UserName"]["string"] | |||
keys=redis_helper.redis_helper.client.keys('__AI_OPS_WX__:LOGININFO:*') | |||
wxid="" | |||
for k in keys: | |||
key=k.decode('utf-8') | |||
hash_key=f"__AI_OPS_WX__:LOGININFO:{key}" | |||
cache_app_id=redis_helper.redis_helper.get_hash_field(hash_key,"appId") | |||
if app_id==cache_app_id: | |||
wxid=redis_helper.redis_helper.get_hash_field(hash_key,"wxid") | |||
break | |||
if wxid!="": | |||
hash_key=f"__AI_OPS_WX__:CONTACTS_BRIEF:{wxid}" | |||
cache_str=redis_helper.redis_helper.get_hash_field(hash_key,"data") | |||
cache = json.loads(cache_str) if cache_str else [] | |||
for c in cache: | |||
if c["userName"]==contact_wxid: | |||
c["nickName"] = msg_data["NickName"] | |||
c["snsBgImg"] = msg_data["SnsBgimgId"] | |||
c["smallHeadImgUrl"] = msg_data["SmallHeadImgUrl"] | |||
c["signature"]= msg_data["Signature"] | |||
# 更新好友信息 | |||
ret,msg,contacts_list = gewe_chat.wxchat.fetch_contacts_list(token_id, app_id) | |||
friend_wxids = contacts_list['friends'][3:] # 可以调整截取范围 | |||
print(friend_wxids) | |||
#friend_wxids.remove('weixin') | |||
gewe_chat.wxchat.save_contacts_brief_to_cache(token_id, app_id, wxid, friend_wxids) | |||
# keys=redis_helper.redis_helper.client.keys('__AI_OPS_WX__:LOGININFO:*') | |||
# wxid="" | |||
# for k in keys: | |||
# key=k.decode('utf-8') | |||
# hash_key=f"__AI_OPS_WX__:LOGININFO:{key}" | |||
# cache_app_id=redis_helper.redis_helper.get_hash_field(hash_key,"appId") | |||
# if app_id==cache_app_id: | |||
# wxid=redis_helper.redis_helper.get_hash_field(hash_key,"wxid") | |||
# break | |||
# print(wxid) | |||
# print('~~~~~~~~~~~~~~~~~~~~~~~~~~~\n') | |||
# if wxid!="": | |||
# print('~~~~~~~~~~~~~~~~~~~~~~~~~~~\n') | |||
# hash_key=f"__AI_OPS_WX__:CONTACTS_BRIEF:{wxid}" | |||
# ret,msg,contacts_list = gewe_chat.wxchat.fetch_contacts_list(token_id, app_id) | |||
# friend_wxids = contacts_list['friends'][3:] # 可以调整截取范围 | |||
# print(friend_wxids) | |||
# #friend_wxids.remove('weixin') | |||
# gewe_chat.wxchat.save_contacts_brief_to_cache(token_id, app_id, wxid, friend_wxids) | |||
# cache_str=redis_helper.redis_helper.get_hash_field(hash_key,"data") | |||
# cache = json.loads(cache_str) if cache_str else [] | |||
# for c in cache: | |||
# if c["userName"]==contact_wxid: | |||
# c["nickName"] = msg_data["NickName"] | |||
# c["snsBgImg"] = msg_data["SnsBgimgId"] | |||
# c["smallHeadImgUrl"] = msg_data["SmallHeadImgUrl"] | |||
# c["signature"]= msg_data["Signature"] | |||
# 更新缓存,如果有修改过的话 | |||
if cache: | |||
updated_cache_str = json.dumps(cache) | |||
redis_helper.redis_helper.set_hash_field(hash_key, "data", updated_cache_str) | |||
# # 更新缓存,如果有修改过的话 | |||
# if cache: | |||
# updated_cache_str = json.dumps(cache) | |||
# redis_helper.redis_helper.set_hash_field(hash_key, "data", updated_cache_str) | |||
else: | |||
logger.info('群聊好友通过验证及好友资料变更的通知消息') | |||
@@ -266,7 +266,7 @@ def fetch_and_save_contacts(wxchat:gewe_chat.GeWeChatCom, token_id, app_id, hash | |||
""" | |||
获取联系人列表并保存到缓存 | |||
""" | |||
contacts_list = wxchat.fetch_contacts_list(token_id, app_id) | |||
ret,msg,contacts_list = wxchat.fetch_contacts_list(token_id, app_id) | |||
friend_wxids = contacts_list['friends'][3:] # 可以调整截取范围 | |||
wxid = redis_helper.redis_helper.get_hash_field(hash_key, 'wxid') | |||
@@ -194,9 +194,8 @@ class GeWeChatCom: | |||
"appId": app_id | |||
} | |||
response = requests.post(url=api_url, headers=headers, data=json.dumps(data)) | |||
response_data = response.json() | |||
print(response_data) | |||
return response_data.get('data') | |||
response_object = response.json() | |||
return response_object.get('ret',None),response_object.get('msg',None),response_object.get('data',None) | |||
def fetch_contacts_list_cache(self, token_id, app_id): | |||
''' | |||
@@ -536,6 +535,23 @@ class GeWeChatCom: | |||
response_object = response.json() | |||
return response_object.get('ret',None),response_object.get('msg',None),response_object.get('data',None) | |||
def send_text_sns(self, token_id, app_id,content): | |||
''' | |||
发送文字朋友圈 | |||
''' | |||
api_url = f"{self.base_url}/v2/api/sns/sendTextSns" | |||
headers = { | |||
'X-GEWE-TOKEN': token_id, | |||
'Content-Type': 'application/json' | |||
} | |||
data = { | |||
"appId": app_id, | |||
"content": content | |||
} | |||
response = requests.post(url=api_url, headers=headers, data=json.dumps(data)) | |||
response_object = response.json() | |||
return response_object.get('ret',None),response_object.get('msg',None),response_object.get('data',None) | |||
############################### 其他 ############################### | |||
def save_contacts_brief_to_cache(self, token_id, app_id, wxid, contacts_wxids: list)->list: | |||
@@ -656,6 +672,13 @@ class GeWeChatCom: | |||
hash_key = f"__AI_OPS_WX__:WXCHAT_CONFIG" | |||
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): | |||
""" | |||
保存配置信息 | |||
""" | |||
hash_key = f"__AI_OPS_WX__:WXCHAT_CONFIG" | |||
redis_helper.redis_helper.update_hash_field(hash_key, wxid, json.dumps(config, ensure_ascii=False)) | |||
def start(): | |||
global wxchat | |||