From a337ee3c7bc256df94db054657a2f260b7b9d3cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wuyi=E6=97=A0=E7=96=91?= <444835641@qq.com> Date: Sun, 12 Mar 2023 20:22:14 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=96=B0=E5=A2=9E`=E5=BC=80?= =?UTF-8?q?=E9=BB=91=E5=95=A6`=E3=80=81`Telegram`=E9=80=82=E9=85=8D?= =?UTF-8?q?=E5=99=A8=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GenshinUID/__init__.py | 35 ++++++++++++++++-- GenshinUID/client.py | 84 ++++++++++++++++++++++++++++++++++++++++++ poetry.lock | 6 +-- 3 files changed, 119 insertions(+), 6 deletions(-) diff --git a/GenshinUID/__init__.py b/GenshinUID/__init__.py index 61a1e409..0625cb41 100644 --- a/GenshinUID/__init__.py +++ b/GenshinUID/__init__.py @@ -33,6 +33,7 @@ async def send_char_adv(bot: Bot, ev: Event): group_id = sessions[-2] if len(sessions) >= 2 else None message: List[Message] = [] msg_id = '' + sp_bot_id: Optional[str] = None sp_user_type: Optional[ Literal['group', 'direct', 'channel', 'sub_channel'] ] = None @@ -50,6 +51,30 @@ async def send_char_adv(bot: Bot, ev: Event): else: group_id = str(raw_data['channel_id']) msg_id = raw_data['id'] + # telegram + elif 'telegram_model' in raw_data: + messages = raw_data['message'] + # message.append(Message(type='text', data=text)) + if raw_data['chat'].type == 'group': + sp_user_type = 'group' + group_id = str(raw_data['chat'].id) + + else: + sp_user_type = 'direct' + group_id = None + user_id = str(raw_data['from_'].id) + # kaiheila + elif 'channel_type' in raw_data: + sp_bot_id = 'kaiheila' + messages = raw_data['event'].content + if raw_data['channel_type'] == 'GROUP': + sp_user_type = 'group' + group_id = raw_data['target_id'] + else: + sp_user_type = 'direct' + group_id = None + user_id = raw_data['author_id'] + msg_id = raw_data['message_id'] # ntchat elif not messages and 'message' in raw_data: messages = raw_data['message'] @@ -65,7 +90,11 @@ async def send_char_adv(bot: Bot, ev: Event): _at_list = raw_data['data']['at_user_list'] at_list = [Message('at', i) for i in _at_list] message.extend(at_list) - bot_id = messages.__class__.__module__.split('.')[2] + + if sp_bot_id: + bot_id = sp_bot_id + else: + bot_id = messages.__class__.__module__.split('.')[2] # 处理消息 for _msg in messages: @@ -73,9 +102,9 @@ async def send_char_adv(bot: Bot, ev: Event): message.append( Message( 'text', - _msg.data['text'] + _msg.data['text'].replace('/', '') if 'text' in _msg.data - else _msg.data['content'], + else _msg.data['content'].replace('/', ''), ) ) elif _msg.type == 'image': diff --git a/GenshinUID/client.py b/GenshinUID/client.py index 20b9cf59..f24d6d84 100644 --- a/GenshinUID/client.py +++ b/GenshinUID/client.py @@ -45,6 +45,7 @@ class GsClient: async def recv_msg(self): try: global bots + await asyncio.sleep(5) _bots = get_bots() for bot_real_id in _bots: bot = _bots[bot_real_id] @@ -106,6 +107,23 @@ class GsClient: msg.target_type, msg.msg_id, ) + elif msg.bot_id == 'telegram': + await telegram_send( + bot, + content, + image, + node, + msg.target_id, + ) + elif msg.bot_id == 'kaiheila': + await kaiheila_send( + bot, + content, + image, + node, + msg.target_id, + msg.target_type, + ) except Exception as e: logger.error(e) except ConnectionClosedError: @@ -266,3 +284,69 @@ async def ntchat_send( await _send(_msg['data'], None) else: await _send(content, image) + + +async def kaiheila_send( + bot: Bot, + content: Optional[str], + image: Optional[str], + node: Optional[List[Dict]], + target_id: Optional[str], + target_type: Optional[str], +): + async def _send(content: Optional[str], image: Optional[str]): + result = {} + result['type'] = 1 + if image: + img_bytes = base64.b64decode(image.replace('base64://', '')) + url = await bot.upload_file(img_bytes, 'GSUID-TEMP') # type:ignore + result['type'] = 2 + result['content'] = url + else: + result['content'] = content + + if target_type == 'group': + api = 'message/create' + result['channel_id'] = target_id + else: + api = 'direct-message/create' + result['target_id'] = target_id + await bot.call_api(api, **result) + + 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) + + +async def telegram_send( + bot: Bot, + content: Optional[str], + image: Optional[str], + node: Optional[List[Dict]], + target_id: Optional[str], +): + async def _send(content: Optional[str], image: Optional[str]): + result = {} + if image: + img_bytes = base64.b64decode(image.replace('base64://', '')) + result['photo'] = img_bytes + if content: + result['text'] = content + if content: + await bot.call_api('send_message', chat_id=target_id, **result) + if image: + await bot.call_api('send_photo', chat_id=target_id, **result) + + 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 8fc4ce2b..70327d20 100644 --- a/poetry.lock +++ b/poetry.lock @@ -152,14 +152,14 @@ gitdb = ">=4.0.1,<5" [[package]] name = "identify" -version = "2.5.19" +version = "2.5.20" description = "File identification library for Python" category = "dev" optional = false python-versions = ">=3.7" files = [ - {file = "identify-2.5.19-py2.py3-none-any.whl", hash = "sha256:3ee3533e7f6f5023157fbebbd5687bb4b698ce6f305259e0d24b2d7d9efb72bc"}, - {file = "identify-2.5.19.tar.gz", hash = "sha256:4102ecd051f6884449e7359e55b38ba6cd7aafb6ef27b8e2b38495a5723ea106"}, + {file = "identify-2.5.20-py2.py3-none-any.whl", hash = "sha256:5dfef8a745ca4f2c95f27e9db74cb4c8b6d9916383988e8791f3595868f78a33"}, + {file = "identify-2.5.20.tar.gz", hash = "sha256:c8b288552bc5f05a08aff09af2f58e6976bf8ac87beb38498a0e3d98ba64eb18"}, ] [package.extras]