mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-24 19:17:14 +00:00
feat: modify for new plugin system
This commit is contained in:
@@ -9,16 +9,18 @@ import traceback
|
|||||||
import httpx
|
import httpx
|
||||||
from libs.wecom_ai_bot_api.WXBizMsgCrypt3 import WXBizMsgCrypt
|
from libs.wecom_ai_bot_api.WXBizMsgCrypt3 import WXBizMsgCrypt
|
||||||
from quart import Quart, request, Response, jsonify
|
from quart import Quart, request, Response, jsonify
|
||||||
from pkg.platform.types import message as platform_message
|
import langbot_plugin.api.entities.builtin.platform.message as platform_message
|
||||||
import asyncio
|
import asyncio
|
||||||
from libs.wecom_ai_bot_api import wecombotevent
|
from libs.wecom_ai_bot_api import wecombotevent
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
import base64
|
import base64
|
||||||
from Crypto.Cipher import AES
|
from Crypto.Cipher import AES
|
||||||
|
from pkg.platform.logger import EventLogger
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class WecomBotClient:
|
class WecomBotClient:
|
||||||
def __init__(self,Token:str,EnCodingAESKey:str,Corpid:str,logger:None):
|
def __init__(self,Token:str,EnCodingAESKey:str,Corpid:str,logger:EventLogger):
|
||||||
self.Token=Token
|
self.Token=Token
|
||||||
self.EnCodingAESKey=EnCodingAESKey
|
self.EnCodingAESKey=EnCodingAESKey
|
||||||
self.Corpid=Corpid
|
self.Corpid=Corpid
|
||||||
@@ -139,7 +141,8 @@ class WecomBotClient:
|
|||||||
reply_timestamp = str(int(time.time()))
|
reply_timestamp = str(int(time.time()))
|
||||||
ret, encrypt_text = self.wxcpt.EncryptMsg(reply_plain_str, nonce, reply_timestamp)
|
ret, encrypt_text = self.wxcpt.EncryptMsg(reply_plain_str, nonce, reply_timestamp)
|
||||||
if ret != 0:
|
if ret != 0:
|
||||||
await self.logger.error("加密失败")
|
|
||||||
|
await self.logger.error("加密失败"+str(ret))
|
||||||
|
|
||||||
|
|
||||||
root = ET.fromstring(encrypt_text)
|
root = ET.fromstring(encrypt_text)
|
||||||
@@ -155,7 +158,7 @@ class WecomBotClient:
|
|||||||
await asyncio.sleep(interval)
|
await asyncio.sleep(interval)
|
||||||
|
|
||||||
if self.msg_id_map.get(message_data['msgid'], 1) == 3:
|
if self.msg_id_map.get(message_data['msgid'], 1) == 3:
|
||||||
print('请求失效:暂不支持智能机器人超过7秒的请求,如有需求,请联系 LangBot 团队。')
|
await self.logger.error('请求失效:暂不支持智能机器人超过7秒的请求,如有需求,请联系 LangBot 团队。')
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -4,17 +4,17 @@ import asyncio
|
|||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
from pkg.platform.adapter import MessagePlatformAdapter
|
import langbot_plugin.api.definition.abstract.platform.adapter as abstract_platform_adapter
|
||||||
from pkg.platform.types import events as platform_events, message as platform_message
|
import langbot_plugin.api.entities.builtin.platform.message as platform_message
|
||||||
|
import langbot_plugin.api.entities.builtin.platform.events as platform_events
|
||||||
|
import langbot_plugin.api.entities.builtin.platform.entities as platform_entities
|
||||||
|
import pydantic
|
||||||
|
from ..logger import EventLogger
|
||||||
from libs.wecom_ai_bot_api.wecombotevent import WecomBotEvent
|
from libs.wecom_ai_bot_api.wecombotevent import WecomBotEvent
|
||||||
from libs.wecom_ai_bot_api.api import WecomBotClient
|
from libs.wecom_ai_bot_api.api import WecomBotClient
|
||||||
from .. import adapter
|
|
||||||
from ...core import app
|
from ...core import app
|
||||||
from ..types import entities as platform_entities
|
|
||||||
from ...command.errors import ParamNotEnoughError
|
|
||||||
from ..logger import EventLogger
|
|
||||||
|
|
||||||
class WecomBotMessageConverter(adapter.MessageConverter):
|
class WecomBotMessageConverter(abstract_platform_adapter.AbstractMessageConverter):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def yiri2target(message_chain: platform_message.MessageChain):
|
async def yiri2target(message_chain: platform_message.MessageChain):
|
||||||
content = ''
|
content = ''
|
||||||
@@ -36,7 +36,7 @@ class WecomBotMessageConverter(adapter.MessageConverter):
|
|||||||
|
|
||||||
return chain
|
return chain
|
||||||
|
|
||||||
class WecomBotEventConverter(adapter.EventConverter):
|
class WecomBotEventConverter(abstract_platform_adapter.AbstractEventConverter):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def yiri2target(event:platform_events.MessageEvent):
|
async def yiri2target(event:platform_events.MessageEvent):
|
||||||
@@ -82,29 +82,35 @@ class WecomBotEventConverter(adapter.EventConverter):
|
|||||||
except Exception:
|
except Exception:
|
||||||
print(traceback.format_exc())
|
print(traceback.format_exc())
|
||||||
|
|
||||||
class WecomBotAdapter(adapter.MessagePlatformAdapter):
|
class WecomBotAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
||||||
bot : WecomBotClient
|
bot: WecomBotClient
|
||||||
app: app.Application
|
bot_account_id: str
|
||||||
message_converter = WecomBotMessageConverter()
|
message_converter: WecomBotMessageConverter = WecomBotMessageConverter()
|
||||||
event_converter = WecomBotEventConverter()
|
event_converter: WecomBotEventConverter = WecomBotEventConverter()
|
||||||
config:dict
|
config: dict
|
||||||
bot_account_id:str
|
|
||||||
|
|
||||||
def __init__(self, config:dict, ap:app.Application, logger:EventLogger):
|
def __init__(self, config: dict, logger: EventLogger):
|
||||||
self.config = config
|
required_keys = ['Token', 'EncodingAESKey', 'Corpid', 'BotId', 'port']
|
||||||
self.app = ap
|
|
||||||
self.logger = logger
|
|
||||||
required_keys = ['Token', 'EncodingAESKey', 'Corpid']
|
|
||||||
missing_keys = [key for key in required_keys if key not in config]
|
missing_keys = [key for key in required_keys if key not in config]
|
||||||
if missing_keys:
|
if missing_keys:
|
||||||
raise ParamNotEnoughError('缺少相关配置项,请查看文档或联系管理员')
|
raise Exception(f'WecomBot 缺少配置项: {missing_keys}')
|
||||||
self.bot = WecomBotClient(
|
|
||||||
Token=self.config['Token'],
|
# 创建运行时 bot 对象
|
||||||
EnCodingAESKey=self.config['EncodingAESKey'],
|
bot = WecomBotClient(
|
||||||
Corpid=self.config['Corpid'],
|
Token=config['Token'],
|
||||||
logger=self.logger,
|
EnCodingAESKey=config['EncodingAESKey'],
|
||||||
|
Corpid=config['Corpid'],
|
||||||
|
logger=logger,
|
||||||
)
|
)
|
||||||
self.bot_account_id = self.config['BotId']
|
bot_account_id = config['BotId']
|
||||||
|
|
||||||
|
super().__init__(
|
||||||
|
config=config,
|
||||||
|
logger=logger,
|
||||||
|
bot=bot,
|
||||||
|
bot_account_id=bot_account_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def reply_message(self, message_source:platform_events.MessageEvent, message:platform_message.MessageChain,quote_origin: bool = False):
|
async def reply_message(self, message_source:platform_events.MessageEvent, message:platform_message.MessageChain,quote_origin: bool = False):
|
||||||
|
|
||||||
@@ -117,7 +123,7 @@ class WecomBotAdapter(adapter.MessagePlatformAdapter):
|
|||||||
def register_listener(
|
def register_listener(
|
||||||
self,
|
self,
|
||||||
event_type: typing.Type[platform_events.Event],
|
event_type: typing.Type[platform_events.Event],
|
||||||
callback: typing.Callable[[platform_events.Event, MessagePlatformAdapter], None],
|
callback: typing.Callable[[platform_events.Event, abstract_platform_adapter.AbstractMessagePlatformAdapter], None],
|
||||||
):
|
):
|
||||||
async def on_message(event: WecomBotEvent):
|
async def on_message(event: WecomBotEvent):
|
||||||
try:
|
try:
|
||||||
@@ -151,8 +157,11 @@ class WecomBotAdapter(adapter.MessagePlatformAdapter):
|
|||||||
async def unregister_listener(
|
async def unregister_listener(
|
||||||
self,
|
self,
|
||||||
event_type: type,
|
event_type: type,
|
||||||
callback: typing.Callable[[platform_events.Event, MessagePlatformAdapter], None],
|
callback: typing.Callable[[platform_events.Event, abstract_platform_adapter.AbstractMessagePlatformAdapter], None],
|
||||||
):
|
):
|
||||||
return super().unregister_listener(event_type, callback)
|
return super().unregister_listener(event_type, callback)
|
||||||
|
|
||||||
|
async def is_muted(self, group_id: int) -> bool:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user