feat: 关键词发送小程序,以及wxid

This commit is contained in:
hackrobot
2024-03-24 18:19:30 +08:00
parent 011d80e316
commit 5e7767f80a
14 changed files with 167 additions and 94 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

61
main.py
View File

@@ -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)
# 保存好友信息
utils.saveWxidInfo(account_wxid, wxid)
# 发送小程序
utils.sendminiProgram(data, account_wxid, wxid, planetXml)
# 保存好友信息
saveWxidInfo(account_wxid, wxid)
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}'

View File

@@ -8,31 +8,27 @@ planetXml='''<?xml version="1.0"?>
<type>33</type>
<url>https://mp.weixin.qq.com/mp/waerrpage?appid=wxa55815dd741a540d&amp;type=upgrade&amp;upgradetype=3#wechat_redirect</url>
<appattach>
<cdnthumburl>3057020100044b304902010002043866a09902032f591902044da90c79020465fa34a2042433653236333932352d646237622d343330332d613561302d3031663130346562343634630204051408030201000405004c53d900</cdnthumburl>
<cdnthumbmd5>ceccddf866f36866b6fd956330bf32ed</cdnthumbmd5>
<cdnthumblength>69368</cdnthumblength>
<cdnthumburl>3057020100044b304902010002043866a09902032f591902045ba90c79020465fffd3d042438353138636161662d636539352d346166322d393431632d6663393539646437383265390204051408030201000405004c55cd00</cdnthumburl>
<cdnthumbmd5>6619aa9eb04a48f41b34375baa11261c</cdnthumbmd5>
<cdnthumblength>74726</cdnthumblength>
<cdnthumbwidth>720</cdnthumbwidth>
<cdnthumbheight>576</cdnthumbheight>
<cdnthumbaeskey>ab3f1e24e3a0af4c436b308a123bdc02</cdnthumbaeskey>
<aeskey>ab3f1e24e3a0af4c436b308a123bdc02</aeskey>
<cdnthumbaeskey>15c4f51dd6761513e0650fec1bed0373</cdnthumbaeskey>
<aeskey>15c4f51dd6761513e0650fec1bed0373</aeskey>
<encryver>0</encryver>
<filekey>wxid_haqoszjheyuz22_322_1710910860</filekey>
<filekey>wxid_sfvsru738v3122_252_1711275446</filekey>
</appattach>
<sourceusername>gh_735af7f13193@app</sourceusername>
<sourcedisplayname>异度世界的星球</sourcedisplayname>
<md5>ceccddf866f36866b6fd956330bf32ed</md5>
<md5>6619aa9eb04a48f41b34375baa11261c</md5>
<weappinfo>
<username><![CDATA[gh_735af7f13193@app]]></username>
<appid><![CDATA[wxa55815dd741a540d]]></appid>
<type>2</type>
<version>4</version>
<version>8</version>
<weappiconurl><![CDATA[http://mmbiz.qpic.cn/sz_mmbiz_png/43cSpHKSBGyHO7qHeuad8BJLUQ6W9ib7QLfgYicyvGTvpibvLicrS0MaTia3nplqDbWrLIXSfxQgpL3Aib1jGjHjOl9A/640?wx_fmt=png&wxfrom=200]]></weappiconurl>
<pagepath><![CDATA[pages/index/index.html]]></pagepath>
<shareId><![CDATA[0_wxa55815dd741a540d_a96a890b27e8c43d18e2e75fda2a9215_1710910859_0]]></shareId>
<pkginfo>
<type>2</type>
<md5><![CDATA[887a279c203c0af7f0dacdfe17948305]]></md5>
</pkginfo>
<shareId><![CDATA[0_wxa55815dd741a540d_a96a890b27e8c43d18e2e75fda2a9215_1711275445_0]]></shareId>
<appservicetype>0</appservicetype>
<brandofficialflag>0</brandofficialflag>
<showRelievedBuyFlag>0</showRelievedBuyFlag>

46
text.xml Normal file
View File

@@ -0,0 +1,46 @@
<?xml version="1.0"?>
<msg>
<appmsg appid="" sdkver="0">
<title>异度世界的星球</title>
<des>异度世界的星球</des>
<type>33</type>
<url>https://mp.weixin.qq.com/mp/waerrpage?appid=wxa55815dd741a540d&amp;type=upgrade&amp;upgradetype=3#wechat_redirect</url>
<appattach>
<cdnthumburl>3057020100044b304902010002043866a09902032f591902045ba90c79020465fffd3d042438353138636161662d636539352d346166322d393431632d6663393539646437383265390204051408030201000405004c55cd00</cdnthumburl>
<cdnthumbmd5>6619aa9eb04a48f41b34375baa11261c</cdnthumbmd5>
<cdnthumblength>74726</cdnthumblength>
<cdnthumbwidth>720</cdnthumbwidth>
<cdnthumbheight>576</cdnthumbheight>
<cdnthumbaeskey>15c4f51dd6761513e0650fec1bed0373</cdnthumbaeskey>
<aeskey>15c4f51dd6761513e0650fec1bed0373</aeskey>
<encryver>0</encryver>
<filekey>wxid_sfvsru738v3122_252_1711275446</filekey>
</appattach>
<sourceusername>gh_735af7f13193@app</sourceusername>
<sourcedisplayname>异度世界的星球</sourcedisplayname>
<md5>6619aa9eb04a48f41b34375baa11261c</md5>
<weappinfo>
<username><![CDATA[gh_735af7f13193@app]]></username>
<appid><![CDATA[wxa55815dd741a540d]]></appid>
<type>2</type>
<version>8</version>
<weappiconurl><![CDATA[http://mmbiz.qpic.cn/sz_mmbiz_png/43cSpHKSBGyHO7qHeuad8BJLUQ6W9ib7QLfgYicyvGTvpibvLicrS0MaTia3nplqDbWrLIXSfxQgpL3Aib1jGjHjOl9A/640?wx_fmt=png&wxfrom=200]]></weappiconurl>
<pagepath><![CDATA[pages/index/index.html]]></pagepath>
<shareId><![CDATA[0_wxa55815dd741a540d_a96a890b27e8c43d18e2e75fda2a9215_1711275445_0]]></shareId>
<appservicetype>0</appservicetype>
<brandofficialflag>0</brandofficialflag>
<showRelievedBuyFlag>0</showRelievedBuyFlag>
<hasRelievedBuyPlugin>0</hasRelievedBuyPlugin>
<flagshipflag>0</flagshipflag>
<subType>0</subType>
<isprivatemessage>0</isprivatemessage>
</weappinfo>
</appmsg>
<fromusername>wxid_4413224132412</fromusername>
<scene>0</scene>
<appinfo>
<version>1</version>
<appname></appname>
</appinfo>
<commenturl></commenturl>
</msg>

62
utils.py Normal file
View File

@@ -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)