mirror of
https://github.com/langbot-app/LangBot.git
synced 2025-11-25 11:29:39 +08:00
Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11b1110eed | ||
|
|
682b897e21 | ||
|
|
998ad7623c | ||
|
|
4f1db33abc | ||
|
|
ca6cb60bdd | ||
|
|
133e48a5a9 | ||
|
|
d659d01b1e | ||
|
|
34f73fd84b | ||
|
|
54b87ff79d | ||
|
|
6c2843e7c1 | ||
|
|
6761a31982 | ||
|
|
9401a79b2b | ||
|
|
7a4905d943 | ||
|
|
4db1d2b3a3 | ||
|
|
2ffe2967d6 | ||
|
|
0875c0f266 | ||
|
|
e21a27ff23 | ||
|
|
91ad7944de | ||
|
|
c86602ebaf | ||
|
|
f75ac292db | ||
|
|
2742c249bf | ||
|
|
36f04849ab | ||
|
|
a60c896e89 | ||
|
|
c442320c7f | ||
|
|
6aeae7e9f5 | ||
|
|
cae79aac48 |
@@ -29,6 +29,7 @@ from ..discover import engine as discover_engine
|
|||||||
from ..utils import logcache, ip
|
from ..utils import logcache, ip
|
||||||
from . import taskmgr
|
from . import taskmgr
|
||||||
from . import entities as core_entities
|
from . import entities as core_entities
|
||||||
|
from .bootutils import config
|
||||||
|
|
||||||
|
|
||||||
class Application:
|
class Application:
|
||||||
@@ -203,6 +204,8 @@ class Application:
|
|||||||
case core_entities.LifecycleControlScope.PROVIDER.value:
|
case core_entities.LifecycleControlScope.PROVIDER.value:
|
||||||
self.logger.info("执行热重载 scope="+scope)
|
self.logger.info("执行热重载 scope="+scope)
|
||||||
|
|
||||||
|
latest_llm_model_config = await config.load_json_config("data/metadata/llm-models.json", "templates/metadata/llm-models.json")
|
||||||
|
self.llm_models_meta = latest_llm_model_config
|
||||||
llm_model_mgr_inst = llm_model_mgr.ModelManager(self)
|
llm_model_mgr_inst = llm_model_mgr.ModelManager(self)
|
||||||
await llm_model_mgr_inst.initialize()
|
await llm_model_mgr_inst.initialize()
|
||||||
self.model_mgr = llm_model_mgr_inst
|
self.model_mgr = llm_model_mgr_inst
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ class GewechatConfigMigration(migration.Migration):
|
|||||||
"adapter": "gewechat",
|
"adapter": "gewechat",
|
||||||
"enable": False,
|
"enable": False,
|
||||||
"gewechat_url": "http://your-gewechat-server:2531",
|
"gewechat_url": "http://your-gewechat-server:2531",
|
||||||
|
"gewechat_file_url": "http://your-gewechat-server:2532",
|
||||||
"port": 2286,
|
"port": 2286,
|
||||||
"callback_url": "http://your-callback-url:2286/gewechat/callback",
|
"callback_url": "http://your-callback-url:2286/gewechat/callback",
|
||||||
"app_id": "",
|
"app_id": "",
|
||||||
|
|||||||
29
pkg/core/migrations/m034_gewechat_file_url_config.py
Normal file
29
pkg/core/migrations/m034_gewechat_file_url_config.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
from .. import migration
|
||||||
|
|
||||||
|
|
||||||
|
@migration.migration_class("gewechat-file-url-config", 34)
|
||||||
|
class GewechatFileUrlConfigMigration(migration.Migration):
|
||||||
|
"""迁移"""
|
||||||
|
|
||||||
|
async def need_migrate(self) -> bool:
|
||||||
|
"""判断当前环境是否需要运行此迁移"""
|
||||||
|
|
||||||
|
for adapter in self.ap.platform_cfg.data['platform-adapters']:
|
||||||
|
if adapter['adapter'] == 'gewechat':
|
||||||
|
if 'gewechat_file_url' not in adapter:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
async def run(self):
|
||||||
|
"""执行迁移"""
|
||||||
|
for adapter in self.ap.platform_cfg.data['platform-adapters']:
|
||||||
|
if adapter['adapter'] == 'gewechat':
|
||||||
|
if 'gewechat_file_url' not in adapter:
|
||||||
|
parsed_url = urlparse(adapter['gewechat_url'])
|
||||||
|
adapter['gewechat_file_url'] = f"{parsed_url.scheme}://{parsed_url.hostname}:2532"
|
||||||
|
|
||||||
|
await self.ap.platform_cfg.dump_config()
|
||||||
@@ -11,7 +11,7 @@ from ..migrations import m015_gitee_ai_config, m016_dify_service_api, m017_dify_
|
|||||||
from ..migrations import m020_wecom_config, m021_lark_config, m022_lmstudio_config, m023_siliconflow_config, m024_discord_config, m025_gewechat_config
|
from ..migrations import m020_wecom_config, m021_lark_config, m022_lmstudio_config, m023_siliconflow_config, m024_discord_config, m025_gewechat_config
|
||||||
from ..migrations import m026_qqofficial_config, m027_wx_official_account_config, m028_aliyun_requester_config
|
from ..migrations import m026_qqofficial_config, m027_wx_official_account_config, m028_aliyun_requester_config
|
||||||
from ..migrations import m029_dashscope_app_api_config, m030_lark_config_cmpl, m031_dingtalk_config, m032_volcark_config
|
from ..migrations import m029_dashscope_app_api_config, m030_lark_config_cmpl, m031_dingtalk_config, m032_volcark_config
|
||||||
from ..migrations import m033_dify_thinking_config
|
from ..migrations import m033_dify_thinking_config, m034_gewechat_file_url_config
|
||||||
|
|
||||||
@stage.stage_class("MigrationStage")
|
@stage.stage_class("MigrationStage")
|
||||||
class MigrationStage(stage.BootingStage):
|
class MigrationStage(stage.BootingStage):
|
||||||
|
|||||||
@@ -28,7 +28,10 @@ from ...utils import image
|
|||||||
|
|
||||||
|
|
||||||
class GewechatMessageConverter(adapter.MessageConverter):
|
class GewechatMessageConverter(adapter.MessageConverter):
|
||||||
|
|
||||||
|
def __init__(self, config: dict):
|
||||||
|
self.config = config
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def yiri2target(
|
async def yiri2target(
|
||||||
message_chain: platform_message.MessageChain
|
message_chain: platform_message.MessageChain
|
||||||
@@ -40,20 +43,23 @@ class GewechatMessageConverter(adapter.MessageConverter):
|
|||||||
elif isinstance(component, platform_message.Plain):
|
elif isinstance(component, platform_message.Plain):
|
||||||
content_list.append({"type": "text", "content": component.text})
|
content_list.append({"type": "text", "content": component.text})
|
||||||
elif isinstance(component, platform_message.Image):
|
elif isinstance(component, platform_message.Image):
|
||||||
|
content_list.append({"type": "image", "url": component.url})
|
||||||
# content_list.append({"type": "image", "image_id": component.image_id})
|
# content_list.append({"type": "image", "image_id": component.image_id})
|
||||||
pass
|
#pass
|
||||||
|
elif isinstance(component, platform_message.Voice):
|
||||||
|
content_list.append({"type": "voice", "url": component.url, "length": component.length})
|
||||||
elif isinstance(component, platform_message.Forward):
|
elif isinstance(component, platform_message.Forward):
|
||||||
for node in component.node_list:
|
for node in component.node_list:
|
||||||
content_list.extend(await GewechatMessageConverter.yiri2target(node.message_chain))
|
content_list.extend(await GewechatMessageConverter.yiri2target(node.message_chain))
|
||||||
|
|
||||||
return content_list
|
return content_list
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
async def target2yiri(
|
async def target2yiri(
|
||||||
|
self,
|
||||||
message: dict,
|
message: dict,
|
||||||
bot_account_id: str
|
bot_account_id: str
|
||||||
) -> platform_message.MessageChain:
|
) -> platform_message.MessageChain:
|
||||||
|
|
||||||
if message["Data"]["MsgType"] == 1:
|
if message["Data"]["MsgType"] == 1:
|
||||||
# 检查消息开头,如果有 wxid_sbitaz0mt65n22:\n 则删掉
|
# 检查消息开头,如果有 wxid_sbitaz0mt65n22:\n 则删掉
|
||||||
regex = re.compile(r"^wxid_.*:")
|
regex = re.compile(r"^wxid_.*:")
|
||||||
@@ -74,25 +80,76 @@ class GewechatMessageConverter(adapter.MessageConverter):
|
|||||||
return platform_message.MessageChain(content_list)
|
return platform_message.MessageChain(content_list)
|
||||||
|
|
||||||
elif message["Data"]["MsgType"] == 3:
|
elif message["Data"]["MsgType"] == 3:
|
||||||
image_base64 = message["Data"]["ImgBuf"]["buffer"]
|
image_xml = message["Data"]["Content"]["string"]
|
||||||
|
if not image_xml:
|
||||||
|
return platform_message.MessageChain([
|
||||||
|
platform_message.Plain(text="[图片内容为空]")
|
||||||
|
])
|
||||||
|
|
||||||
|
try:
|
||||||
|
base64_str, image_format = await image.get_gewechat_image_base64(
|
||||||
|
gewechat_url=self.config["gewechat_url"],
|
||||||
|
gewechat_file_url=self.config["gewechat_file_url"],
|
||||||
|
app_id=self.config["app_id"],
|
||||||
|
xml_content=image_xml,
|
||||||
|
token=self.config["token"],
|
||||||
|
image_type=2,
|
||||||
|
)
|
||||||
|
|
||||||
|
return platform_message.MessageChain([
|
||||||
|
platform_message.Image(
|
||||||
|
base64=f"data:image/{image_format};base64,{base64_str}"
|
||||||
|
)
|
||||||
|
])
|
||||||
|
except Exception as e:
|
||||||
|
print(f"处理图片消息失败: {str(e)}")
|
||||||
|
return platform_message.MessageChain([
|
||||||
|
platform_message.Plain(text="[图片处理失败]")
|
||||||
|
])
|
||||||
|
elif message["Data"]["MsgType"] == 34:
|
||||||
|
audio_base64 = message["Data"]["ImgBuf"]["buffer"]
|
||||||
return platform_message.MessageChain(
|
return platform_message.MessageChain(
|
||||||
[platform_message.Image(base64=f"data:image/jpeg;base64,{image_base64}")]
|
[platform_message.Voice(base64=f"data:audio/silk;base64,{audio_base64}")]
|
||||||
)
|
)
|
||||||
|
elif message["Data"]["MsgType"] == 49:
|
||||||
|
# 支持微信聊天记录的消息类型,将 XML 内容转换为 MessageChain 传递
|
||||||
|
try:
|
||||||
|
content = message["Data"]["Content"]["string"]
|
||||||
|
|
||||||
|
try:
|
||||||
|
content_bytes = content.encode('utf-8')
|
||||||
|
decoded_content = base64.b64decode(content_bytes)
|
||||||
|
return platform_message.MessageChain(
|
||||||
|
[platform_message.Unknown(content=decoded_content)]
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return platform_message.MessageChain(
|
||||||
|
[platform_message.Plain(text=content)]
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error processing type 49 message: {str(e)}")
|
||||||
|
return platform_message.MessageChain(
|
||||||
|
[platform_message.Plain(text="[无法解析的消息]")]
|
||||||
|
)
|
||||||
|
|
||||||
class GewechatEventConverter(adapter.EventConverter):
|
class GewechatEventConverter(adapter.EventConverter):
|
||||||
|
|
||||||
|
def __init__(self, config: dict):
|
||||||
|
self.config = config
|
||||||
|
self.message_converter = GewechatMessageConverter(config)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def yiri2target(
|
async def yiri2target(
|
||||||
event: platform_events.MessageEvent
|
event: platform_events.MessageEvent
|
||||||
) -> dict:
|
) -> dict:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
async def target2yiri(
|
async def target2yiri(
|
||||||
|
self,
|
||||||
event: dict,
|
event: dict,
|
||||||
bot_account_id: str
|
bot_account_id: str
|
||||||
) -> platform_events.MessageEvent:
|
) -> platform_events.MessageEvent:
|
||||||
message_chain = await GewechatMessageConverter.target2yiri(copy.deepcopy(event), bot_account_id)
|
message_chain = await self.message_converter.target2yiri(copy.deepcopy(event), bot_account_id)
|
||||||
|
|
||||||
if not message_chain:
|
if not message_chain:
|
||||||
return None
|
return None
|
||||||
@@ -120,7 +177,7 @@ class GewechatEventConverter(adapter.EventConverter):
|
|||||||
time=event["Data"]["CreateTime"],
|
time=event["Data"]["CreateTime"],
|
||||||
source_platform_object=event,
|
source_platform_object=event,
|
||||||
)
|
)
|
||||||
elif 'wxid_' in event["Data"]["FromUserName"]["string"]:
|
else:
|
||||||
return platform_events.FriendMessage(
|
return platform_events.FriendMessage(
|
||||||
sender=platform_entities.Friend(
|
sender=platform_entities.Friend(
|
||||||
id=event["Data"]["FromUserName"]["string"],
|
id=event["Data"]["FromUserName"]["string"],
|
||||||
@@ -134,7 +191,9 @@ class GewechatEventConverter(adapter.EventConverter):
|
|||||||
|
|
||||||
|
|
||||||
class GeWeChatAdapter(adapter.MessagePlatformAdapter):
|
class GeWeChatAdapter(adapter.MessagePlatformAdapter):
|
||||||
|
|
||||||
|
name: str = "gewechat" # 定义适配器名称
|
||||||
|
|
||||||
bot: gewechat_client.GewechatClient
|
bot: gewechat_client.GewechatClient
|
||||||
quart_app: quart.Quart
|
quart_app: quart.Quart
|
||||||
|
|
||||||
@@ -144,8 +203,8 @@ class GeWeChatAdapter(adapter.MessagePlatformAdapter):
|
|||||||
|
|
||||||
ap: app.Application
|
ap: app.Application
|
||||||
|
|
||||||
message_converter: GewechatMessageConverter = GewechatMessageConverter()
|
message_converter: GewechatMessageConverter
|
||||||
event_converter: GewechatEventConverter = GewechatEventConverter()
|
event_converter: GewechatEventConverter
|
||||||
|
|
||||||
listeners: typing.Dict[
|
listeners: typing.Dict[
|
||||||
typing.Type[platform_events.Event],
|
typing.Type[platform_events.Event],
|
||||||
@@ -158,6 +217,9 @@ class GeWeChatAdapter(adapter.MessagePlatformAdapter):
|
|||||||
|
|
||||||
self.quart_app = quart.Quart(__name__)
|
self.quart_app = quart.Quart(__name__)
|
||||||
|
|
||||||
|
self.message_converter = GewechatMessageConverter(config)
|
||||||
|
self.event_converter = GewechatEventConverter(config)
|
||||||
|
|
||||||
@self.quart_app.route('/gewechat/callback', methods=['POST'])
|
@self.quart_app.route('/gewechat/callback', methods=['POST'])
|
||||||
async def gewechat_callback():
|
async def gewechat_callback():
|
||||||
data = await quart.request.json
|
data = await quart.request.json
|
||||||
@@ -183,7 +245,11 @@ class GeWeChatAdapter(adapter.MessagePlatformAdapter):
|
|||||||
target_id: str,
|
target_id: str,
|
||||||
message: platform_message.MessageChain
|
message: platform_message.MessageChain
|
||||||
):
|
):
|
||||||
pass
|
geweap_msg = await GewechatMessageConverter.yiri2target(message)
|
||||||
|
|
||||||
|
for msg in geweap_msg:
|
||||||
|
if msg['type'] == 'text':
|
||||||
|
await self.bot.post_text(app_id=self.config['app_id'], to_wxid=target_id,content=msg['content'])
|
||||||
|
|
||||||
async def reply_message(
|
async def reply_message(
|
||||||
self,
|
self,
|
||||||
@@ -281,4 +347,4 @@ class GeWeChatAdapter(adapter.MessagePlatformAdapter):
|
|||||||
)
|
)
|
||||||
|
|
||||||
async def kill(self) -> bool:
|
async def kill(self) -> bool:
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -17,6 +17,13 @@ spec:
|
|||||||
type: string
|
type: string
|
||||||
required: true
|
required: true
|
||||||
default: ""
|
default: ""
|
||||||
|
- name: gewechat_file_url
|
||||||
|
label:
|
||||||
|
en_US: GeWeChat file download URL
|
||||||
|
zh_CN: GeWeChat 文件下载URL
|
||||||
|
type: string
|
||||||
|
required: true
|
||||||
|
default: ""
|
||||||
- name: port
|
- name: port
|
||||||
label:
|
label:
|
||||||
en_US: Port
|
en_US: Port
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
semantic_version = "v3.4.9.3"
|
semantic_version = "v3.4.9.5"
|
||||||
|
|
||||||
debug_mode = False
|
debug_mode = False
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,106 @@ import aiohttp
|
|||||||
import PIL.Image
|
import PIL.Image
|
||||||
import httpx
|
import httpx
|
||||||
|
|
||||||
|
import os
|
||||||
|
import aiofiles
|
||||||
|
import pathlib
|
||||||
|
import asyncio
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
|
||||||
|
async def get_gewechat_image_base64(
|
||||||
|
gewechat_url: str,
|
||||||
|
gewechat_file_url: str,
|
||||||
|
app_id: str,
|
||||||
|
xml_content: str,
|
||||||
|
token: str,
|
||||||
|
image_type: int = 2,
|
||||||
|
) -> typing.Tuple[str, str]:
|
||||||
|
"""从gewechat服务器获取图片并转换为base64格式
|
||||||
|
|
||||||
|
Args:
|
||||||
|
gewechat_url (str): gewechat服务器地址(用于获取图片URL)
|
||||||
|
gewechat_file_url (str): gewechat文件下载服务地址
|
||||||
|
app_id (str): gewechat应用ID
|
||||||
|
xml_content (str): 图片的XML内容
|
||||||
|
token (str): Gewechat API Token
|
||||||
|
image_type (int, optional): 图片类型. Defaults to 2.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
typing.Tuple[str, str]: (base64编码, 图片格式)
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
aiohttp.ClientTimeout: 请求超时(15秒)或连接超时(2秒)
|
||||||
|
Exception: 其他错误
|
||||||
|
"""
|
||||||
|
headers = {
|
||||||
|
'X-GEWE-TOKEN': token,
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
|
|
||||||
|
# 设置超时
|
||||||
|
timeout = aiohttp.ClientTimeout(
|
||||||
|
total=15.0, # 总超时时间15秒
|
||||||
|
connect=2.0, # 连接超时2秒
|
||||||
|
sock_connect=2.0, # socket连接超时2秒
|
||||||
|
sock_read=15.0 # socket读取超时15秒
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with aiohttp.ClientSession(timeout=timeout) as session:
|
||||||
|
# 获取图片下载链接
|
||||||
|
try:
|
||||||
|
async with session.post(
|
||||||
|
f"{gewechat_url}/v2/api/message/downloadImage",
|
||||||
|
headers=headers,
|
||||||
|
json={
|
||||||
|
"appId": app_id,
|
||||||
|
"type": image_type,
|
||||||
|
"xml": xml_content
|
||||||
|
}
|
||||||
|
) as response:
|
||||||
|
if response.status != 200:
|
||||||
|
raise Exception(f"获取gewechat图片下载失败: {await response.text()}")
|
||||||
|
|
||||||
|
resp_data = await response.json()
|
||||||
|
if resp_data.get("ret") != 200:
|
||||||
|
raise Exception(f"获取gewechat图片下载链接失败: {resp_data}")
|
||||||
|
|
||||||
|
file_url = resp_data['data']['fileUrl']
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
raise Exception("获取图片下载链接超时")
|
||||||
|
except aiohttp.ClientError as e:
|
||||||
|
raise Exception(f"获取图片下载链接网络错误: {str(e)}")
|
||||||
|
|
||||||
|
# 解析原始URL并替换端口
|
||||||
|
base_url = gewechat_file_url
|
||||||
|
download_url = f"{base_url}/download/{file_url}"
|
||||||
|
|
||||||
|
# 下载图片
|
||||||
|
try:
|
||||||
|
async with session.get(download_url) as img_response:
|
||||||
|
if img_response.status != 200:
|
||||||
|
raise Exception(f"下载图片失败: {await img_response.text()}, URL: {download_url}")
|
||||||
|
|
||||||
|
image_data = await img_response.read()
|
||||||
|
|
||||||
|
content_type = img_response.headers.get('Content-Type', '')
|
||||||
|
if content_type:
|
||||||
|
image_format = content_type.split('/')[-1]
|
||||||
|
else:
|
||||||
|
image_format = file_url.split('.')[-1]
|
||||||
|
|
||||||
|
base64_str = base64.b64encode(image_data).decode('utf-8')
|
||||||
|
|
||||||
|
return base64_str, image_format
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
raise Exception(f"下载图片超时, URL: {download_url}")
|
||||||
|
except aiohttp.ClientError as e:
|
||||||
|
raise Exception(f"下载图片网络错误: {str(e)}, URL: {download_url}")
|
||||||
|
except Exception as e:
|
||||||
|
raise Exception(f"获取图片失败: {str(e)}") from e
|
||||||
|
|
||||||
|
|
||||||
async def get_wecom_image_base64(pic_url: str) -> tuple[str, str]:
|
async def get_wecom_image_base64(pic_url: str) -> tuple[str, str]:
|
||||||
"""
|
"""
|
||||||
下载企业微信图片并转换为 base64
|
下载企业微信图片并转换为 base64
|
||||||
|
|||||||
@@ -64,6 +64,7 @@
|
|||||||
"adapter": "gewechat",
|
"adapter": "gewechat",
|
||||||
"enable": false,
|
"enable": false,
|
||||||
"gewechat_url": "http://your-gewechat-server:2531",
|
"gewechat_url": "http://your-gewechat-server:2531",
|
||||||
|
"gewechat_file_url": "http://your-gewechat-server:2532",
|
||||||
"port": 2286,
|
"port": 2286,
|
||||||
"callback_url": "http://your-callback-url:2286/gewechat/callback",
|
"callback_url": "http://your-callback-url:2286/gewechat/callback",
|
||||||
"app_id": "",
|
"app_id": "",
|
||||||
|
|||||||
@@ -325,6 +325,11 @@
|
|||||||
"default": "",
|
"default": "",
|
||||||
"description": "gewechat 的 url"
|
"description": "gewechat 的 url"
|
||||||
},
|
},
|
||||||
|
"gewechat_file_url": {
|
||||||
|
"type": "string",
|
||||||
|
"default": "",
|
||||||
|
"description": "gewechat 文件下载URL"
|
||||||
|
},
|
||||||
"port": {
|
"port": {
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
"default": 2286,
|
"default": 2286,
|
||||||
|
|||||||
Reference in New Issue
Block a user