mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-29 13:47:14 +00:00
Fixed the issue where the at bot did not remove the at symbol, result… (#2062)
* Fixed the issue where the at bot did not remove the at symbol, resulting in some commands not being activated in group chats. Also, adjusted the logic in the on_message section. * fix:reply_message del bot_name
This commit is contained in:
@@ -24,14 +24,18 @@ class WecomBotMessageConverter(abstract_platform_adapter.AbstractMessageConverte
|
|||||||
return content
|
return content
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def target2yiri(event: WecomBotEvent):
|
async def target2yiri(event: WecomBotEvent, bot_name: str = ''):
|
||||||
yiri_msg_list = []
|
yiri_msg_list = []
|
||||||
if event.type == 'group':
|
if event.type == 'group':
|
||||||
yiri_msg_list.append(platform_message.At(target=event.ai_bot_id))
|
yiri_msg_list.append(platform_message.At(target=event.ai_bot_id))
|
||||||
|
|
||||||
yiri_msg_list.append(platform_message.Source(id=event.message_id, time=datetime.datetime.now()))
|
yiri_msg_list.append(platform_message.Source(id=event.message_id, time=datetime.datetime.now()))
|
||||||
|
|
||||||
if event.content:
|
if event.content:
|
||||||
yiri_msg_list.append(platform_message.Plain(text=event.content))
|
content = event.content
|
||||||
|
if bot_name:
|
||||||
|
content = content.replace(f'@{bot_name}', '').strip()
|
||||||
|
yiri_msg_list.append(platform_message.Plain(text=content))
|
||||||
|
|
||||||
images = []
|
images = []
|
||||||
if event.images:
|
if event.images:
|
||||||
@@ -134,13 +138,15 @@ class WecomBotMessageConverter(abstract_platform_adapter.AbstractMessageConverte
|
|||||||
|
|
||||||
|
|
||||||
class WecomBotEventConverter(abstract_platform_adapter.AbstractEventConverter):
|
class WecomBotEventConverter(abstract_platform_adapter.AbstractEventConverter):
|
||||||
|
def __init__(self, bot_name: str = ''):
|
||||||
|
self.bot_name = bot_name
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def yiri2target(event: platform_events.MessageEvent):
|
async def yiri2target(event: platform_events.MessageEvent):
|
||||||
return event.source_platform_object
|
return event.source_platform_object
|
||||||
|
|
||||||
@staticmethod
|
async def target2yiri(self, event: WecomBotEvent):
|
||||||
async def target2yiri(event: WecomBotEvent):
|
message_chain = await WecomBotMessageConverter.target2yiri(event, bot_name=self.bot_name)
|
||||||
message_chain = await WecomBotMessageConverter.target2yiri(event)
|
|
||||||
if event.type == 'single':
|
if event.type == 'single':
|
||||||
return platform_events.FriendMessage(
|
return platform_events.FriendMessage(
|
||||||
sender=platform_entities.Friend(
|
sender=platform_entities.Friend(
|
||||||
@@ -180,13 +186,16 @@ class WecomBotAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
|||||||
bot: typing.Union[WecomBotClient, WecomBotWsClient]
|
bot: typing.Union[WecomBotClient, WecomBotWsClient]
|
||||||
bot_account_id: str
|
bot_account_id: str
|
||||||
message_converter: WecomBotMessageConverter = WecomBotMessageConverter()
|
message_converter: WecomBotMessageConverter = WecomBotMessageConverter()
|
||||||
event_converter: WecomBotEventConverter = WecomBotEventConverter()
|
event_converter: WecomBotEventConverter
|
||||||
config: dict
|
config: dict
|
||||||
bot_uuid: str = None
|
bot_uuid: str = None
|
||||||
_ws_mode: bool = False
|
_ws_mode: bool = False
|
||||||
|
bot_name: str = ''
|
||||||
|
listeners: dict = {}
|
||||||
|
|
||||||
def __init__(self, config: dict, logger: EventLogger):
|
def __init__(self, config: dict, logger: EventLogger):
|
||||||
enable_webhook = config.get('enable-webhook', False)
|
enable_webhook = config.get('enable-webhook', False)
|
||||||
|
bot_name = config.get('robot_name', '')
|
||||||
|
|
||||||
if not enable_webhook:
|
if not enable_webhook:
|
||||||
bot = WecomBotWsClient(
|
bot = WecomBotWsClient(
|
||||||
@@ -195,7 +204,6 @@ class WecomBotAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
|||||||
logger=logger,
|
logger=logger,
|
||||||
encoding_aes_key=config.get('EncodingAESKey', ''),
|
encoding_aes_key=config.get('EncodingAESKey', ''),
|
||||||
)
|
)
|
||||||
ws_mode = True
|
|
||||||
else:
|
else:
|
||||||
# Webhook callback mode
|
# Webhook callback mode
|
||||||
required_keys = ['Token', 'EncodingAESKey', 'Corpid']
|
required_keys = ['Token', 'EncodingAESKey', 'Corpid']
|
||||||
@@ -210,17 +218,18 @@ class WecomBotAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
|||||||
logger=logger,
|
logger=logger,
|
||||||
unified_mode=True,
|
unified_mode=True,
|
||||||
)
|
)
|
||||||
ws_mode = False
|
|
||||||
|
|
||||||
bot_account_id = config.get('BotId', '')
|
bot_account_id = config.get('BotId', '')
|
||||||
|
event_converter = WecomBotEventConverter(bot_name=bot_name)
|
||||||
super().__init__(
|
super().__init__(
|
||||||
config=config,
|
config=config,
|
||||||
logger=logger,
|
logger=logger,
|
||||||
bot=bot,
|
bot=bot,
|
||||||
bot_account_id=bot_account_id,
|
bot_account_id=bot_account_id,
|
||||||
|
bot_name=bot_name,
|
||||||
|
event_converter=event_converter,
|
||||||
)
|
)
|
||||||
self._ws_mode = ws_mode
|
self.listeners = {}
|
||||||
|
|
||||||
async def reply_message(
|
async def reply_message(
|
||||||
self,
|
self,
|
||||||
@@ -229,7 +238,9 @@ class WecomBotAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
|||||||
quote_origin: bool = False,
|
quote_origin: bool = False,
|
||||||
):
|
):
|
||||||
content = await self.message_converter.yiri2target(message)
|
content = await self.message_converter.yiri2target(message)
|
||||||
if self._ws_mode:
|
_ws_mode = not self.config.get('enable-webhook', False)
|
||||||
|
|
||||||
|
if _ws_mode:
|
||||||
event = message_source.source_platform_object
|
event = message_source.source_platform_object
|
||||||
req_id = event.get('req_id', '')
|
req_id = event.get('req_id', '')
|
||||||
if req_id:
|
if req_id:
|
||||||
@@ -249,8 +260,9 @@ class WecomBotAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
|||||||
):
|
):
|
||||||
content = await self.message_converter.yiri2target(message)
|
content = await self.message_converter.yiri2target(message)
|
||||||
msg_id = message_source.source_platform_object.message_id
|
msg_id = message_source.source_platform_object.message_id
|
||||||
|
_ws_mode = not self.config.get('enable-webhook', False)
|
||||||
|
|
||||||
if self._ws_mode:
|
if _ws_mode:
|
||||||
success = await self.bot.push_stream_chunk(msg_id, content, is_final=is_final)
|
success = await self.bot.push_stream_chunk(msg_id, content, is_final=is_final)
|
||||||
if not success and is_final:
|
if not success and is_final:
|
||||||
event = message_source.source_platform_object
|
event = message_source.source_platform_object
|
||||||
@@ -275,12 +287,22 @@ class WecomBotAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
async def send_message(self, target_type, target_id, message):
|
async def send_message(self, target_type, target_id, message):
|
||||||
if self._ws_mode:
|
_ws_mode = not self.config.get('enable-webhook', False)
|
||||||
|
if _ws_mode:
|
||||||
content = await self.message_converter.yiri2target(message)
|
content = await self.message_converter.yiri2target(message)
|
||||||
await self.bot.send_message(target_id, content)
|
await self.bot.send_message(target_id, content)
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
async def on_message(self, event: WecomBotEvent):
|
||||||
|
try:
|
||||||
|
lb_event = await self.event_converter.target2yiri(event)
|
||||||
|
if lb_event:
|
||||||
|
await self.listeners[type(lb_event)](lb_event, self)
|
||||||
|
except Exception:
|
||||||
|
await self.logger.error(f'Error in wecombot callback: {traceback.format_exc()}')
|
||||||
|
print(traceback.format_exc())
|
||||||
|
|
||||||
def register_listener(
|
def register_listener(
|
||||||
self,
|
self,
|
||||||
event_type: typing.Type[platform_events.Event],
|
event_type: typing.Type[platform_events.Event],
|
||||||
@@ -288,18 +310,13 @@ class WecomBotAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
|||||||
[platform_events.Event, abstract_platform_adapter.AbstractMessagePlatformAdapter], None
|
[platform_events.Event, abstract_platform_adapter.AbstractMessagePlatformAdapter], None
|
||||||
],
|
],
|
||||||
):
|
):
|
||||||
async def on_message(event: WecomBotEvent):
|
self.listeners[event_type] = callback
|
||||||
try:
|
|
||||||
return await callback(await self.event_converter.target2yiri(event), self)
|
|
||||||
except Exception:
|
|
||||||
await self.logger.error(f'Error in wecombot callback: {traceback.format_exc()}')
|
|
||||||
print(traceback.format_exc())
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if event_type == platform_events.FriendMessage:
|
if event_type == platform_events.FriendMessage:
|
||||||
self.bot.on_message('single')(on_message)
|
self.bot.on_message('single')(self.on_message)
|
||||||
elif event_type == platform_events.GroupMessage:
|
elif event_type == platform_events.GroupMessage:
|
||||||
self.bot.on_message('group')(on_message)
|
self.bot.on_message('group')(self.on_message)
|
||||||
except Exception:
|
except Exception:
|
||||||
print(traceback.format_exc())
|
print(traceback.format_exc())
|
||||||
|
|
||||||
@@ -308,12 +325,14 @@ class WecomBotAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
|||||||
self.bot_uuid = bot_uuid
|
self.bot_uuid = bot_uuid
|
||||||
|
|
||||||
async def handle_unified_webhook(self, bot_uuid: str, path: str, request):
|
async def handle_unified_webhook(self, bot_uuid: str, path: str, request):
|
||||||
if self._ws_mode:
|
_ws_mode = not self.config.get('enable-webhook', False)
|
||||||
|
if _ws_mode:
|
||||||
return None
|
return None
|
||||||
return await self.bot.handle_unified_webhook(request)
|
return await self.bot.handle_unified_webhook(request)
|
||||||
|
|
||||||
async def run_async(self):
|
async def run_async(self):
|
||||||
if self._ws_mode:
|
_ws_mode = not self.config.get('enable-webhook', False)
|
||||||
|
if _ws_mode:
|
||||||
await self.bot.connect()
|
await self.bot.connect()
|
||||||
else:
|
else:
|
||||||
|
|
||||||
@@ -324,7 +343,8 @@ class WecomBotAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
|||||||
await keep_alive()
|
await keep_alive()
|
||||||
|
|
||||||
async def kill(self) -> bool:
|
async def kill(self) -> bool:
|
||||||
if self._ws_mode:
|
_ws_mode = not self.config.get('enable-webhook', False)
|
||||||
|
if _ws_mode:
|
||||||
await self.bot.disconnect()
|
await self.bot.disconnect()
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|||||||
@@ -18,6 +18,13 @@ spec:
|
|||||||
type: string
|
type: string
|
||||||
required: true
|
required: true
|
||||||
default: ""
|
default: ""
|
||||||
|
- name: robot_name
|
||||||
|
label:
|
||||||
|
en_US: Robot Name
|
||||||
|
zh_Hans: 机器人名称
|
||||||
|
type: string
|
||||||
|
required: true
|
||||||
|
default: ""
|
||||||
- name: enable-webhook
|
- name: enable-webhook
|
||||||
label:
|
label:
|
||||||
en_US: Enable Webhook Mode
|
en_US: Enable Webhook Mode
|
||||||
|
|||||||
Reference in New Issue
Block a user