HibiKier / zhenxun_bot

基于 Nonebot2 开发,以 postgresql 作为数据库,非常可爱的绪山真寻bot
GNU Affero General Public License v3.0
3.3k stars 604 forks source link

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

Closed HuaWaterED closed 3 weeks ago

HuaWaterED commented 9 months ago

系统版本:Ubuntu 20.04

真寻版本:main分支最新commit

错误截图

QQ图片20231126132000

日志截图

Snipaste_2023-11-26_13-20-35

错误说明

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

PackageInstaller commented 9 months ago

用base64吧,不过要改源码

HuaWaterED commented 9 months ago

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

HuaWaterED commented 9 months ago

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

HuaWaterED commented 9 months ago

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

PackageInstaller commented 9 months ago

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

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

YingLing3 commented 9 months ago

修改\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 commented 7 months ago

修改\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 commented 7 months ago

修改\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 commented 5 months ago

修改\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 commented 5 months ago

修改\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 commented 4 months ago

修改\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 commented 4 months ago

好的

---原始邮件--- 发件人: @.> 发送时间: 2024年4月10日(周三) 下午5:05 收件人: @.>; 抄送: @.**@.>; 主题: Re: [HibiKier/zhenxun_bot] zhenxunbot连接shamrock发送图片的问题 (Issue #1501)

修改\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)

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>