diff --git a/astrbot/core/platform/sources/gewechat/gewechat_event.py b/astrbot/core/platform/sources/gewechat/gewechat_event.py index da94aff66..f62c71145 100644 --- a/astrbot/core/platform/sources/gewechat/gewechat_event.py +++ b/astrbot/core/platform/sources/gewechat/gewechat_event.py @@ -6,7 +6,7 @@ from astrbot.core.utils.tencent_record_helper import wav_to_tencent_silk from astrbot.api import logger from astrbot.api.event import AstrMessageEvent, MessageChain from astrbot.api.platform import AstrBotMessage, PlatformMetadata -from astrbot.api.message_components import Plain, Image, Record, At, File, Reply +from astrbot.api.message_components import Plain, Image, Record, At, File from .client import SimpleGewechatClient def get_wav_duration(file_path): @@ -104,23 +104,10 @@ class GewechatPlatformEvent(AstrMessageEvent): record_path = record_url silk_path = f"data/temp/{uuid.uuid4()}.silk" - logger.info("开始转换语音文件: " + record_path) duration = await wav_to_tencent_silk(record_path, silk_path) - - print(f"duration: {duration}, {silk_path}") - - # 检查 record_path 是否在 data/temp 目录中, record_path 可能是绝对路径 - # temp_directory = os.path.abspath('data/temp') - # record_path = os.path.abspath(record_path) - # if os.path.commonpath([temp_directory, record_path]) != temp_directory: - # with open(record_path, "rb") as f: - # record_path = f"data/temp/{uuid.uuid4()}.wav" - # with open(record_path, "wb") as f2: - # f2.write(f.read()) - + logger.info("Silk 语音文件格式转换至: " + record_path) if duration == 0: duration = get_wav_duration(record_path) - file_id = os.path.basename(silk_path) record_url = f"{self.client.file_server_url}/{file_id}" logger.debug(f"gewe callback record url: {record_url}") @@ -138,6 +125,8 @@ class GewechatPlatformEvent(AstrMessageEvent): file_id = os.path.basename(file_path) file_url = f"{self.client.file_server_url}/{file_id}" logger.debug(f"gewe callback file url: {file_url}") - await self.client.post_file(to_wxid, file_url, file_id) + await self.client.post_file(to_wxid, file_url, file_id) + else: + logger.error(f"gewechat 暂不支持发送消息类型: {comp.type}") await super().send(message) \ No newline at end of file diff --git a/astrbot/core/platform/sources/qqofficial/qqofficial_message_event.py b/astrbot/core/platform/sources/qqofficial/qqofficial_message_event.py index 9cadb1447..ef66486bf 100644 --- a/astrbot/core/platform/sources/qqofficial/qqofficial_message_event.py +++ b/astrbot/core/platform/sources/qqofficial/qqofficial_message_event.py @@ -8,6 +8,7 @@ from astrbot.api.platform import AstrBotMessage, PlatformMetadata from astrbot.api.message_components import Plain, Image, Reply from botpy import Client from botpy.http import Route +from astrbot.api import logger class QQOfficialMessageEvent(AstrMessageEvent): @@ -114,4 +115,6 @@ class QQOfficialMessageEvent(AstrMessageEvent): else: image_base64 = file_to_base64(i.file).replace("base64://", "") image_file_path = i.file + else: + logger.error(f"qq_official 暂不支持发送消息类型 {i.type}") return plain_text, image_base64, image_file_path \ No newline at end of file diff --git a/astrbot/core/platform/sources/vchat/vchat_message_event.py b/astrbot/core/platform/sources/vchat/vchat_message_event.py deleted file mode 100644 index 13242138f..000000000 --- a/astrbot/core/platform/sources/vchat/vchat_message_event.py +++ /dev/null @@ -1,44 +0,0 @@ -import random -import asyncio -from astrbot.core.utils.io import download_image_by_url -from astrbot.api import logger -from astrbot.api.event import AstrMessageEvent, MessageChain -from astrbot.api.platform import AstrBotMessage, PlatformMetadata -from astrbot.api.message_components import Plain, Image -from vchat import Core - -class VChatPlatformEvent(AstrMessageEvent): - def __init__(self, message_str: str, message_obj: AstrBotMessage, platform_meta: PlatformMetadata, session_id: str, client: Core): - super().__init__(message_str, message_obj, platform_meta, session_id) - self.client = client - - @staticmethod - async def send_with_client(client: Core, message: MessageChain, user_name: str): - plain = "" - for comp in message.chain: - if isinstance(comp, Plain): - if message.is_split_: - await client.send_msg(comp.text, user_name) - else: - plain += comp.text - elif isinstance(comp, Image): - if comp.file and comp.file.startswith("file:///"): - file_path = comp.file.replace("file:///", "") - with open(file_path, "rb") as f: - await client.send_image(user_name, fd=f) - elif comp.file and comp.file.startswith("http"): - image_path = await download_image_by_url(comp.file) - with open(image_path, "rb") as f: - await client.send_image(user_name, fd=f) - else: - logger.error(f"不支持的 vchat(微信适配器) 消息类型: {comp}") - await asyncio.sleep(random.uniform(0.5, 1.5)) # 🤓 - - if plain: - await client.send_msg(plain, user_name) - - - async def send(self, message: MessageChain): - await VChatPlatformEvent.send_with_client(self.client, message, self.message_obj.raw_message.from_.username) - await super().send(message) - \ No newline at end of file diff --git a/astrbot/core/platform/sources/vchat/vchat_platform_adapter.py b/astrbot/core/platform/sources/vchat/vchat_platform_adapter.py deleted file mode 100644 index f224ecf8f..000000000 --- a/astrbot/core/platform/sources/vchat/vchat_platform_adapter.py +++ /dev/null @@ -1,120 +0,0 @@ -import sys -import time -import uuid -import asyncio -import os - -from astrbot.api.platform import Platform, AstrBotMessage, MessageMember, MessageType, PlatformMetadata -from astrbot.api.event import MessageChain -from astrbot.api.message_components import * -from astrbot.api import logger -from astrbot.core.platform.astr_message_event import MessageSesion -from .vchat_message_event import VChatPlatformEvent -from ...register import register_platform_adapter - -from vchat import Core -from vchat import model - -if sys.version_info >= (3, 12): - from typing import override -else: - from typing_extensions import override - -@register_platform_adapter("vchat", "基于 VChat 的 Wechat 适配器") -class VChatPlatformAdapter(Platform): - - def __init__(self, platform_config: dict, platform_settings: dict, event_queue: asyncio.Queue) -> None: - super().__init__(event_queue) - self.config = platform_config - self.settingss = platform_settings - self.test_mode = os.environ.get('TEST_MODE', 'off') == 'on' - self.client_self_id = uuid.uuid4().hex[:8] - - @override - async def send_by_session(self, session: MessageSesion, message_chain: MessageChain): - from_username = session.session_id.split('$$')[0] - await VChatPlatformEvent.send_with_client(self.client, message_chain, from_username) - await super().send_by_session(session, message_chain) - - @override - def meta(self) -> PlatformMetadata: - return PlatformMetadata( - "vchat", - "基于 VChat 的 Wechat 适配器", - ) - - @override - def run(self): - self.client = Core() - @self.client.msg_register(msg_types=model.ContentTypes.TEXT, - contact_type=model.ContactTypes.CHATROOM | model.ContactTypes.USER) - async def _(msg: model.Message): - if isinstance(msg.content, model.UselessContent): - return - if msg.create_time < self.start_time: - logger.debug(f"忽略旧消息: {msg}") - return - logger.debug(f"收到消息: {msg.todict()}") - abmsg = self.convert_message(msg) - # await self.handle_msg(abmsg) # 不能直接调用,否则会阻塞 - asyncio.create_task(self.handle_msg(abmsg)) - - # TODO: 对齐微信服务器时间 - self.start_time = int(time.time()) - return self._run() - - - async def _run(self): - await self.client.init() - await self.client.auto_login(hot_reload=True, enable_cmd_qr=True) - await self.client.run() - - def convert_message(self, msg: model.Message) -> AstrBotMessage: - # credits: https://github.com/z2z63/astrbot_plugin_vchat/blob/master/main.py#L49 - assert isinstance(msg.content, model.TextContent) - amsg = AstrBotMessage() - amsg.message = [Plain(msg.content.content)] - amsg.self_id = self.client_self_id - if msg.content.is_at_me: - amsg.message.insert(0, At(qq=amsg.self_id)) - - sender = msg.chatroom_sender or msg.from_ - amsg.sender = MessageMember(sender.username, sender.nickname) - - if msg.content.is_at_me: - amsg.message_str = msg.content.content.split("\u2005")[1].strip() - else: - amsg.message_str = msg.content.content - amsg.message_id = msg.message_id - if isinstance(msg.from_, model.User): - amsg.type = MessageType.FRIEND_MESSAGE - elif isinstance(msg.from_, model.Chatroom): - amsg.type = MessageType.GROUP_MESSAGE - amsg.group_id = msg.from_.username - else: - logger.error(f"不支持的 Wechat 消息类型: {msg.from_}") - - amsg.raw_message = msg - - if self.settingss['unique_session']: - session_id = msg.from_.username + "$$" + msg.to.username - if msg.chatroom_sender is not None: - session_id += '$$' + msg.chatroom_sender.username - else: - session_id = msg.from_.username - - amsg.session_id = session_id - return amsg - - async def handle_msg(self, message: AstrBotMessage): - message_event = VChatPlatformEvent( - message_str=message.message_str, - message_obj=message, - platform_meta=self.meta(), - session_id=message.session_id, - client=self.client - ) - - logger.info(f"处理消息: {message_event}") - - self.commit_event(message_event) \ No newline at end of file diff --git a/astrbot/core/platform/sources/webchat/webchat_event.py b/astrbot/core/platform/sources/webchat/webchat_event.py index f447a616c..8345b5c51 100644 --- a/astrbot/core/platform/sources/webchat/webchat_event.py +++ b/astrbot/core/platform/sources/webchat/webchat_event.py @@ -1,8 +1,9 @@ import os import uuid +from astrbot.api import logger from astrbot.api.event import AstrMessageEvent, MessageChain from astrbot.api.message_components import Plain, Image -from astrbot.core.utils.io import file_to_base64, download_image_by_url +from astrbot.core.utils.io import download_image_by_url from astrbot.core import web_chat_back_queue class WebChatMessageEvent(AstrMessageEvent): @@ -37,5 +38,7 @@ class WebChatMessageEvent(AstrMessageEvent): with open(comp.file, "rb") as f2: f.write(f2.read()) web_chat_back_queue.put_nowait((f"[IMAGE]{filename}", cid)) + else: + logger.error(f"webchat 暂不支持发送消息类型: {comp.type}") web_chat_back_queue.put_nowait(None) await super().send(message) \ No newline at end of file diff --git a/astrbot/core/provider/sources/openai_source.py b/astrbot/core/provider/sources/openai_source.py index 8062a055c..9b7803491 100644 --- a/astrbot/core/provider/sources/openai_source.py +++ b/astrbot/core/provider/sources/openai_source.py @@ -2,7 +2,7 @@ import base64 import json import os -from openai import AsyncOpenAI, AsyncAzureOpenAI, NOT_GIVEN +from openai import AsyncOpenAI, AsyncAzureOpenAI from openai.types.chat.chat_completion import ChatCompletion from openai._exceptions import NotFoundError, UnprocessableEntityError from astrbot.core.utils.io import download_image_by_url