This commit is contained in:
hackrobot
2024-04-11 12:47:20 +08:00
parent 3c77b48106
commit 2849922338

52
main.py
View File

@@ -31,16 +31,18 @@ from urllib.parse import quote, unquote
import re import re
import emoji import emoji
models.Base.metadata.create_all(bind=engine) models.Base.metadata.create_all(bind=engine)
app = FastAPI() app = FastAPI()
oldEmoji = "异度世界🤖"
testEmo="异度世界🤖" original_string = WXBotService.emoji_to_unicode(oldEmoji)
print("original_string", original_string)
newtestEmo=WXBotService.emoji_to_unicode(testEmo) # 定义正则表达式模式,匹配 \\uXXXX 形式的 Unicode 编码
print ("newtestEmo",newtestEmo) unicode_regex = r'\\u([0-9a-fA-F]{4})'
# 使用正则表达式替换
converted_string = re.sub(unicode_regex, r'[@emoji=\1]', original_string)
print("converted_string", converted_string)
# Dependency # Dependency
@@ -107,7 +109,9 @@ def addH5openid(h5openid, wxid, db: Session = Depends(get_db)):
# 机器人id # 机器人id
account_wxid = config.account_wxid account_wxid = config.account_wxid
# 新增用户 # 新增用户
new_user = utils.saveWxidInfo(account_wxid=config.account_wxid,wxid=wxid, h5openid=h5openid) new_user = utils.saveWxidInfo(account_wxid=config.account_wxid,
wxid=wxid,
h5openid=h5openid)
# "errmsg":r['errmsg'],,"errcode":r['errcode'] # "errmsg":r['errmsg'],,"errcode":r['errcode']
# return {"session_key":r['session_key'],"unionid":r['unionid'],"openid":r['openid']} # return {"session_key":r['session_key'],"unionid":r['unionid'],"openid":r['openid']}
return new_user return new_user
@@ -146,16 +150,16 @@ def h5group(nickname, db: Session = Depends(get_db)):
pattern = r'收到(\w+)的在看' pattern = r'收到(\w+)的在看'
print(newtext) print(newtext)
matches = re.findall(pattern, newtext) matches = re.findall(pattern, newtext)
print("matches",matches) print("matches", matches)
matchesString=None matchesString = None
if len(matches)>0: if len(matches) > 0:
matchesString=matches[0] matchesString = matches[0]
print("matchesString",matchesString) print("matchesString", matchesString)
if matchesString: if matchesString:
matchesString=remove_emojis(matchesString) #删除emoji字符串 matchesString = remove_emojis(matchesString) #删除emoji字符串
print("匹配到的结果:", matchesString) print("匹配到的结果:", matchesString)
# 判断是否在群中 # 判断是否在群中
inGroup = False inGroup = False
groupList = WXBotService.get_group_members_list( groupList = WXBotService.get_group_members_list(
robot_wxid=config.account_wxid, from_wxid=config.chatroomId) robot_wxid=config.account_wxid, from_wxid=config.chatroomId)
@@ -170,15 +174,16 @@ def h5group(nickname, db: Session = Depends(get_db)):
allFriend = WXBotService.get_address_list(config.account_wxid) allFriend = WXBotService.get_address_list(config.account_wxid)
for friendItem in allFriend['data']["contactList"]: for friendItem in allFriend['data']["contactList"]:
# if friendItem['nickname'] == matchesString: # if friendItem['nickname'] == matchesString:
if matchesString in friendItem['nickname'] : if matchesString in friendItem['nickname']:
new_wxid = friendItem['wxid'] new_wxid = friendItem['wxid']
else: else:
# print('通过nickname查不到wxid') # print('通过nickname查不到wxid')
pass pass
if new_wxid: if new_wxid:
WXBotService.send_text_message(robot_wxid=config.account_wxid, WXBotService.send_text_message(
from_wxid=new_wxid, robot_wxid=config.account_wxid,
msg=f'已收到-[在看]支持,谢谢,即将发送群聊邀请\n[本消息是机器人自动回复]') from_wxid=new_wxid,
msg=f'已收到-[在看]支持,谢谢,即将发送群聊邀请\n[本消息是机器人自动回复]')
WXBotService.add_group_members_40_more( WXBotService.add_group_members_40_more(
robot_wxid=config.account_wxid, robot_wxid=config.account_wxid,
from_wxid=config.chatroomId, #异度星球群 from_wxid=config.chatroomId, #异度星球群
@@ -191,7 +196,6 @@ def h5group(nickname, db: Session = Depends(get_db)):
print("未找到匹配项,或者有特殊字符") print("未找到匹配项,或者有特殊字符")
# 创建用户 # 创建用户
@app.post("/users") @app.post("/users")
# schemas.UserCreate决定了swagger文档 # schemas.UserCreate决定了swagger文档
@@ -243,6 +247,7 @@ def helloget(q: dict):
def remove_emojis(text): def remove_emojis(text):
return ''.join(c for c in text if c not in emoji.UNICODE_EMOJI) return ''.join(c for c in text if c not in emoji.UNICODE_EMOJI)
# 监听机器人post日志消息 # 监听机器人post日志消息
@app.post("/wxbot") @app.post("/wxbot")
# def hellopost(requestData:schemas.User): # def hellopost(requestData:schemas.User):
@@ -365,18 +370,19 @@ def kp(event_data):
# WXBotService.add_group_members_40_more(robot_wxid='wxid_sfvsru738v3122', # WXBotService.add_group_members_40_more(robot_wxid='wxid_sfvsru738v3122',
# from_wxid='49187487591@chatroom', # from_wxid='49187487591@chatroom',
# new_wxid='wxid_4413224132412') # new_wxid='wxid_4413224132412')
print("data",data) print("data", data)
if "emoji" in wx_name: if "emoji" in wx_name:
# 发送小程序 # 发送小程序
utils.sendminiProgram(data, account_wxid, wxid, planetXml) utils.sendminiProgram(data, account_wxid, wxid, planetXml)
else: else:
# H5链接 # H5链接
WXBotService.send_link_message( WXBotService.send_link_message(
account_wxid, account_wxid,
wxid, wxid,
title="欢迎加入微信群", title="欢迎加入微信群",
desc="请查看规则", desc="请查看规则",
url=f'https://link.hackrobot.cn#/pages/index/index?wxid={wxid}', url=
f'https://link.hackrobot.cn#/pages/index/index?wxid={wxid}',
pic= pic=
"https://www.hackrobot.cn/wp-content/uploads/2023/05/cropped-cropped-Screenshot-2023-05-22-at-17.26.38.png" "https://www.hackrobot.cn/wp-content/uploads/2023/05/cropped-cropped-Screenshot-2023-05-22-at-17.26.38.png"
) )