2024-02-07 20:03:46 +08:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
import typing
|
|
|
|
|
|
import asyncio
|
|
|
|
|
|
import traceback
|
|
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
|
|
|
|
import aiocqhttp
|
2025-06-16 13:18:59 +08:00
|
|
|
|
import pydantic
|
2024-02-07 20:03:46 +08:00
|
|
|
|
|
2025-06-16 13:18:59 +08:00
|
|
|
|
import langbot_plugin.api.definition.abstract.platform.adapter as abstract_platform_adapter
|
|
|
|
|
|
import langbot_plugin.api.entities.builtin.platform.message as platform_message
|
|
|
|
|
|
import langbot_plugin.api.entities.builtin.platform.events as platform_events
|
|
|
|
|
|
import langbot_plugin.api.entities.builtin.platform.entities as platform_entities
|
2024-12-24 10:57:17 +08:00
|
|
|
|
from ...utils import image
|
2025-06-16 13:18:59 +08:00
|
|
|
|
import langbot_plugin.api.definition.abstract.platform.event_logger as abstract_platform_logger
|
2024-02-07 20:03:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-06-16 13:18:59 +08:00
|
|
|
|
class AiocqhttpMessageConverter(abstract_platform_adapter.AbstractMessageConverter):
|
2024-02-07 20:03:46 +08:00
|
|
|
|
@staticmethod
|
2025-04-29 17:24:07 +08:00
|
|
|
|
async def yiri2target(
|
|
|
|
|
|
message_chain: platform_message.MessageChain,
|
|
|
|
|
|
) -> typing.Tuple[list, int, datetime.datetime]:
|
2024-02-07 20:03:46 +08:00
|
|
|
|
msg_list = aiocqhttp.Message()
|
|
|
|
|
|
|
|
|
|
|
|
msg_id = 0
|
|
|
|
|
|
msg_time = None
|
|
|
|
|
|
|
|
|
|
|
|
for msg in message_chain:
|
2024-09-26 00:23:03 +08:00
|
|
|
|
if type(msg) is platform_message.Plain:
|
2024-02-07 20:03:46 +08:00
|
|
|
|
msg_list.append(aiocqhttp.MessageSegment.text(msg.text))
|
2024-09-26 00:23:03 +08:00
|
|
|
|
elif type(msg) is platform_message.Source:
|
2024-02-07 20:03:46 +08:00
|
|
|
|
msg_id = msg.id
|
|
|
|
|
|
msg_time = msg.time
|
2024-09-26 00:23:03 +08:00
|
|
|
|
elif type(msg) is platform_message.Image:
|
2024-04-11 03:26:12 +00:00
|
|
|
|
arg = ''
|
2024-05-17 17:05:20 +08:00
|
|
|
|
if msg.base64:
|
2024-05-17 08:20:06 +00:00
|
|
|
|
arg = msg.base64
|
2025-04-29 17:24:07 +08:00
|
|
|
|
msg_list.append(aiocqhttp.MessageSegment.image(f'base64://{arg}'))
|
2024-05-17 08:20:06 +00:00
|
|
|
|
elif msg.url:
|
2024-04-11 03:26:12 +00:00
|
|
|
|
arg = msg.url
|
2024-05-17 08:20:06 +00:00
|
|
|
|
msg_list.append(aiocqhttp.MessageSegment.image(arg))
|
2024-04-11 03:26:12 +00:00
|
|
|
|
elif msg.path:
|
|
|
|
|
|
arg = msg.path
|
2024-05-17 08:20:06 +00:00
|
|
|
|
msg_list.append(aiocqhttp.MessageSegment.image(arg))
|
2024-09-26 00:23:03 +08:00
|
|
|
|
elif type(msg) is platform_message.At:
|
2024-02-07 20:03:46 +08:00
|
|
|
|
msg_list.append(aiocqhttp.MessageSegment.at(msg.target))
|
2024-09-26 00:23:03 +08:00
|
|
|
|
elif type(msg) is platform_message.AtAll:
|
2025-04-29 17:24:07 +08:00
|
|
|
|
msg_list.append(aiocqhttp.MessageSegment.at('all'))
|
2024-09-26 00:23:03 +08:00
|
|
|
|
elif type(msg) is platform_message.Voice:
|
2024-07-25 16:14:24 +08:00
|
|
|
|
arg = ''
|
|
|
|
|
|
if msg.base64:
|
|
|
|
|
|
arg = msg.base64
|
2025-04-29 17:24:07 +08:00
|
|
|
|
msg_list.append(aiocqhttp.MessageSegment.record(f'base64://{arg}'))
|
2024-07-25 16:14:24 +08:00
|
|
|
|
elif msg.url:
|
|
|
|
|
|
arg = msg.url
|
|
|
|
|
|
msg_list.append(aiocqhttp.MessageSegment.record(arg))
|
|
|
|
|
|
elif msg.path:
|
|
|
|
|
|
arg = msg.path
|
|
|
|
|
|
msg_list.append(aiocqhttp.MessageSegment.record(msg.path))
|
2025-03-24 14:03:44 +08:00
|
|
|
|
elif type(msg) is platform_message.Forward:
|
2024-02-07 20:03:46 +08:00
|
|
|
|
for node in msg.node_list:
|
2025-05-10 18:04:58 +08:00
|
|
|
|
msg_list.extend((await AiocqhttpMessageConverter.yiri2target(node.message_chain))[0])
|
2025-06-15 17:22:35 +08:00
|
|
|
|
elif isinstance(msg, platform_message.File):
|
2025-07-16 11:36:01 +08:00
|
|
|
|
msg_list.append({'type': 'file', 'data': {'file': msg.url, 'name': msg.name}})
|
2025-06-19 10:38:02 +08:00
|
|
|
|
elif isinstance(msg, platform_message.Face):
|
2025-07-16 11:36:01 +08:00
|
|
|
|
if msg.face_type == 'face':
|
2025-06-19 10:38:02 +08:00
|
|
|
|
msg_list.append(aiocqhttp.MessageSegment.face(msg.face_id))
|
2025-07-16 11:36:01 +08:00
|
|
|
|
elif msg.face_type == 'rps':
|
2025-06-19 10:38:02 +08:00
|
|
|
|
msg_list.append(aiocqhttp.MessageSegment.rps())
|
2025-07-16 11:36:01 +08:00
|
|
|
|
elif msg.face_type == 'dice':
|
2025-06-19 10:38:02 +08:00
|
|
|
|
msg_list.append(aiocqhttp.MessageSegment.dice())
|
2025-06-15 17:22:35 +08:00
|
|
|
|
|
2024-02-07 20:03:46 +08:00
|
|
|
|
else:
|
|
|
|
|
|
msg_list.append(aiocqhttp.MessageSegment.text(str(msg)))
|
|
|
|
|
|
|
|
|
|
|
|
return msg_list, msg_id, msg_time
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2025-07-16 11:36:01 +08:00
|
|
|
|
async def target2yiri(message: str, message_id: int = -1, bot: aiocqhttp.CQHttp = None):
|
2024-02-07 20:03:46 +08:00
|
|
|
|
message = aiocqhttp.Message(message)
|
|
|
|
|
|
|
2025-06-19 10:38:02 +08:00
|
|
|
|
def get_face_name(face_id):
|
|
|
|
|
|
face_code_dict = {
|
2025-07-16 11:36:01 +08:00
|
|
|
|
'2': '好色',
|
|
|
|
|
|
'4': '得意',
|
|
|
|
|
|
'5': '流泪',
|
|
|
|
|
|
'8': '睡',
|
|
|
|
|
|
'9': '大哭',
|
|
|
|
|
|
'10': '尴尬',
|
|
|
|
|
|
'12': '调皮',
|
|
|
|
|
|
'14': '微笑',
|
|
|
|
|
|
'16': '酷',
|
|
|
|
|
|
'21': '可爱',
|
|
|
|
|
|
'23': '傲慢',
|
|
|
|
|
|
'24': '饥饿',
|
|
|
|
|
|
'25': '困',
|
|
|
|
|
|
'26': '惊恐',
|
|
|
|
|
|
'27': '流汗',
|
|
|
|
|
|
'28': '憨笑',
|
|
|
|
|
|
'29': '悠闲',
|
|
|
|
|
|
'30': '奋斗',
|
|
|
|
|
|
'32': '疑问',
|
|
|
|
|
|
'33': '嘘',
|
|
|
|
|
|
'34': '晕',
|
|
|
|
|
|
'38': '敲打',
|
|
|
|
|
|
'39': '再见',
|
|
|
|
|
|
'41': '发抖',
|
|
|
|
|
|
'42': '爱情',
|
|
|
|
|
|
'43': '跳跳',
|
|
|
|
|
|
'49': '拥抱',
|
|
|
|
|
|
'53': '蛋糕',
|
|
|
|
|
|
'60': '咖啡',
|
|
|
|
|
|
'63': '玫瑰',
|
|
|
|
|
|
'66': '爱心',
|
|
|
|
|
|
'74': '太阳',
|
|
|
|
|
|
'75': '月亮',
|
|
|
|
|
|
'76': '赞',
|
|
|
|
|
|
'78': '握手',
|
|
|
|
|
|
'79': '胜利',
|
|
|
|
|
|
'85': '飞吻',
|
|
|
|
|
|
'89': '西瓜',
|
|
|
|
|
|
'96': '冷汗',
|
|
|
|
|
|
'97': '擦汗',
|
|
|
|
|
|
'98': '抠鼻',
|
|
|
|
|
|
'99': '鼓掌',
|
|
|
|
|
|
'100': '糗大了',
|
|
|
|
|
|
'101': '坏笑',
|
|
|
|
|
|
'102': '左哼哼',
|
|
|
|
|
|
'103': '右哼哼',
|
|
|
|
|
|
'104': '哈欠',
|
|
|
|
|
|
'106': '委屈',
|
|
|
|
|
|
'109': '左亲亲',
|
|
|
|
|
|
'111': '可怜',
|
|
|
|
|
|
'116': '示爱',
|
|
|
|
|
|
'118': '抱拳',
|
|
|
|
|
|
'120': '拳头',
|
|
|
|
|
|
'122': '爱你',
|
|
|
|
|
|
'123': 'NO',
|
|
|
|
|
|
'124': 'OK',
|
|
|
|
|
|
'125': '转圈',
|
|
|
|
|
|
'129': '挥手',
|
|
|
|
|
|
'144': '喝彩',
|
|
|
|
|
|
'147': '棒棒糖',
|
|
|
|
|
|
'171': '茶',
|
|
|
|
|
|
'173': '泪奔',
|
|
|
|
|
|
'174': '无奈',
|
|
|
|
|
|
'175': '卖萌',
|
|
|
|
|
|
'176': '小纠结',
|
|
|
|
|
|
'179': 'doge',
|
|
|
|
|
|
'180': '惊喜',
|
|
|
|
|
|
'181': '骚扰',
|
|
|
|
|
|
'182': '笑哭',
|
|
|
|
|
|
'183': '我最美',
|
|
|
|
|
|
'201': '点赞',
|
|
|
|
|
|
'203': '托脸',
|
|
|
|
|
|
'212': '托腮',
|
|
|
|
|
|
'214': '啵啵',
|
|
|
|
|
|
'219': '蹭一蹭',
|
|
|
|
|
|
'222': '抱抱',
|
|
|
|
|
|
'227': '拍手',
|
|
|
|
|
|
'232': '佛系',
|
|
|
|
|
|
'240': '喷脸',
|
|
|
|
|
|
'243': '甩头',
|
|
|
|
|
|
'246': '加油抱抱',
|
|
|
|
|
|
'262': '脑阔疼',
|
|
|
|
|
|
'264': '捂脸',
|
|
|
|
|
|
'265': '辣眼睛',
|
|
|
|
|
|
'266': '哦哟',
|
|
|
|
|
|
'267': '头秃',
|
|
|
|
|
|
'268': '问号脸',
|
|
|
|
|
|
'269': '暗中观察',
|
|
|
|
|
|
'270': 'emm',
|
|
|
|
|
|
'271': '吃瓜',
|
|
|
|
|
|
'272': '呵呵哒',
|
|
|
|
|
|
'273': '我酸了',
|
|
|
|
|
|
'277': '汪汪',
|
|
|
|
|
|
'278': '汗',
|
|
|
|
|
|
'281': '无眼笑',
|
|
|
|
|
|
'282': '敬礼',
|
|
|
|
|
|
'284': '面无表情',
|
|
|
|
|
|
'285': '摸鱼',
|
|
|
|
|
|
'287': '哦',
|
|
|
|
|
|
'289': '睁眼',
|
|
|
|
|
|
'290': '敲开心',
|
|
|
|
|
|
'293': '摸锦鲤',
|
|
|
|
|
|
'294': '期待',
|
|
|
|
|
|
'297': '拜谢',
|
|
|
|
|
|
'298': '元宝',
|
|
|
|
|
|
'299': '牛啊',
|
|
|
|
|
|
'305': '右亲亲',
|
|
|
|
|
|
'306': '牛气冲天',
|
|
|
|
|
|
'307': '喵喵',
|
|
|
|
|
|
'314': '仔细分析',
|
|
|
|
|
|
'315': '加油',
|
|
|
|
|
|
'318': '崇拜',
|
|
|
|
|
|
'319': '比心',
|
|
|
|
|
|
'320': '庆祝',
|
|
|
|
|
|
'322': '拒绝',
|
|
|
|
|
|
'324': '吃糖',
|
|
|
|
|
|
'326': '生气',
|
2025-06-19 10:38:02 +08:00
|
|
|
|
}
|
2025-07-16 11:36:01 +08:00
|
|
|
|
return face_code_dict.get(face_id, '')
|
2025-06-19 10:38:02 +08:00
|
|
|
|
|
2025-05-19 22:24:18 +08:00
|
|
|
|
async def process_message_data(msg_data, reply_list):
|
2025-06-15 12:51:51 +08:00
|
|
|
|
if msg_data['type'] == 'image':
|
|
|
|
|
|
image_base64, image_format = await image.qq_image_url_to_base64(msg_data['data']['url'])
|
|
|
|
|
|
reply_list.append(platform_message.Image(base64=f'data:image/{image_format};base64,{image_base64}'))
|
2025-05-19 22:24:18 +08:00
|
|
|
|
|
2025-06-15 12:51:51 +08:00
|
|
|
|
elif msg_data['type'] == 'text':
|
|
|
|
|
|
reply_list.append(platform_message.Plain(text=msg_data['data']['text']))
|
2025-05-19 22:24:18 +08:00
|
|
|
|
|
2025-06-15 12:51:51 +08:00
|
|
|
|
elif msg_data['type'] == 'forward': # 这里来应该传入转发消息组,暂时传入qoute
|
|
|
|
|
|
for forward_msg_datas in msg_data['data']['content']:
|
|
|
|
|
|
for forward_msg_data in forward_msg_datas['message']:
|
2025-05-19 22:24:18 +08:00
|
|
|
|
await process_message_data(forward_msg_data, reply_list)
|
|
|
|
|
|
|
2025-06-15 12:51:51 +08:00
|
|
|
|
elif msg_data['type'] == 'at':
|
|
|
|
|
|
if msg_data['data']['qq'] == 'all':
|
2025-05-19 22:24:18 +08:00
|
|
|
|
reply_list.append(platform_message.AtAll())
|
|
|
|
|
|
else:
|
|
|
|
|
|
reply_list.append(
|
|
|
|
|
|
platform_message.At(
|
2025-06-15 12:51:51 +08:00
|
|
|
|
target=msg_data['data']['qq'],
|
2025-05-19 22:24:18 +08:00
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2024-02-07 20:03:46 +08:00
|
|
|
|
yiri_msg_list = []
|
|
|
|
|
|
|
2025-05-10 18:04:58 +08:00
|
|
|
|
yiri_msg_list.append(platform_message.Source(id=message_id, time=datetime.datetime.now()))
|
2024-02-07 20:03:46 +08:00
|
|
|
|
|
|
|
|
|
|
for msg in message:
|
2025-05-19 22:24:18 +08:00
|
|
|
|
reply_list = []
|
2025-04-29 17:24:07 +08:00
|
|
|
|
if msg.type == 'at':
|
|
|
|
|
|
if msg.data['qq'] == 'all':
|
2024-09-26 00:23:03 +08:00
|
|
|
|
yiri_msg_list.append(platform_message.AtAll())
|
2024-02-07 20:03:46 +08:00
|
|
|
|
else:
|
|
|
|
|
|
yiri_msg_list.append(
|
2024-09-26 00:23:03 +08:00
|
|
|
|
platform_message.At(
|
2025-04-29 17:24:07 +08:00
|
|
|
|
target=msg.data['qq'],
|
2024-02-07 20:03:46 +08:00
|
|
|
|
)
|
|
|
|
|
|
)
|
2025-04-29 17:24:07 +08:00
|
|
|
|
elif msg.type == 'text':
|
|
|
|
|
|
yiri_msg_list.append(platform_message.Plain(text=msg.data['text']))
|
|
|
|
|
|
elif msg.type == 'image':
|
2025-07-16 11:36:01 +08:00
|
|
|
|
emoji_id = msg.data.get('emoji_package_id', None)
|
2025-06-19 10:38:02 +08:00
|
|
|
|
if emoji_id:
|
|
|
|
|
|
face_id = emoji_id
|
2025-07-16 11:36:01 +08:00
|
|
|
|
face_name = msg.data.get('summary', '')
|
2025-06-19 10:38:02 +08:00
|
|
|
|
image_msg = platform_message.Face(face_id=face_id, face_name=face_name)
|
|
|
|
|
|
else:
|
|
|
|
|
|
image_base64, image_format = await image.qq_image_url_to_base64(msg.data['url'])
|
|
|
|
|
|
image_msg = platform_message.Image(base64=f'data:image/{image_format};base64,{image_base64}')
|
|
|
|
|
|
yiri_msg_list.append(image_msg)
|
2025-05-19 22:24:18 +08:00
|
|
|
|
elif msg.type == 'forward':
|
|
|
|
|
|
# 暂时不太合理
|
|
|
|
|
|
# msg_datas = await bot.get_msg(message_id=message_id)
|
|
|
|
|
|
# print(msg_datas)
|
|
|
|
|
|
# for msg_data in msg_datas["message"]:
|
|
|
|
|
|
# await process_message_data(msg_data, yiri_msg_list)
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
elif msg.type == 'reply': # 此处处理引用消息传入Qoute
|
2025-06-15 12:51:51 +08:00
|
|
|
|
msg_datas = await bot.get_msg(message_id=msg.data['id'])
|
2025-05-19 22:24:18 +08:00
|
|
|
|
|
2025-06-15 12:51:51 +08:00
|
|
|
|
for msg_data in msg_datas['message']:
|
2025-05-19 22:24:18 +08:00
|
|
|
|
await process_message_data(msg_data, reply_list)
|
|
|
|
|
|
|
2025-06-15 12:51:51 +08:00
|
|
|
|
reply_msg = platform_message.Quote(
|
|
|
|
|
|
message_id=msg.data['id'], sender_id=msg_datas['user_id'], origin=reply_list
|
|
|
|
|
|
)
|
2025-05-19 22:24:18 +08:00
|
|
|
|
yiri_msg_list.append(reply_msg)
|
|
|
|
|
|
|
2025-06-15 17:22:35 +08:00
|
|
|
|
elif msg.type == 'file':
|
2025-08-24 21:40:02 +08:00
|
|
|
|
pass
|
2025-06-15 17:22:35 +08:00
|
|
|
|
# file_name = msg.data['file']
|
2025-08-24 21:40:02 +08:00
|
|
|
|
# file_id = msg.data['file_id']
|
|
|
|
|
|
# file_data = await bot.get_file(file_id=file_id)
|
|
|
|
|
|
# file_name = file_data.get('file_name')
|
|
|
|
|
|
# file_path = file_data.get('file')
|
|
|
|
|
|
# _ = file_path
|
|
|
|
|
|
# file_url = file_data.get('file_url')
|
|
|
|
|
|
# file_size = file_data.get('file_size')
|
|
|
|
|
|
# yiri_msg_list.append(platform_message.File(id=file_id, name=file_name,url=file_url,size=file_size))
|
2025-06-19 10:38:02 +08:00
|
|
|
|
elif msg.type == 'face':
|
|
|
|
|
|
face_id = msg.data['id']
|
|
|
|
|
|
face_name = msg.data['raw']['faceText']
|
|
|
|
|
|
if not face_name:
|
|
|
|
|
|
face_name = get_face_name(face_id)
|
2025-07-16 11:36:01 +08:00
|
|
|
|
yiri_msg_list.append(platform_message.Face(face_id=int(face_id), face_name=face_name.replace('/', '')))
|
2025-06-19 10:38:02 +08:00
|
|
|
|
elif msg.type == 'rps':
|
|
|
|
|
|
face_id = msg.data['result']
|
2025-07-16 11:36:01 +08:00
|
|
|
|
yiri_msg_list.append(platform_message.Face(face_type='rps', face_id=int(face_id), face_name='猜拳'))
|
2025-06-19 10:38:02 +08:00
|
|
|
|
elif msg.type == 'dice':
|
|
|
|
|
|
face_id = msg.data['result']
|
2025-07-16 11:36:01 +08:00
|
|
|
|
yiri_msg_list.append(platform_message.Face(face_type='dice', face_id=int(face_id), face_name='骰子'))
|
2025-06-15 17:22:35 +08:00
|
|
|
|
|
2024-09-26 00:23:03 +08:00
|
|
|
|
chain = platform_message.MessageChain(yiri_msg_list)
|
2024-02-07 20:03:46 +08:00
|
|
|
|
|
|
|
|
|
|
return chain
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-16 13:18:59 +08:00
|
|
|
|
class AiocqhttpEventConverter(abstract_platform_adapter.AbstractEventConverter):
|
2024-02-07 20:03:46 +08:00
|
|
|
|
@staticmethod
|
2025-03-24 14:03:44 +08:00
|
|
|
|
async def yiri2target(event: platform_events.MessageEvent, bot_account_id: int):
|
|
|
|
|
|
return event.source_platform_object
|
2024-02-07 20:03:46 +08:00
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2025-06-15 12:51:51 +08:00
|
|
|
|
async def target2yiri(event: aiocqhttp.Event, bot=None):
|
|
|
|
|
|
yiri_chain = await AiocqhttpMessageConverter.target2yiri(event.message, event.message_id, bot)
|
2024-02-07 20:03:46 +08:00
|
|
|
|
|
2025-04-29 17:24:07 +08:00
|
|
|
|
if event.message_type == 'group':
|
|
|
|
|
|
permission = 'MEMBER'
|
2024-02-07 20:03:46 +08:00
|
|
|
|
|
2025-04-29 17:24:07 +08:00
|
|
|
|
if 'role' in event.sender:
|
|
|
|
|
|
if event.sender['role'] == 'admin':
|
|
|
|
|
|
permission = 'ADMINISTRATOR'
|
|
|
|
|
|
elif event.sender['role'] == 'owner':
|
|
|
|
|
|
permission = 'OWNER'
|
2024-09-26 00:23:03 +08:00
|
|
|
|
converted_event = platform_events.GroupMessage(
|
|
|
|
|
|
sender=platform_entities.GroupMember(
|
2025-04-29 17:24:07 +08:00
|
|
|
|
id=event.sender['user_id'], # message_seq 放哪?
|
|
|
|
|
|
member_name=event.sender['nickname'],
|
2024-02-07 20:03:46 +08:00
|
|
|
|
permission=permission,
|
2024-09-26 00:23:03 +08:00
|
|
|
|
group=platform_entities.Group(
|
2024-02-07 20:03:46 +08:00
|
|
|
|
id=event.group_id,
|
2025-04-29 17:24:07 +08:00
|
|
|
|
name=event.sender['nickname'],
|
2024-09-26 00:23:03 +08:00
|
|
|
|
permission=platform_entities.Permission.Member,
|
2024-02-07 20:03:46 +08:00
|
|
|
|
),
|
2025-05-10 18:04:58 +08:00
|
|
|
|
special_title=event.sender['title'] if 'title' in event.sender else '',
|
2024-02-07 20:03:46 +08:00
|
|
|
|
join_timestamp=0,
|
|
|
|
|
|
last_speak_timestamp=0,
|
|
|
|
|
|
mute_time_remaining=0,
|
|
|
|
|
|
),
|
|
|
|
|
|
message_chain=yiri_chain,
|
|
|
|
|
|
time=event.time,
|
2025-04-29 17:24:07 +08:00
|
|
|
|
source_platform_object=event,
|
2024-02-07 20:03:46 +08:00
|
|
|
|
)
|
|
|
|
|
|
return converted_event
|
2025-04-29 17:24:07 +08:00
|
|
|
|
elif event.message_type == 'private':
|
2024-09-26 00:23:03 +08:00
|
|
|
|
return platform_events.FriendMessage(
|
|
|
|
|
|
sender=platform_entities.Friend(
|
2025-04-29 17:24:07 +08:00
|
|
|
|
id=event.sender['user_id'],
|
|
|
|
|
|
nickname=event.sender['nickname'],
|
|
|
|
|
|
remark='',
|
2024-02-07 20:03:46 +08:00
|
|
|
|
),
|
|
|
|
|
|
message_chain=yiri_chain,
|
|
|
|
|
|
time=event.time,
|
2025-04-29 17:24:07 +08:00
|
|
|
|
source_platform_object=event,
|
2024-02-07 20:03:46 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-16 13:18:59 +08:00
|
|
|
|
class AiocqhttpAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
|
|
|
|
|
bot: aiocqhttp.CQHttp = pydantic.Field(exclude=True, default_factory=aiocqhttp.CQHttp)
|
2024-02-07 20:03:46 +08:00
|
|
|
|
|
|
|
|
|
|
message_converter: AiocqhttpMessageConverter = AiocqhttpMessageConverter()
|
|
|
|
|
|
event_converter: AiocqhttpEventConverter = AiocqhttpEventConverter()
|
|
|
|
|
|
|
2025-05-27 22:36:50 +08:00
|
|
|
|
on_websocket_connection_event_cache: typing.List[typing.Callable[[aiocqhttp.Event], None]] = []
|
|
|
|
|
|
|
2025-06-16 13:18:59 +08:00
|
|
|
|
def __init__(self, config: dict, logger: abstract_platform_logger.AbstractEventLogger):
|
|
|
|
|
|
super().__init__(
|
|
|
|
|
|
config=config,
|
|
|
|
|
|
logger=logger,
|
|
|
|
|
|
)
|
2024-02-15 22:21:56 +08:00
|
|
|
|
|
|
|
|
|
|
async def shutdown_trigger_placeholder():
|
2024-02-16 13:08:26 +08:00
|
|
|
|
while True:
|
|
|
|
|
|
await asyncio.sleep(1)
|
2025-04-29 17:24:07 +08:00
|
|
|
|
|
2024-02-15 22:21:56 +08:00
|
|
|
|
self.config['shutdown_trigger'] = shutdown_trigger_placeholder
|
|
|
|
|
|
|
2025-05-27 22:36:50 +08:00
|
|
|
|
self.on_websocket_connection_event_cache = []
|
2024-02-07 20:03:46 +08:00
|
|
|
|
|
2025-04-29 17:24:07 +08:00
|
|
|
|
if 'access-token' in config:
|
|
|
|
|
|
self.bot = aiocqhttp.CQHttp(access_token=config['access-token'])
|
|
|
|
|
|
del self.config['access-token']
|
2024-03-13 16:49:11 +08:00
|
|
|
|
else:
|
|
|
|
|
|
self.bot = aiocqhttp.CQHttp()
|
2024-02-07 20:03:46 +08:00
|
|
|
|
|
2025-05-10 18:04:58 +08:00
|
|
|
|
async def send_message(self, target_type: str, target_id: str, message: platform_message.MessageChain):
|
2024-12-24 23:37:02 +08:00
|
|
|
|
aiocq_msg = (await AiocqhttpMessageConverter.yiri2target(message))[0]
|
2024-03-16 15:16:46 +08:00
|
|
|
|
|
2025-04-29 17:24:07 +08:00
|
|
|
|
if target_type == 'group':
|
2024-03-16 15:16:46 +08:00
|
|
|
|
await self.bot.send_group_msg(group_id=int(target_id), message=aiocq_msg)
|
2025-04-29 17:24:07 +08:00
|
|
|
|
elif target_type == 'person':
|
2024-03-16 15:16:46 +08:00
|
|
|
|
await self.bot.send_private_msg(user_id=int(target_id), message=aiocq_msg)
|
2024-02-07 20:03:46 +08:00
|
|
|
|
|
|
|
|
|
|
async def reply_message(
|
|
|
|
|
|
self,
|
2024-09-26 00:23:03 +08:00
|
|
|
|
message_source: platform_events.MessageEvent,
|
|
|
|
|
|
message: platform_message.MessageChain,
|
2024-02-07 20:03:46 +08:00
|
|
|
|
quote_origin: bool = False,
|
2025-04-29 17:24:07 +08:00
|
|
|
|
):
|
2025-05-10 18:04:58 +08:00
|
|
|
|
aiocq_event = await AiocqhttpEventConverter.yiri2target(message_source, self.bot_account_id)
|
2024-12-24 10:57:17 +08:00
|
|
|
|
aiocq_msg = (await AiocqhttpMessageConverter.yiri2target(message))[0]
|
2024-02-07 20:03:46 +08:00
|
|
|
|
if quote_origin:
|
2025-05-10 18:04:58 +08:00
|
|
|
|
aiocq_msg = aiocqhttp.MessageSegment.reply(aiocq_event.message_id) + aiocq_msg
|
2024-02-07 20:03:46 +08:00
|
|
|
|
|
2025-04-29 17:24:07 +08:00
|
|
|
|
return await self.bot.send(aiocq_event, aiocq_msg)
|
2024-02-07 20:03:46 +08:00
|
|
|
|
|
|
|
|
|
|
async def is_muted(self, group_id: int) -> bool:
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def register_listener(
|
|
|
|
|
|
self,
|
2024-09-26 00:23:03 +08:00
|
|
|
|
event_type: typing.Type[platform_events.Event],
|
2025-06-16 13:18:59 +08:00
|
|
|
|
callback: typing.Callable[
|
|
|
|
|
|
[platform_events.Event, abstract_platform_adapter.AbstractMessagePlatformAdapter], None
|
|
|
|
|
|
],
|
2024-02-07 20:03:46 +08:00
|
|
|
|
):
|
|
|
|
|
|
async def on_message(event: aiocqhttp.Event):
|
|
|
|
|
|
self.bot_account_id = event.self_id
|
|
|
|
|
|
try:
|
2025-06-15 12:51:51 +08:00
|
|
|
|
return await callback(await self.event_converter.target2yiri(event, self.bot), self)
|
2025-04-29 17:24:07 +08:00
|
|
|
|
except Exception:
|
2025-05-27 22:36:50 +08:00
|
|
|
|
await self.logger.error(f'Error in on_message: {traceback.format_exc()}')
|
2024-02-07 20:03:46 +08:00
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
2024-09-26 00:23:03 +08:00
|
|
|
|
if event_type == platform_events.GroupMessage:
|
2025-04-29 17:24:07 +08:00
|
|
|
|
self.bot.on_message('group')(on_message)
|
2025-06-19 10:38:02 +08:00
|
|
|
|
# self.bot.on_notice()(on_message)
|
2024-09-26 00:23:03 +08:00
|
|
|
|
elif event_type == platform_events.FriendMessage:
|
2025-04-29 17:24:07 +08:00
|
|
|
|
self.bot.on_message('private')(on_message)
|
2025-06-19 10:38:02 +08:00
|
|
|
|
# self.bot.on_notice()(on_message)
|
|
|
|
|
|
# print(event_type)
|
2024-02-07 20:03:46 +08:00
|
|
|
|
|
2025-05-27 22:36:50 +08:00
|
|
|
|
async def on_websocket_connection(event: aiocqhttp.Event):
|
|
|
|
|
|
for event in self.on_websocket_connection_event_cache:
|
|
|
|
|
|
if event.self_id == event.self_id and event.time == event.time:
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
self.on_websocket_connection_event_cache.append(event)
|
|
|
|
|
|
await self.logger.info(f'WebSocket connection established, bot id: {event.self_id}')
|
|
|
|
|
|
|
|
|
|
|
|
self.bot.on_websocket_connection(on_websocket_connection)
|
|
|
|
|
|
|
2024-02-07 20:03:46 +08:00
|
|
|
|
def unregister_listener(
|
|
|
|
|
|
self,
|
2024-09-26 00:23:03 +08:00
|
|
|
|
event_type: typing.Type[platform_events.Event],
|
2025-06-16 13:18:59 +08:00
|
|
|
|
callback: typing.Callable[
|
|
|
|
|
|
[platform_events.Event, abstract_platform_adapter.AbstractMessagePlatformAdapter], None
|
|
|
|
|
|
],
|
2024-02-07 20:03:46 +08:00
|
|
|
|
):
|
|
|
|
|
|
return super().unregister_listener(event_type, callback)
|
|
|
|
|
|
|
|
|
|
|
|
async def run_async(self):
|
|
|
|
|
|
await self.bot._server_app.run_task(**self.config)
|
|
|
|
|
|
|
|
|
|
|
|
async def kill(self) -> bool:
|
2025-03-29 17:50:45 +08:00
|
|
|
|
# Current issue: existing connection will not be closed
|
|
|
|
|
|
# self.should_shutdown = True
|
2024-02-07 20:03:46 +08:00
|
|
|
|
return False
|