Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/langbot/pkg/platform/botmgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import langbot_plugin.api.entities.builtin.platform.events as platform_events
import langbot_plugin.api.entities.builtin.platform.message as platform_message
import langbot_plugin.api.definition.abstract.platform.adapter as abstract_platform_adapter
import langbot_plugin.api.entities.events as events


class RuntimeBot:
Expand Down Expand Up @@ -141,6 +142,56 @@ async def on_group_message(
self.adapter.register_listener(platform_events.FriendMessage, on_friend_message)
self.adapter.register_listener(platform_events.GroupMessage, on_group_message)

async def on_notice(
event: platform_events.NoticeEvent,
adapter: abstract_platform_adapter.AbstractMessagePlatformAdapter,
):
await self.logger.info(f'Notice event: {event.notice_type} {event.sub_type}')

try:
event_obj = events.NoticeReceived(
notice_type=event.notice_type,
sub_type=event.sub_type,
group_id=event.group_id,
user_id=event.user_id,
operator_id=event.operator_id,
target_id=event.target_id,
message_id=event.message_id,
duration=event.duration,
file=event.file,
honor_type=event.honor_type,
)

if hasattr(self.ap, 'plugin_connector') and self.ap.plugin_connector:
await self.ap.plugin_connector.emit_event(event_obj)
except Exception:
await self.logger.error(f'Error emitting notice event: {traceback.format_exc()}')

self.adapter.register_listener(platform_events.NoticeEvent, on_notice)

async def on_request(
event: platform_events.RequestEvent,
adapter: abstract_platform_adapter.AbstractMessagePlatformAdapter,
):
await self.logger.info(f'Request event: {event.request_type} {event.sub_type}')

try:
event_obj = events.RequestReceived(
request_type=event.request_type,
sub_type=event.sub_type,
user_id=event.user_id,
group_id=event.group_id,
comment=event.comment,
flag=event.flag,
)

if hasattr(self.ap, 'plugin_connector') and self.ap.plugin_connector:
await self.ap.plugin_connector.emit_event(event_obj)
except Exception:
await self.logger.error(f'Error emitting request event: {traceback.format_exc()}')

self.adapter.register_listener(platform_events.RequestEvent, on_request)

async def run(self):
async def exception_wrapper():
try:
Expand Down
76 changes: 73 additions & 3 deletions src/langbot/pkg/platform/sources/aiocqhttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,8 @@ async def yiri2target(event: platform_events.MessageEvent, bot_account_id: int):

@staticmethod
async def target2yiri(event: aiocqhttp.Event, bot=None):
yiri_chain = await AiocqhttpMessageConverter.target2yiri(event.message, event.message_id, bot)

if event.message_type == 'group':
yiri_chain = await AiocqhttpMessageConverter.target2yiri(event.message, event.message_id, bot)
permission = 'MEMBER'

if 'role' in event.sender:
Expand All @@ -334,6 +333,7 @@ async def target2yiri(event: aiocqhttp.Event, bot=None):
)
return converted_event
elif event.message_type == 'private':
yiri_chain = await AiocqhttpMessageConverter.target2yiri(event.message, event.message_id, bot)
return platform_events.FriendMessage(
sender=platform_entities.Friend(
id=event.sender['user_id'],
Expand All @@ -344,6 +344,57 @@ async def target2yiri(event: aiocqhttp.Event, bot=None):
time=event.time,
source_platform_object=event,
)
elif event.post_type == 'notice':
yiri_chain = platform_message.MessageChain(
[
platform_message.Source(id=-1, time=datetime.datetime.now()),
platform_message.Notice(
notice_type=event.get('notice_type', ''),
sub_type=event.get('sub_type', ''),
user_id=event.get('user_id', None),
target_id=event.get('target_id', None),
group_id=event.get('group_id', None),
operator_id=event.get('operator_id', None),
message_id=event.get('message_id', None),
duration=event.get('duration', None),
file=event.get('file', None),
honor_type=event.get('honor_type', None),
),
]
)
return platform_events.NoticeEvent(
notice_type=event.get('notice_type', ''),
sub_type=event.get('sub_type', ''),
user_id=event.get('user_id', None),
target_id=event.get('target_id', None),
group_id=event.get('group_id', None),
time=event.time,
source_platform_object=event,
)
elif event.post_type == 'request':
yiri_chain = platform_message.MessageChain(
[
platform_message.Source(id=-1, time=datetime.datetime.now()),
platform_message.Request(
request_type=event.get('request_type', ''),
sub_type=event.get('sub_type', ''),
user_id=event.get('user_id', None),
group_id=event.get('group_id', None),
comment=event.get('comment', ''),
flag=event.get('flag', ''),
),
]
)
return platform_events.RequestEvent(
request_type=event.get('request_type', ''),
sub_type=event.get('sub_type', ''),
user_id=event.get('user_id', None),
group_id=event.get('group_id', None),
comment=event.get('comment', ''),
flag=event.get('flag', ''),
time=event.time,
source_platform_object=event,
)


class AiocqhttpAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
Expand Down Expand Up @@ -413,12 +464,31 @@ async def on_message(event: aiocqhttp.Event):
await self.logger.error(f'Error in on_message: {traceback.format_exc()}')
traceback.print_exc()

async def on_notice(event: aiocqhttp.Event):
self.bot_account_id = event.self_id
try:
return await callback(await self.event_converter.target2yiri(event, self.bot), self)
except Exception:
await self.logger.error(f'Error in on_notice: {traceback.format_exc()}')
traceback.print_exc()

async def on_request(event: aiocqhttp.Event):
self.bot_account_id = event.self_id
try:
return await callback(await self.event_converter.target2yiri(event, self.bot), self)
except Exception:
await self.logger.error(f'Error in on_request: {traceback.format_exc()}')
traceback.print_exc()

if event_type == platform_events.GroupMessage:
self.bot.on_message('group')(on_message)
# self.bot.on_notice()(on_message)
elif event_type == platform_events.FriendMessage:
self.bot.on_message('private')(on_message)
# self.bot.on_notice()(on_message)
elif event_type == platform_events.NoticeEvent:
self.bot.on_notice()(on_notice)
elif event_type == platform_events.RequestEvent:
self.bot.on_request()(on_request)
# print(event_type)

async def on_websocket_connection(event: aiocqhttp.Event):
Expand Down
Loading