This commit is contained in:
hackrobot
2024-04-11 17:20:31 +08:00
parent f10ada4e79
commit 7bade2d633

49
main.py
View File

@@ -35,14 +35,15 @@ models.Base.metadata.create_all(bind=engine)
app = FastAPI()
# 全局消息变量
globalMsgData=None
globalMsgData = None
def is_chinese(char):
"""检查字符是否为中文字符。"""
return '\u4e00' <= char <= '\u9fff'
def convert_special_chars_to_emoji(input_string):
output = []
for char in input_string:
@@ -57,13 +58,11 @@ def convert_special_chars_to_emoji(input_string):
emoji_representation = f"[@emoji={code_point}]"
# 将非中文且非 ASCII 字符替换为 emoji 代码点形式
output.append(emoji_representation)
# 将列表中的字符组合成一个字符串
return ''.join(output)
# 转换emoji字符串为[]
def transOldemoji(oldEmoji=None):
# oldEmoji = "异度世界🤖"
@@ -82,13 +81,13 @@ def transOldemoji(oldEmoji=None):
# 在 Unicode 编码后添加 ']'
# 如果原始字符串中的 \\uXXX 是在方括号中,需要保留方括号
# 替换后,如果需要,可以调整这一步骤,根据特定需求
converted_string = re.sub(r'\[@emoji=[0-9a-fA-F]{4}\]', lambda match: f'{match.group()}]', converted_string)
converted_string=converted_string.replace('[\\@', '[@')
converted_string = re.sub(r'\[@emoji=[0-9a-fA-F]{4}\]',
lambda match: f'{match.group()}]',
converted_string)
converted_string = converted_string.replace('[\\@', '[@')
# 输出转换后的字符串
print(converted_string)
return converted_string
# Dependency
@@ -195,15 +194,15 @@ def h5group(nickname, db: Session = Depends(get_db)):
newtext = unquote(nickname, 'utf-8')
# pattern = r'收到(\w+)的在看'
pattern = r'收到([\w\[\]@=]+)的在看'
print("newtext",newtext)
print("newtext", newtext)
# 处理emoji转为[@emoji=1f916] 格式
newtext=transOldemoji(newtext)
print("newtextemoji",newtext)
newtext = transOldemoji(newtext)
print("newtextemoji", newtext)
# 不是 ASCII 字符,则获取该字符的 Unicode 代码点
newtext = convert_special_chars_to_emoji(newtext)
print("newtextASI",newtext)
print("newtextASI", newtext)
matches = re.findall(pattern, newtext)
print("matches", matches)
matchesString = None
@@ -229,25 +228,29 @@ def h5group(nickname, db: Session = Depends(get_db)):
allFriend = WXBotService.get_address_list(config.account_wxid)
for friendItem in allFriend['data']["contactList"]:
if friendItem['nickname'] == matchesString:
# if matchesString in friendItem['nickname']:
# if matchesString in friendItem['nickname']:
new_wxid = friendItem['wxid']
else:
# print('通过nickname查不到wxid')
pass
if new_wxid:
WXBotService.send_text_message(
robot_wxid=config.account_wxid,
from_wxid=new_wxid,
msg=f'已收到-[在看]支持,谢谢,即将发送群聊邀请\n[本消息是机器人自动回复]')
WXBotService.add_group_members_40_more(
robot_wxid=config.account_wxid,
from_wxid=config.chatroomId, #异度星球群
new_wxid=new_wxid)
# WXBotService.add_group_members_40_more(
# robot_wxid=config.account_wxid,
# from_wxid=config.chatroomId, #异度星球群
# new_wxid=new_wxid)
print('new_wxid--', new_wxid)
# global globalMsgData
# # 发送小程序
# print("globalMsgData",globalMsgData)
utils.sendminiProgram(data=None,account_wxid= config.account_wxid, wxid=new_wxid,msgXml= planetXml)
utils.sendminiProgram(data=None,
account_wxid=config.account_wxid,
wxid=new_wxid,
msgXml=planetXml)
WXBotService.send_text_message(
robot_wxid=config.account_wxid,
from_wxid=new_wxid,
msg=f'已收到-[在看]支持,谢谢,请从小程序,点击"立即加入"\n[本消息是机器人自动回复]')
return {"code": 200, "success": "ok", "message": "群聊邀请已发送"}
else:
return {"code": 200, "success": "ok", "message": "查不到nickname"}