diff --git a/__pycache__/WXBotService.cpython-310.pyc b/__pycache__/WXBotService.cpython-310.pyc new file mode 100644 index 0000000..ad4f439 Binary files /dev/null and b/__pycache__/WXBotService.cpython-310.pyc differ diff --git a/__pycache__/config.cpython-310.pyc b/__pycache__/config.cpython-310.pyc new file mode 100644 index 0000000..3b8704a Binary files /dev/null and b/__pycache__/config.cpython-310.pyc differ diff --git a/__pycache__/crud.cpython-310.pyc b/__pycache__/crud.cpython-310.pyc new file mode 100644 index 0000000..d3dc328 Binary files /dev/null and b/__pycache__/crud.cpython-310.pyc differ diff --git a/__pycache__/database.cpython-310.pyc b/__pycache__/database.cpython-310.pyc new file mode 100644 index 0000000..bafe19b Binary files /dev/null and b/__pycache__/database.cpython-310.pyc differ diff --git a/__pycache__/main.cpython-310.pyc b/__pycache__/main.cpython-310.pyc new file mode 100644 index 0000000..218daf4 Binary files /dev/null and b/__pycache__/main.cpython-310.pyc differ diff --git a/__pycache__/models.cpython-310.pyc b/__pycache__/models.cpython-310.pyc new file mode 100644 index 0000000..577dcc9 Binary files /dev/null and b/__pycache__/models.cpython-310.pyc differ diff --git a/__pycache__/planet.cpython-310.pyc b/__pycache__/planet.cpython-310.pyc new file mode 100644 index 0000000..3aedbf0 Binary files /dev/null and b/__pycache__/planet.cpython-310.pyc differ diff --git a/__pycache__/schemas.cpython-310.pyc b/__pycache__/schemas.cpython-310.pyc new file mode 100644 index 0000000..7210e47 Binary files /dev/null and b/__pycache__/schemas.cpython-310.pyc differ diff --git a/__pycache__/utils.cpython-310.pyc b/__pycache__/utils.cpython-310.pyc new file mode 100644 index 0000000..2f21364 Binary files /dev/null and b/__pycache__/utils.cpython-310.pyc differ diff --git a/kunpeng.db b/kunpeng.db index 67c6898..2670b8f 100644 Binary files a/kunpeng.db and b/kunpeng.db differ diff --git a/main.py b/main.py index 50133d0..26f629b 100644 --- a/main.py +++ b/main.py @@ -22,6 +22,7 @@ from planet import planetXml from lxml import etree import xml.dom.minidom from pydantic import BaseModel +import utils models.Base.metadata.create_all(bind=engine) @@ -104,42 +105,6 @@ def hellopost(requestData: dict): return json.dumps({"Code": 0}) -# 保存好友信息 -def saveWxidInfo(account_wxid, wxid): - allFriend = WXBotService.get_address_list(account_wxid) - for friendItem in allFriend['data']["contactList"]: - if friendItem['wxid'] == wxid: - friendItemObj = models.User(**friendItem) - # 当前的路由函数 - create_user(db=SessionLocal(), user=friendItemObj) - - -# 发送小程序模版,建议远程更新 -def sendminiProgram(data, account_wxid, wxid, msgXml): - # 发送小程序 - if data.get('isSender') != 1: - root = ET.fromstring(msgXml) #xml-字符串形式 - - #打开xml文档 - # current_dir = os.path.dirname(os.path.abspath(__file__)) - # print('current_dir+/planet.xml',current_dir+'\planet.xml') - # tree = ET.parse(current_dir+'\planet.xml') - # root = tree.getroot() - - # 微信好友的wxid - # wxid='wxid_w2imh7ki5s4q12' - for pagepath in root.iter('pagepath'): - print(pagepath.text) # 打印名字 - pagepath.text = pagepath.text + f'?wxid={wxid}' - print('修改path', pagepath.text) # 打印名字 - - # 修改后的xml转成string , method='xml' - msgXml = ET.tostring(root, encoding='utf-8').decode('utf-8') - WXBotService.send_xml_message( - account_wxid, wxid, msgXml - # account_wxid, "wxid_w2imh7ki5s4q12", planetXml - ) - def kp(event_data): # unicode [\\uxxxxx] 转 emoji 表情 @@ -233,15 +198,20 @@ def kp(event_data): if msg_type == 1: # 1 文本消息/自带表情 kp_log = f'{type_str} 文本消息 内容:{msg}' - # 将好友信息=>保存到数据库 - newFriendDict = WXBotService.get_friend_info(account_wxid, wxid) - # print('newFriendDict[data]---',newFriendDict['data']) - userClass = models.User(**(newFriendDict['data'])) - # print('userClass====',type(userClass)) - # crud.create_user(db=SessionLocal(),user=userClass) + if msg in ['加群']: + # 将好友信息=>保存到数据库 + newFriendDict = WXBotService.get_friend_info(account_wxid, wxid) + # print('newFriendDict[data]---',newFriendDict['data']) + userClass = models.User(**(newFriendDict['data'])) + # print('userClass====',type(userClass)) + # crud.create_user(db=SessionLocal(),user=userClass) - # 保存好友信息 - saveWxidInfo(account_wxid, wxid) + # 保存好友信息 + + utils.saveWxidInfo(account_wxid, wxid) + # 发送小程序 + utils.sendminiProgram(data, account_wxid, wxid, planetXml) + elif msg_type == 3: # 3 图片消息 kp_log = f'{type_str} 图片消息 内容:{filepath}' @@ -316,8 +286,7 @@ def kp(event_data): f'appid:{xml_appmsg.findtext("weappinfo/appid")} ' \ f'pagepath:{xml_appmsg.findtext("weappinfo/pagepath")}' print("---------------------------kp_log", msg) - # 发送小程序 - sendminiProgram(data, account_wxid, wxid, planetXml) + elif xml_type == '57': # 引用消息 kp_log = f'{type_str} 引用消息 内容:{msg}' diff --git a/planet.py b/planet.py index 2abfc8a..05f27c9 100644 --- a/planet.py +++ b/planet.py @@ -1,52 +1,48 @@ #第28行,cmder手动复制,会丢失信息, 可以写到text文件先 -planetXml=''' - - - 异度世界的星球 - 异度世界的星球 - 33 - https://mp.weixin.qq.com/mp/waerrpage?appid=wxa55815dd741a540d&type=upgrade&upgradetype=3#wechat_redirect - - 3057020100044b304902010002043866a09902032f591902044da90c79020465fa34a2042433653236333932352d646237622d343330332d613561302d3031663130346562343634630204051408030201000405004c53d900 - ceccddf866f36866b6fd956330bf32ed - 69368 - 720 - 576 - ab3f1e24e3a0af4c436b308a123bdc02 - ab3f1e24e3a0af4c436b308a123bdc02 - 0 - wxid_haqoszjheyuz22_322_1710910860 - - gh_735af7f13193@app - 异度世界的星球 - ceccddf866f36866b6fd956330bf32ed - - - - 2 - 4 +planetXml=''' + + + 异度世界的星球 + 异度世界的星球 + 33 + https://mp.weixin.qq.com/mp/waerrpage?appid=wxa55815dd741a540d&type=upgrade&upgradetype=3#wechat_redirect + + 3057020100044b304902010002043866a09902032f591902045ba90c79020465fffd3d042438353138636161662d636539352d346166322d393431632d6663393539646437383265390204051408030201000405004c55cd00 + 6619aa9eb04a48f41b34375baa11261c + 74726 + 720 + 576 + 15c4f51dd6761513e0650fec1bed0373 + 15c4f51dd6761513e0650fec1bed0373 + 0 + wxid_sfvsru738v3122_252_1711275446 + + gh_735af7f13193@app + 异度世界的星球 + 6619aa9eb04a48f41b34375baa11261c + + + + 2 + 8 - - - - 2 - - - 0 - 0 - 0 - 0 - 0 - 0 - 0 - - - wxid_4413224132412 - 0 - - 1 - - - + + + 0 + 0 + 0 + 0 + 0 + 0 + 0 + + + wxid_4413224132412 + 0 + + 1 + + + ''' \ No newline at end of file diff --git a/text.xml b/text.xml new file mode 100644 index 0000000..90a20f6 --- /dev/null +++ b/text.xml @@ -0,0 +1,46 @@ + + + + 异度世界的星球 + 异度世界的星球 + 33 + https://mp.weixin.qq.com/mp/waerrpage?appid=wxa55815dd741a540d&type=upgrade&upgradetype=3#wechat_redirect + + 3057020100044b304902010002043866a09902032f591902045ba90c79020465fffd3d042438353138636161662d636539352d346166322d393431632d6663393539646437383265390204051408030201000405004c55cd00 + 6619aa9eb04a48f41b34375baa11261c + 74726 + 720 + 576 + 15c4f51dd6761513e0650fec1bed0373 + 15c4f51dd6761513e0650fec1bed0373 + 0 + wxid_sfvsru738v3122_252_1711275446 + + gh_735af7f13193@app + 异度世界的星球 + 6619aa9eb04a48f41b34375baa11261c + + + + 2 + 8 + + + + 0 + 0 + 0 + 0 + 0 + 0 + 0 + + + wxid_4413224132412 + 0 + + 1 + + + + \ No newline at end of file diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..1944f48 --- /dev/null +++ b/utils.py @@ -0,0 +1,62 @@ +from fastapi import Depends, FastAPI, HTTPException +from sqlalchemy.orm import Session + +# from . import crud, models, schemas +import crud, models, schemas +from database import SessionLocal, engine +# from .database import SessionLocal, engine + +import json +import os +import threading +import time +from pydantic import BaseModel +from flask import request, Flask +try: + import xml.etree.cElementTree as ET +except ImportError: + import xml.etree.ElementTree as ET + +import WXBotService +from planet import planetXml +from lxml import etree +import xml.dom.minidom +from pydantic import BaseModel + + +# 保存好友信息 +def saveWxidInfo(account_wxid, wxid): + allFriend = WXBotService.get_address_list(account_wxid) + for friendItem in allFriend['data']["contactList"]: + if friendItem['wxid'] == wxid: + friendItemObj = models.User(**friendItem) + db_user = crud.get_user_by_wxid(db=SessionLocal(), wxid=wxid) + if db_user: + print("wxid already registered") + # return db_user + else: + crud.create_user(db=SessionLocal(), user=friendItemObj) + + +# 发送小程序模版,建议远程更新 +def sendminiProgram(data, account_wxid, wxid, msgXml): + # 发送小程序 + if data.get('isSender') != 1: + root = ET.fromstring(msgXml) #xml-字符串形式 + + #打开xml文档 + # current_dir = os.path.dirname(os.path.abspath(__file__)) + # print('current_dir+/planet.xml',current_dir+'\planet.xml') + # tree = ET.parse(current_dir+'\planet.xml') + # root = tree.getroot() + + # 微信好友的wxid + # wxid='wxid_w2imh7ki5s4q12' + for pagepath in root.iter('pagepath'): + print(pagepath.text) # 打印名字 + pagepath.text = pagepath.text + f'?wxid={wxid}' + print('修改path', pagepath.text) # 打印名字 + + # 修改后的xml转成string , method='xml' + msgXml = ET.tostring(root, encoding='utf-8').decode('utf-8') + WXBotService.send_xml_message(account_wxid, wxid, msgXml)