diff --git a/GenshinUID/__init__.py b/GenshinUID/__init__.py index 90a10a93..bf6b2147 100644 --- a/GenshinUID/__init__.py +++ b/GenshinUID/__init__.py @@ -41,9 +41,6 @@ async def send_char_adv(bot: Bot, ev: Event): ] = None pm = 3 - if await SUPERUSER(bot, ev): - pm = 1 - # qqguild if '_message' in raw_data: messages = raw_data['_message'] @@ -53,8 +50,13 @@ async def send_char_adv(bot: Bot, ev: Event): else: group_id = str(raw_data['channel_id']) msg_id = raw_data['id'] + if 4 in raw_data['roles'] or 2 in raw_data['roles']: + pm = 2 # telegram elif 'telegram_model' in raw_data: + # 如果发送者是个Bot,不响应 + if raw_data['from_'].is_bot: + return messages = raw_data['message'] # message.append(Message(type='text', data=text)) if raw_data['chat'].type == 'group': @@ -67,6 +69,9 @@ async def send_char_adv(bot: Bot, ev: Event): user_id = str(raw_data['from_'].id) # kaiheila elif 'channel_type' in raw_data: + # 如果发送者是个Bot,不响应 + if raw_data['event'].author.bot: + return sp_bot_id = 'kaiheila' messages = raw_data['event'].content if raw_data['channel_type'] == 'GROUP': @@ -80,6 +85,27 @@ async def send_char_adv(bot: Bot, ev: Event): # ntchat elif not messages and 'message' in raw_data: messages = raw_data['message'] + # onebot + elif 'sender' in raw_data: + if ( + raw_data['sender'].role == 'owner' + or raw_data['sender'].role == 'admin' + ): + pm = 2 + msg_id = raw_data['message_id'] + # feishu + elif 'schema_' in raw_data: + messages = raw_data['event'].message.content + for feishu_msg in messages: + if 'image_key' in feishu_msg.data: + feishu_msg.data['url'] = feishu_msg.data['image_key'] + if raw_data['event'].message.chat_type == 'group': + sp_user_type = 'group' + group_id = raw_data['event'].message.chat_id + else: + sp_user_type = 'direct' + group_id = None + user_id = raw_data['event'].sender.sender_id.union_id # ntchat if 'data' in raw_data: if 'chatroom' in raw_data['data']['to_wxid']: @@ -100,6 +126,9 @@ async def send_char_adv(bot: Bot, ev: Event): else: bot_id = messages.__class__.__module__.split('.')[2] + if await SUPERUSER(bot, ev): + pm = 1 + # 处理消息 for _msg in messages: if _msg.type == 'text': @@ -115,6 +144,8 @@ async def send_char_adv(bot: Bot, ev: Event): message.append(Message('image', _msg.data['url'])) elif _msg.type == 'at': message.append(Message('at', _msg.data['qq'])) + elif _msg.type == 'reply': + message.append(Message('reply', _msg.data['id'])) if not message: return diff --git a/GenshinUID/client.py b/GenshinUID/client.py index ef3f3fbc..0f4bbfc2 100644 --- a/GenshinUID/client.py +++ b/GenshinUID/client.py @@ -1,4 +1,5 @@ import os +import json import base64 import asyncio from pathlib import Path @@ -142,10 +143,20 @@ class GsClient: msg.target_id, msg.target_type, ) + elif msg.bot_id == 'feishu': + await feishu_send( + bot, + content, + image, + file, + node, + msg.target_id, + msg.target_type, + ) except Exception as e: logger.error(e) - except RuntimeError: - pass + except RuntimeError as e: + logger.error(e) except ConnectionClosedError: logger.warning(f'与[gsuid-core]断开连接! Bot_ID: {BOT_ID}') self.is_alive = False @@ -435,3 +446,68 @@ async def telegram_send( else: await _send(content, image) await _send(content, image) + + +async def feishu_send( + bot: Bot, + content: Optional[str], + image: Optional[str], + file: Optional[str], + node: Optional[List[Dict]], + target_id: Optional[str], + target_type: Optional[str], +): + async def _send(content: Optional[str], image: Optional[str]): + if file: + file_name, file_content = file.split('|') + path = Path(__file__).resolve().parent / file_name + store_file(path, file_content) + with open(path, 'rb') as f: + doc = f.read() + msg = await bot.call_api( + 'im/v1/files', + method='POST', + data={'file_type': 'stream', 'file_name': file_name}, + files={'file': doc}, + ) + del_file(path) + _type = 'file' + elif content: + msg = {'text': content} + _type = 'text' + elif image: + data = {"image_type": "message"} + files = {"image": base64.b64decode(image.replace('base64://', ''))} + params = { + "method": "POST", + "data": data, + "files": files, + } + msg = await bot.call_api('im/v1/images', **params) + _type = 'image' + else: + return + + params = { + "method": "POST", + "query": { + "receive_id_type": 'union_id' + if target_type == 'direct' + else 'chat_id' + }, + "body": { + "receive_id": target_id, + "content": json.dumps(msg), + "msg_type": _type, + }, + } + await bot.call_api('im/v1/messages', **params) + + if node: + for _msg in node: + if _msg['type'] == 'image': + await _send(None, _msg['data']) + else: + await _send(_msg['data'], None) + else: + await _send(content, image) diff --git a/poetry.lock b/poetry.lock index de89d844..bca877d7 100644 --- a/poetry.lock +++ b/poetry.lock @@ -89,14 +89,14 @@ files = [ [[package]] name = "filelock" -version = "3.10.6" +version = "3.10.7" description = "A platform independent file lock." category = "dev" optional = false python-versions = ">=3.7" files = [ - {file = "filelock-3.10.6-py3-none-any.whl", hash = "sha256:52f119747b2b9c4730dac715a7b1ab34b8ee70fd9259cba158ee53da566387ff"}, - {file = "filelock-3.10.6.tar.gz", hash = "sha256:409105becd604d6b176a483f855e7e8903c5cb2873e47f2c64f66a370c046aaf"}, + {file = "filelock-3.10.7-py3-none-any.whl", hash = "sha256:bde48477b15fde2c7e5a0713cbe72721cb5a5ad32ee0b8f419907960b9d75536"}, + {file = "filelock-3.10.7.tar.gz", hash = "sha256:892be14aa8efc01673b5ed6589dbccb95f9a8596f0507e232626155495c18105"}, ] [package.extras] @@ -773,14 +773,14 @@ files = [ [[package]] name = "setuptools" -version = "67.6.0" +version = "67.6.1" description = "Easily download, build, install, upgrade, and uninstall Python packages" category = "dev" optional = false python-versions = ">=3.7" files = [ - {file = "setuptools-67.6.0-py3-none-any.whl", hash = "sha256:b78aaa36f6b90a074c1fa651168723acbf45d14cb1196b6f02c0fd07f17623b2"}, - {file = "setuptools-67.6.0.tar.gz", hash = "sha256:2ee892cd5f29f3373097f5a814697e397cf3ce313616df0af11231e2ad118077"}, + {file = "setuptools-67.6.1-py3-none-any.whl", hash = "sha256:e728ca814a823bf7bf60162daf9db95b93d532948c4c0bea762ce62f60189078"}, + {file = "setuptools-67.6.1.tar.gz", hash = "sha256:257de92a9d50a60b8e22abfcbb771571fde0dbf3ec234463212027a4eeecbe9a"}, ] [package.extras] @@ -814,14 +814,14 @@ files = [ [[package]] name = "tomlkit" -version = "0.11.6" +version = "0.11.7" description = "Style preserving TOML library" category = "main" optional = false -python-versions = ">=3.6" +python-versions = ">=3.7" files = [ - {file = "tomlkit-0.11.6-py3-none-any.whl", hash = "sha256:07de26b0d8cfc18f871aec595fda24d95b08fef89d147caa861939f37230bf4b"}, - {file = "tomlkit-0.11.6.tar.gz", hash = "sha256:71b952e5721688937fb02cf9d354dbcf0785066149d2855e44531ebdd2b65d73"}, + {file = "tomlkit-0.11.7-py3-none-any.whl", hash = "sha256:5325463a7da2ef0c6bbfefb62a3dc883aebe679984709aee32a317907d0a8d3c"}, + {file = "tomlkit-0.11.7.tar.gz", hash = "sha256:f392ef70ad87a672f02519f99967d28a4d3047133e2d1df936511465fbb3791d"}, ] [[package]] diff --git a/requirements.txt b/requirements.txt index 8a21f81f..ef7b2f95 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,7 +11,7 @@ pydantic[dotenv]==1.10.7 ; python_full_version >= "3.8.1" and python_version < " pygtrie==2.5.0 ; python_full_version >= "3.8.1" and python_version < "4.0" python-dotenv==1.0.0 ; python_full_version >= "3.8.1" and python_version < "4.0" smmap==5.0.0 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0" -tomlkit==0.11.6 ; python_full_version >= "3.8.1" and python_version < "4.0" +tomlkit==0.11.7 ; python_full_version >= "3.8.1" and python_version < "4.0" typing-extensions==4.5.0 ; python_full_version >= "3.8.1" and python_version < "4.0" win32-setctime==1.1.0 ; python_full_version >= "3.8.1" and python_version < "4.0" and sys_platform == "win32" yarl==1.8.2 ; python_full_version >= "3.8.1" and python_version < "4.0"