feat: 关键词发送小程序,以及wxid
This commit is contained in:
61
main.py
61
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}'
|
||||
|
||||
Reference in New Issue
Block a user