zhenxunbot连接shamrock发送图片的问题
系统版本:Ubuntu 20.04
真寻版本:main分支最新commit
错误截图
日志截图
错误说明
zhenxunbot连接shamrock(可以理解为gocq的平替(?),连接成功,并且可以回应文本消息,在发送图片的时候,可能因为shamrock和真寻bot不在同一个机器上导致的报错:找不到一个png文件。我在电脑上看到实际存在这个png文件,shamrock找不到这个文件,可以有其他的方式来尝试上传文件吗?
用base64吧,不过要改源码
emmm,我用另一种方式解决了
就是,把Windows的smb挂载到了安卓手机的/mnt/c目录下,实现了shamrock直接通过路径访问Windows的文件,也可以访问,现在就是,图片会这样:
(我不会python语言,所以想的另一个方法
就是,把Windows的smb挂载到了安卓手机的/mnt/c目录下,实现了shamrock直接通过路径访问Windows的文件,也可以访问,现在就是,图片会这样:
Html 版的老问题,换回正常版显示正常
修改\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("")
修改\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兄弟
修改\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("")
太牛了
修改\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("")
完美解决了问题
修改\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("")
高兴太早了,其他都可以了就剩下帮助怎么都不行,最后只能直接挂载目录了
修改\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)
好的
---原始邮件--- 发件人: @.> 发送时间: 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: @.***>
