新增开黑啦Telegram适配器的支持

This commit is contained in:
Wuyi无疑 2023-03-12 20:22:14 +08:00
parent c3e9ae8f61
commit a337ee3c7b
3 changed files with 119 additions and 6 deletions

View File

@ -33,6 +33,7 @@ async def send_char_adv(bot: Bot, ev: Event):
group_id = sessions[-2] if len(sessions) >= 2 else None group_id = sessions[-2] if len(sessions) >= 2 else None
message: List[Message] = [] message: List[Message] = []
msg_id = '' msg_id = ''
sp_bot_id: Optional[str] = None
sp_user_type: Optional[ sp_user_type: Optional[
Literal['group', 'direct', 'channel', 'sub_channel'] Literal['group', 'direct', 'channel', 'sub_channel']
] = None ] = None
@ -50,6 +51,30 @@ async def send_char_adv(bot: Bot, ev: Event):
else: else:
group_id = str(raw_data['channel_id']) group_id = str(raw_data['channel_id'])
msg_id = raw_data['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 # ntchat
elif not messages and 'message' in raw_data: elif not messages and 'message' in raw_data:
messages = raw_data['message'] messages = raw_data['message']
@ -65,6 +90,10 @@ async def send_char_adv(bot: Bot, ev: Event):
_at_list = raw_data['data']['at_user_list'] _at_list = raw_data['data']['at_user_list']
at_list = [Message('at', i) for i in _at_list] at_list = [Message('at', i) for i in _at_list]
message.extend(at_list) message.extend(at_list)
if sp_bot_id:
bot_id = sp_bot_id
else:
bot_id = messages.__class__.__module__.split('.')[2] bot_id = messages.__class__.__module__.split('.')[2]
# 处理消息 # 处理消息
@ -73,9 +102,9 @@ async def send_char_adv(bot: Bot, ev: Event):
message.append( message.append(
Message( Message(
'text', 'text',
_msg.data['text'] _msg.data['text'].replace('/', '')
if 'text' in _msg.data if 'text' in _msg.data
else _msg.data['content'], else _msg.data['content'].replace('/', ''),
) )
) )
elif _msg.type == 'image': elif _msg.type == 'image':

View File

@ -45,6 +45,7 @@ class GsClient:
async def recv_msg(self): async def recv_msg(self):
try: try:
global bots global bots
await asyncio.sleep(5)
_bots = get_bots() _bots = get_bots()
for bot_real_id in _bots: for bot_real_id in _bots:
bot = _bots[bot_real_id] bot = _bots[bot_real_id]
@ -106,6 +107,23 @@ class GsClient:
msg.target_type, msg.target_type,
msg.msg_id, 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: except Exception as e:
logger.error(e) logger.error(e)
except ConnectionClosedError: except ConnectionClosedError:
@ -266,3 +284,69 @@ async def ntchat_send(
await _send(_msg['data'], None) await _send(_msg['data'], None)
else: else:
await _send(content, image) 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)

6
poetry.lock generated
View File

@ -152,14 +152,14 @@ gitdb = ">=4.0.1,<5"
[[package]] [[package]]
name = "identify" name = "identify"
version = "2.5.19" version = "2.5.20"
description = "File identification library for Python" description = "File identification library for Python"
category = "dev" category = "dev"
optional = false optional = false
python-versions = ">=3.7" python-versions = ">=3.7"
files = [ files = [
{file = "identify-2.5.19-py2.py3-none-any.whl", hash = "sha256:3ee3533e7f6f5023157fbebbd5687bb4b698ce6f305259e0d24b2d7d9efb72bc"}, {file = "identify-2.5.20-py2.py3-none-any.whl", hash = "sha256:5dfef8a745ca4f2c95f27e9db74cb4c8b6d9916383988e8791f3595868f78a33"},
{file = "identify-2.5.19.tar.gz", hash = "sha256:4102ecd051f6884449e7359e55b38ba6cd7aafb6ef27b8e2b38495a5723ea106"}, {file = "identify-2.5.20.tar.gz", hash = "sha256:c8b288552bc5f05a08aff09af2f58e6976bf8ac87beb38498a0e3d98ba64eb18"},
] ]
[package.extras] [package.extras]