feat: aiocqhttp 适配器适配主动发送消息接口
This commit is contained in:
@@ -165,7 +165,7 @@ class AIOCQHTTP(Platform):
|
||||
|
||||
await self._reply(message, res)
|
||||
|
||||
async def _reply(self, message: AstrBotMessage, message_chain: List[BaseMessageComponent]):
|
||||
async def _reply(self, message: Union[AstrBotMessage, Dict], message_chain: List[BaseMessageComponent]):
|
||||
if isinstance(message_chain, str):
|
||||
message_chain = [Plain(text=message_chain), ]
|
||||
|
||||
@@ -179,7 +179,15 @@ class AIOCQHTTP(Platform):
|
||||
image_idx.append(idx)
|
||||
ret.append(d)
|
||||
try:
|
||||
await self.bot.send(message.raw_message, ret)
|
||||
if isinstance(message, AstrBotMessage):
|
||||
await self.bot.send(message.raw_message, ret)
|
||||
if isinstance(message, dict):
|
||||
if 'group_id' in message:
|
||||
await self.bot.send_group_msg(group_id=message['group_id'], message=ret)
|
||||
elif 'user_id' in message:
|
||||
await self.bot.send_private_msg(user_id=message['user_id'], message=ret)
|
||||
else:
|
||||
raise Exception("aiocqhttp: 无法识别的消息来源。仅支持 group_id 和 user_id。")
|
||||
except ActionFailed as e:
|
||||
logger.error(traceback.format_exc())
|
||||
logger.error(f"回复消息失败: {e}")
|
||||
@@ -195,4 +203,19 @@ class AIOCQHTTP(Platform):
|
||||
logger.info(f"上传成功。")
|
||||
ret[idx]['data']['file'] = image_url
|
||||
ret[idx]['data']['path'] = image_url
|
||||
await self.bot.send(message.raw_message, ret)
|
||||
await self.bot.send(message.raw_message, ret)
|
||||
|
||||
async def send_msg(self, target: Dict[str, int], result_message: Union[List[BaseMessageComponent], str]):
|
||||
'''
|
||||
以主动的方式给QQ用户、QQ群发送一条消息。
|
||||
|
||||
`target` 接收一个 dict 类型的值引用。
|
||||
|
||||
- 要发给 QQ 下的某个用户,请添加 key `user_id`,值为 int 类型的 qq 号;
|
||||
- 要发给某个群聊,请添加 key `group_id`,值为 int 类型的 qq 群号;
|
||||
|
||||
'''
|
||||
if isinstance(result_message, str):
|
||||
result_message = [Plain(text=result_message), ]
|
||||
|
||||
await self._reply(target, result_message)
|
||||
@@ -306,8 +306,7 @@ class QQOfficial(Platform):
|
||||
async def _reply(self, **kwargs):
|
||||
if 'group_openid' in kwargs:
|
||||
# QQ群组消息
|
||||
# qq群组消息需要自行上传,暂时不处理
|
||||
if 'file_image' in kwargs:
|
||||
if 'file_image' in kwargs and kwargs['file_image']:
|
||||
file_image_path = kwargs['file_image'].replace("file:///", "")
|
||||
if file_image_path:
|
||||
|
||||
@@ -325,7 +324,7 @@ class QQOfficial(Platform):
|
||||
await self.client.api.post_group_message(**kwargs)
|
||||
elif 'channel_id' in kwargs:
|
||||
# 频道消息
|
||||
if 'file_image' in kwargs:
|
||||
if 'file_image' in kwargs and kwargs['file_image']:
|
||||
kwargs['file_image'] = kwargs['file_image'].replace("file:///", "")
|
||||
# 频道消息发图只支持本地
|
||||
if kwargs['file_image'].startswith("http"):
|
||||
@@ -333,7 +332,7 @@ class QQOfficial(Platform):
|
||||
await self.client.api.post_message(**kwargs)
|
||||
else:
|
||||
# 频道私聊消息
|
||||
if 'file_image' in kwargs:
|
||||
if 'file_image' in kwargs and kwargs['file_image']:
|
||||
kwargs['file_image'] = kwargs['file_image'].replace("file:///", "")
|
||||
if kwargs['file_image'].startswith("http"):
|
||||
kwargs['file_image'] = await download_image_by_url(kwargs['file_image'])
|
||||
@@ -349,6 +348,7 @@ class QQOfficial(Platform):
|
||||
- 如果目标是 频道消息,请添加 key `channel_id`。
|
||||
- 如果目标是 频道私聊,请添加 key `guild_id`。
|
||||
'''
|
||||
image_path = None
|
||||
if isinstance(result_message, list):
|
||||
plain_text, image_path = await self._parse_to_qqofficial(result_message)
|
||||
else:
|
||||
@@ -356,9 +356,10 @@ class QQOfficial(Platform):
|
||||
|
||||
payload = {
|
||||
'content': plain_text,
|
||||
'file_image': image_path,
|
||||
**target
|
||||
}
|
||||
if image_path:
|
||||
payload['file_image'] = image_path
|
||||
await self._reply(**payload)
|
||||
|
||||
def wait_for_message(self, channel_id: int) -> AstrBotMessage:
|
||||
|
||||
@@ -8,4 +8,5 @@ Platform类是消息平台的抽象类,定义了消息平台的基本接口。
|
||||
from model.platform import Platform
|
||||
|
||||
from model.platform.qq_nakuru import QQGOCQ
|
||||
from model.platform.qq_official import QQOfficial
|
||||
from model.platform.qq_official import QQOfficial
|
||||
from model.platform.qq_aiocqhttp import AIOCQHTTP
|
||||
Reference in New Issue
Block a user