From 46a993b9a3d159980b1d06356976724262d5e740 Mon Sep 17 00:00:00 2001 From: Dong_master <2213070223@qq.com> Date: Fri, 27 Feb 2026 05:42:26 +0800 Subject: [PATCH] feat: Add support for notification and request events, and enhance the event handling capabilities. --- src/langbot/pkg/platform/botmgr.py | 51 +++++++++++++ src/langbot/pkg/platform/sources/aiocqhttp.py | 76 ++++++++++++++++++- 2 files changed, 124 insertions(+), 3 deletions(-) diff --git a/src/langbot/pkg/platform/botmgr.py b/src/langbot/pkg/platform/botmgr.py index 7540808d9..384bb441b 100644 --- a/src/langbot/pkg/platform/botmgr.py +++ b/src/langbot/pkg/platform/botmgr.py @@ -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: @@ -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: diff --git a/src/langbot/pkg/platform/sources/aiocqhttp.py b/src/langbot/pkg/platform/sources/aiocqhttp.py index a8cf8acc9..5fa0f4cc3 100644 --- a/src/langbot/pkg/platform/sources/aiocqhttp.py +++ b/src/langbot/pkg/platform/sources/aiocqhttp.py @@ -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: @@ -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'], @@ -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): @@ -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):