Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zhenxunbot连接shamrock发送图片的问题 #1501

Open
HuaWaterED opened this issue Nov 26, 2023 · 12 comments
Open

zhenxunbot连接shamrock发送图片的问题 #1501

HuaWaterED opened this issue Nov 26, 2023 · 12 comments

Comments

@HuaWaterED
Copy link

系统版本:Ubuntu 20.04

真寻版本:main分支最新commit

错误截图

QQ图片20231126132000

日志截图

Snipaste_2023-11-26_13-20-35

错误说明

zhenxunbot连接shamrock(可以理解为gocq的平替(?),连接成功,并且可以回应文本消息,在发送图片的时候,可能因为shamrock和真寻bot不在同一个机器上导致的报错:找不到一个png文件。我在电脑上看到实际存在这个png文件,shamrock找不到这个文件,可以有其他的方式来尝试上传文件吗?

@PackageInstaller
Copy link

用base64吧,不过要改源码

@HuaWaterED
Copy link
Author

emmm,我用另一种方式解决了

@HuaWaterED
Copy link
Author

就是,把Windows的smb挂载到了安卓手机的/mnt/c目录下,实现了shamrock直接通过路径访问Windows的文件,也可以访问,现在就是,图片会这样:
Snipaste_2023-11-27_18-00-20

@HuaWaterED
Copy link
Author

(我不会python语言,所以想的另一个方法

@PackageInstaller
Copy link

就是,把Windows的smb挂载到了安卓手机的/mnt/c目录下,实现了shamrock直接通过路径访问Windows的文件,也可以访问,现在就是,图片会这样: Snipaste_2023-11-27_18-00-20

Html 版的老问题,换回正常版显示正常

@YingLing3
Copy link

YingLing3 commented Dec 5, 2023

修改\utils\message_builder.py,修改` if isinstance(file, Path):这个判断下的逻辑,

    if isinstance(file, Path):
        if file.exists():
            # return MessageSegment.image(file)
            try:
                with open(file.absolute(), 'rb') as file:
                    # 读取图片文件并进行 Base64 编码
                    image_data = base64.b64encode(file.read())
                    image_base64 = image_data.decode('utf-8')
                    # print(image_base64)
                    return MessageSegment.image("base64://" + image_base64)
            except FileNotFoundError:
                logger.warning(f"图片 {file.absolute()} 缺失...")
        logger.warning(f"图片 {file.absolute()}缺失...")

别忘了最上面加上import base64
或者你懒直接粘贴我的image方法

def image(
    file: Optional[Union[str, Path, bytes, BuildImage, io.BytesIO]] = None,
    b64: Optional[str] = None,
) -> MessageSegment:
    """
    说明:
        生成一个 MessageSegment.image 消息
        生成顺序:绝对路径(abspath) > base64(b64) > img_name
    参数:
        :param file: 图片文件
        :param b64: 图片base64(兼容旧方法)
    """
    if b64:
        file = b64 if b64.startswith("base64://") else ("base64://" + b64)
    if isinstance(file, str):
        if file.startswith(("http", "base64://")):
            return MessageSegment.image(file)
        else:
            return MessageSegment.image(IMAGE_PATH / file)
    if isinstance(file, Path):
        if file.exists():
            # return MessageSegment.image(file)
            try:
                with open(file.absolute(), 'rb') as file:
                    # 读取图片文件并进行 Base64 编码
                    image_data = base64.b64encode(file.read())
                    image_base64 = image_data.decode('utf-8')
                    # print(image_base64)
                    return MessageSegment.image("base64://" + image_base64)
            except FileNotFoundError:
                logger.warning(f"图片 {file.absolute()} 缺失...")
        logger.warning(f"图片 {file.absolute()}缺失...")
    if isinstance(file, (bytes, io.BytesIO)):
        return MessageSegment.image(file)
    if isinstance(file, BuildImage):
        return MessageSegment.image(file.pic2bs4())
    return MessageSegment.image("")

@ReactXS
Copy link

ReactXS commented Jan 11, 2024

修改\utils\message_builder.py,修改` if isinstance(file, Path):这个判断下的逻辑,

    if isinstance(file, Path):
        if file.exists():
            # return MessageSegment.image(file)
            try:
                with open(file.absolute(), 'rb') as file:
                    # 读取图片文件并进行 Base64 编码
                    image_data = base64.b64encode(file.read())
                    image_base64 = image_data.decode('utf-8')
                    # print(image_base64)
                    return MessageSegment.image("base64://" + image_base64)
            except FileNotFoundError:
                logger.warning(f"图片 {file.absolute()} 缺失...")
        logger.warning(f"图片 {file.absolute()}缺失...")

别忘了最上面加上import base64 或者你懒直接粘贴我的image方法

def image(
    file: Optional[Union[str, Path, bytes, BuildImage, io.BytesIO]] = None,
    b64: Optional[str] = None,
) -> MessageSegment:
    """
    说明:
        生成一个 MessageSegment.image 消息
        生成顺序:绝对路径(abspath) > base64(b64) > img_name
    参数:
        :param file: 图片文件
        :param b64: 图片base64(兼容旧方法)
    """
    if b64:
        file = b64 if b64.startswith("base64://") else ("base64://" + b64)
    if isinstance(file, str):
        if file.startswith(("http", "base64://")):
            return MessageSegment.image(file)
        else:
            return MessageSegment.image(IMAGE_PATH / file)
    if isinstance(file, Path):
        if file.exists():
            # return MessageSegment.image(file)
            try:
                with open(file.absolute(), 'rb') as file:
                    # 读取图片文件并进行 Base64 编码
                    image_data = base64.b64encode(file.read())
                    image_base64 = image_data.decode('utf-8')
                    # print(image_base64)
                    return MessageSegment.image("base64://" + image_base64)
            except FileNotFoundError:
                logger.warning(f"图片 {file.absolute()} 缺失...")
        logger.warning(f"图片 {file.absolute()}缺失...")
    if isinstance(file, (bytes, io.BytesIO)):
        return MessageSegment.image(file)
    if isinstance(file, BuildImage):
        return MessageSegment.image(file.pic2bs4())
    return MessageSegment.image("")

nice兄弟

@sagirisense
Copy link

修改\utils\message_builder.py,修改` if isinstance(file, Path):这个判断下的逻辑,

    if isinstance(file, Path):
        if file.exists():
            # return MessageSegment.image(file)
            try:
                with open(file.absolute(), 'rb') as file:
                    # 读取图片文件并进行 Base64 编码
                    image_data = base64.b64encode(file.read())
                    image_base64 = image_data.decode('utf-8')
                    # print(image_base64)
                    return MessageSegment.image("base64://" + image_base64)
            except FileNotFoundError:
                logger.warning(f"图片 {file.absolute()} 缺失...")
        logger.warning(f"图片 {file.absolute()}缺失...")

别忘了最上面加上import base64 或者你懒得直接粘贴我的图片方法

def image(
    file: Optional[Union[str, Path, bytes, BuildImage, io.BytesIO]] = None,
    b64: Optional[str] = None,
) -> MessageSegment:
    """
    说明:
        生成一个 MessageSegment.image 消息
        生成顺序:绝对路径(abspath) > base64(b64) > img_name
    参数:
        :param file: 图片文件
        :param b64: 图片base64(兼容旧方法)
    """
    if b64:
        file = b64 if b64.startswith("base64://") else ("base64://" + b64)
    if isinstance(file, str):
        if file.startswith(("http", "base64://")):
            return MessageSegment.image(file)
        else:
            return MessageSegment.image(IMAGE_PATH / file)
    if isinstance(file, Path):
        if file.exists():
            # return MessageSegment.image(file)
            try:
                with open(file.absolute(), 'rb') as file:
                    # 读取图片文件并进行 Base64 编码
                    image_data = base64.b64encode(file.read())
                    image_base64 = image_data.decode('utf-8')
                    # print(image_base64)
                    return MessageSegment.image("base64://" + image_base64)
            except FileNotFoundError:
                logger.warning(f"图片 {file.absolute()} 缺失...")
        logger.warning(f"图片 {file.absolute()}缺失...")
    if isinstance(file, (bytes, io.BytesIO)):
        return MessageSegment.image(file)
    if isinstance(file, BuildImage):
        return MessageSegment.image(file.pic2bs4())
    return MessageSegment.image("")

太牛了

@xingyu42
Copy link

修改\utils\message_builder.py,修改` if isinstance(file, Path):这个判断下的逻辑,

    if isinstance(file, Path):
        if file.exists():
            # return MessageSegment.image(file)
            try:
                with open(file.absolute(), 'rb') as file:
                    # 读取图片文件并进行 Base64 编码
                    image_data = base64.b64encode(file.read())
                    image_base64 = image_data.decode('utf-8')
                    # print(image_base64)
                    return MessageSegment.image("base64://" + image_base64)
            except FileNotFoundError:
                logger.warning(f"图片 {file.absolute()} 缺失...")
        logger.warning(f"图片 {file.absolute()}缺失...")

别忘了最上面加上import base64 或者你懒直接粘贴我的image方法

def image(
    file: Optional[Union[str, Path, bytes, BuildImage, io.BytesIO]] = None,
    b64: Optional[str] = None,
) -> MessageSegment:
    """
    说明:
        生成一个 MessageSegment.image 消息
        生成顺序:绝对路径(abspath) > base64(b64) > img_name
    参数:
        :param file: 图片文件
        :param b64: 图片base64(兼容旧方法)
    """
    if b64:
        file = b64 if b64.startswith("base64://") else ("base64://" + b64)
    if isinstance(file, str):
        if file.startswith(("http", "base64://")):
            return MessageSegment.image(file)
        else:
            return MessageSegment.image(IMAGE_PATH / file)
    if isinstance(file, Path):
        if file.exists():
            # return MessageSegment.image(file)
            try:
                with open(file.absolute(), 'rb') as file:
                    # 读取图片文件并进行 Base64 编码
                    image_data = base64.b64encode(file.read())
                    image_base64 = image_data.decode('utf-8')
                    # print(image_base64)
                    return MessageSegment.image("base64://" + image_base64)
            except FileNotFoundError:
                logger.warning(f"图片 {file.absolute()} 缺失...")
        logger.warning(f"图片 {file.absolute()}缺失...")
    if isinstance(file, (bytes, io.BytesIO)):
        return MessageSegment.image(file)
    if isinstance(file, BuildImage):
        return MessageSegment.image(file.pic2bs4())
    return MessageSegment.image("")

完美解决了问题

@xingyu42
Copy link

修改\utils\message_builder.py,修改` if isinstance(file, Path):这个判断下的逻辑,

    if isinstance(file, Path):
        if file.exists():
            # return MessageSegment.image(file)
            try:
                with open(file.absolute(), 'rb') as file:
                    # 读取图片文件并进行 Base64 编码
                    image_data = base64.b64encode(file.read())
                    image_base64 = image_data.decode('utf-8')
                    # print(image_base64)
                    return MessageSegment.image("base64://" + image_base64)
            except FileNotFoundError:
                logger.warning(f"图片 {file.absolute()} 缺失...")
        logger.warning(f"图片 {file.absolute()}缺失...")

别忘了最上面加上import base64 或者你懒直接粘贴我的image方法

def image(
    file: Optional[Union[str, Path, bytes, BuildImage, io.BytesIO]] = None,
    b64: Optional[str] = None,
) -> MessageSegment:
    """
    说明:
        生成一个 MessageSegment.image 消息
        生成顺序:绝对路径(abspath) > base64(b64) > img_name
    参数:
        :param file: 图片文件
        :param b64: 图片base64(兼容旧方法)
    """
    if b64:
        file = b64 if b64.startswith("base64://") else ("base64://" + b64)
    if isinstance(file, str):
        if file.startswith(("http", "base64://")):
            return MessageSegment.image(file)
        else:
            return MessageSegment.image(IMAGE_PATH / file)
    if isinstance(file, Path):
        if file.exists():
            # return MessageSegment.image(file)
            try:
                with open(file.absolute(), 'rb') as file:
                    # 读取图片文件并进行 Base64 编码
                    image_data = base64.b64encode(file.read())
                    image_base64 = image_data.decode('utf-8')
                    # print(image_base64)
                    return MessageSegment.image("base64://" + image_base64)
            except FileNotFoundError:
                logger.warning(f"图片 {file.absolute()} 缺失...")
        logger.warning(f"图片 {file.absolute()}缺失...")
    if isinstance(file, (bytes, io.BytesIO)):
        return MessageSegment.image(file)
    if isinstance(file, BuildImage):
        return MessageSegment.image(file.pic2bs4())
    return MessageSegment.image("")

高兴太早了,其他都可以了就剩下帮助怎么都不行,最后只能直接挂载目录了

@YuS1aN
Copy link
Contributor

YuS1aN commented Apr 10, 2024

修改\utils\message_builder.py,修改` if isinstance(file, Path):这个判断下的逻辑,

    if isinstance(file, Path):
        if file.exists():
            # return MessageSegment.image(file)
            try:
                with open(file.absolute(), 'rb') as file:
                    # 读取图片文件并进行 Base64 编码
                    image_data = base64.b64encode(file.read())
                    image_base64 = image_data.decode('utf-8')
                    # print(image_base64)
                    return MessageSegment.image("base64://" + image_base64)
            except FileNotFoundError:
                logger.warning(f"图片 {file.absolute()} 缺失...")
        logger.warning(f"图片 {file.absolute()}缺失...")

别忘了最上面加上import base64 或者你懒直接粘贴我的image方法

def image(
    file: Optional[Union[str, Path, bytes, BuildImage, io.BytesIO]] = None,
    b64: Optional[str] = None,
) -> MessageSegment:
    """
    说明:
        生成一个 MessageSegment.image 消息
        生成顺序:绝对路径(abspath) > base64(b64) > img_name
    参数:
        :param file: 图片文件
        :param b64: 图片base64(兼容旧方法)
    """
    if b64:
        file = b64 if b64.startswith("base64://") else ("base64://" + b64)
    if isinstance(file, str):
        if file.startswith(("http", "base64://")):
            return MessageSegment.image(file)
        else:
            return MessageSegment.image(IMAGE_PATH / file)
    if isinstance(file, Path):
        if file.exists():
            # return MessageSegment.image(file)
            try:
                with open(file.absolute(), 'rb') as file:
                    # 读取图片文件并进行 Base64 编码
                    image_data = base64.b64encode(file.read())
                    image_base64 = image_data.decode('utf-8')
                    # print(image_base64)
                    return MessageSegment.image("base64://" + image_base64)
            except FileNotFoundError:
                logger.warning(f"图片 {file.absolute()} 缺失...")
        logger.warning(f"图片 {file.absolute()}缺失...")
    if isinstance(file, (bytes, io.BytesIO)):
        return MessageSegment.image(file)
    if isinstance(file, BuildImage):
        return MessageSegment.image(file.pic2bs4())
    return MessageSegment.image("")

高兴太早了,其他都可以了就剩下帮助怎么都不行,最后只能直接挂载目录了

把上面返回MessageSegment那行改成把file类型转换为Path就行了

            # return MessageSegment.image(IMAGE_PATH / file)
            file = Path(IMAGE_PATH / file)

@xingyu42
Copy link

xingyu42 commented Apr 10, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants