Skip to content

Commit

Permalink
✨ 支持新版本adapter-satori
Browse files Browse the repository at this point in the history
  • Loading branch information
AzideCupric committed Jul 30, 2024
1 parent 537ec7b commit 87790e2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
18 changes: 9 additions & 9 deletions nonebot_plugin_saa/adapters/satori.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class SatoriPlatform(str, Enum):
from nonebot.adapters.satori import Bot as BotSatori
from nonebot.adapters.satori.models import PageResult
from nonebot.adapters.satori import Message, MessageSegment
from nonebot.adapters.satori.models import InnerMessage as SatoriMessage
from nonebot.adapters.satori.models import MessageObject as SatoriMessage
from nonebot.adapters.satori.event import (
MessageEvent,
PublicMessageEvent,
Expand Down Expand Up @@ -146,35 +146,35 @@ def _extract_group_msg_event(event: Event) -> PlatformTarget:
@register_convert_to_arg(adapter, SupportedPlatform.qq_private)
def _gen_qq_private(target: PlatformTarget) -> dict[str, Any]:
assert isinstance(target, TargetQQPrivate)
return {"channel_id": f"private:{target.user_id}"}
return {"channel": f"private:{target.user_id}"}

@register_convert_to_arg(adapter, SupportedPlatform.qq_group)
def _gen_qq_group(target: PlatformTarget) -> dict[str, Any]:
assert isinstance(target, TargetQQGroup)
return {"channel_id": str(target.group_id)}
return {"channel": str(target.group_id)}

@register_convert_to_arg(adapter, SupportedPlatform.feishu_private)
def _gen_feishu_private(target: PlatformTarget) -> dict[str, Any]:
assert isinstance(target, TargetFeishuPrivate)
return {"channel_id": target.open_id}
return {"channel": target.open_id}

Check warning on line 159 in nonebot_plugin_saa/adapters/satori.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/adapters/satori.py#L159

Added line #L159 was not covered by tests

@register_convert_to_arg(adapter, SupportedPlatform.feishu_group)
def _gen_feishu_group(target: PlatformTarget) -> dict[str, Any]:
assert isinstance(target, TargetFeishuGroup)
return {"channel_id": target.chat_id}
return {"channel": target.chat_id}

Check warning on line 164 in nonebot_plugin_saa/adapters/satori.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/adapters/satori.py#L164

Added line #L164 was not covered by tests

@register_convert_to_arg(adapter, SupportedPlatform.telegram_common)
def _gen_telegram_common(target: PlatformTarget) -> dict[str, Any]:
assert isinstance(target, TargetTelegramCommon)
return {"channel_id": target.chat_id}
return {"channel": target.chat_id}

Check warning on line 169 in nonebot_plugin_saa/adapters/satori.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/adapters/satori.py#L169

Added line #L169 was not covered by tests

@register_convert_to_arg(adapter, SupportedPlatform.unknown_satori)
def _to_unknow(target: PlatformTarget):
assert isinstance(target, TargetSatoriUnknown)
# TODO: 如果是私聊,需要先创建私聊会话
if target.channel_id is None:
raise NotImplementedError
return {"channel_id": target.channel_id}
return {"channel": target.channel_id}

class SatoriReceipt(Receipt):
adapter_name: Literal[adapter] = adapter
Expand Down Expand Up @@ -228,7 +228,7 @@ async def send(

if event:
resp = await bot.send_message(
message=message_to_send, channel_id=event.channel.id
message=message_to_send, channel=event.channel
)
else:
resp = await bot.send_message(
Expand Down Expand Up @@ -258,7 +258,7 @@ async def aggregate_send(

if event:
assert isinstance(event, MessageEvent)
await bot.send_message(message=message_to_send, channel_id=event.channel.id)
await bot.send_message(message=message_to_send, channel=event.channel)
else:
await bot.send_message(message=message_to_send, **target.arg_dict(bot))

Expand Down
12 changes: 6 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ nonebot-adapter-feishu = "^2.4.0"
nonebot-adapter-red = "^0.9.0"
nonebot-adapter-dodo = "^0.2.0"
nonebot-adapter-qq = "^1.4.1"
nonebot-adapter-satori = "^0.10.2"
nonebot-adapter-satori = "^0.12.3"
nonebot-adapter-discord = "^0.1.7"

[tool.black]
Expand Down
29 changes: 15 additions & 14 deletions tests/test_satori.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
from nonebot.compat import type_validate_python
from nonebot.adapters.satori import Bot, Adapter
from nonebot.adapters.satori.config import ClientInfo
from nonebot.adapters.satori.models import Login, LoginStatus

from .utils import assert_ms, mock_satori_message_event

satori_kwargs: dict[Literal["platform", "info"], Any] = {
"platform": "qq",
satori_kwargs: dict[Literal["login", "info"], Any] = {
"login": Login(status=LoginStatus.ONLINE, platform="qq"),
"info": ClientInfo(port=12345),
}

Expand Down Expand Up @@ -104,7 +105,7 @@ async def test_reply(app: App, assert_satori):
async def test_send(app: App):
from nonebot import get_driver, on_message
from nonebot.adapters.satori import Bot, MessageEvent
from nonebot.adapters.satori.models import InnerMessage
from nonebot.adapters.satori.models import MessageObject

from nonebot_plugin_saa import Text, MessageFactory, SupportedAdapters

Expand All @@ -125,14 +126,14 @@ async def process(msg: MessageEvent):
"channel_id": "67890",
"content": "123",
},
[InnerMessage(id="321", content="321").model_dump()],
[MessageObject(id="321", content="321").model_dump()],
)


async def test_extract_message_id(app: App):
from nonebot import get_driver, on_message
from nonebot.adapters.satori import Bot, MessageEvent
from nonebot.adapters.satori.models import InnerMessage
from nonebot.adapters.satori.models import MessageObject

from nonebot_plugin_saa import Text, MessageFactory, SupportedAdapters
from nonebot_plugin_saa.adapters.satori import SatoriReceipt, SatoriMessageId
Expand All @@ -156,14 +157,14 @@ async def process(msg: MessageEvent):
"channel_id": "67890",
"content": "123",
},
[InnerMessage(id="321", content="321").model_dump()],
[MessageObject(id="321", content="321").model_dump()],
)


async def test_send_with_reply_and_revoke(app: App):
from nonebot import get_driver, on_message
from nonebot.adapters.satori import Bot, MessageEvent
from nonebot.adapters.satori.models import Channel, ChannelType, InnerMessage
from nonebot.adapters.satori.models import Channel, ChannelType, MessageObject

from nonebot_plugin_saa import Text, MessageFactory, SupportedAdapters

Expand All @@ -186,7 +187,7 @@ async def process(msg: MessageEvent):
"content": "123",
},
[
InnerMessage(
MessageObject(
id="321",
content="321",
channel=Channel(id="67890", type=ChannelType.TEXT),
Expand All @@ -202,7 +203,7 @@ async def process(msg: MessageEvent):
async def test_send_active(app: App):
from nonebot import get_driver, on_message
from nonebot.adapters.satori import Bot, MessageEvent
from nonebot.adapters.satori.models import InnerMessage
from nonebot.adapters.satori.models import MessageObject

from nonebot_plugin_saa import (
Text,
Expand Down Expand Up @@ -234,7 +235,7 @@ async def process(msg: MessageEvent):
"channel_id": "12345",
"content": "123",
},
[InnerMessage(id="321", content="321").model_dump()],
[MessageObject(id="321", content="321").model_dump()],
)
await MessageFactory(Text("123")).send_to(send_target_qq_group, bot)

Expand All @@ -244,7 +245,7 @@ async def process(msg: MessageEvent):
"channel_id": "private:12345",
"content": "123",
},
[InnerMessage(id="321", content="321").model_dump()],
[MessageObject(id="321", content="321").model_dump()],
)
await MessageFactory(Text("123")).send_to(send_target_qq_private, bot)

Expand All @@ -254,15 +255,15 @@ async def process(msg: MessageEvent):
"channel_id": "67890",
"content": "123",
},
[InnerMessage(id="321", content="321").model_dump()],
[MessageObject(id="321", content="321").model_dump()],
)
await MessageFactory(Text("123")).send_to(send_target_satori_unknown, bot)


async def test_send_aggreted_satori(app: App):
from nonebot import get_driver, on_message
from nonebot.adapters.satori import Bot, MessageEvent
from nonebot.adapters.satori.models import InnerMessage
from nonebot.adapters.satori.models import MessageObject

from nonebot_plugin_saa import Text, SupportedAdapters, AggregatedMessageFactory

Expand All @@ -282,7 +283,7 @@ async def process(msg: MessageEvent):
"channel_id": "67890",
"content": "<message>123</message><message>456</message>",
},
[InnerMessage(id="321", content="321").model_dump()],
[MessageObject(id="321", content="321").model_dump()],
)
ctx.receive_event(bot, msg_event)

Expand Down

0 comments on commit 87790e2

Please sign in to comment.