From 13995d29155335ef02e201fdac37792b94281d26 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 29 Jun 2024 00:56:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=93=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ica-rs/plugins/base.py | 54 ++++++++++++++++++++++++++--------- ica-rs/src/tailchat/client.rs | 3 +- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/ica-rs/plugins/base.py b/ica-rs/plugins/base.py index 465e521..df41989 100644 --- a/ica-rs/plugins/base.py +++ b/ica-rs/plugins/base.py @@ -1,7 +1,8 @@ -from typing import TYPE_CHECKING, TypeVar -import platform -import PIL.Image import io +import psutil +import platform +from typing import TYPE_CHECKING, TypeVar +from PIL import (Image, ImageDraw, ImageFont) if TYPE_CHECKING: from ica_typing import IcaNewMessage, IcaClient @@ -12,25 +13,52 @@ else: TailchatReciveMessage = TypeVar("TailchatReciveMessage") TailchatClient = TypeVar("TailchatClient") + +# 生成一张本地信息图 +def local_env_info() -> str: + cache = io.StringIO() + # 参考 DR 的 (crash report) + cache.write(f"系统: {platform.platform()}\n") + # 处理器 + cache.write(f"{"|".join([f"{x}%" for x in psutil.cpu_percent(interval=1, percpu=True)])}\n") + # Python 版本信息 + cache.write(f"{platform.python_implementation()}: {platform.python_version()}-{platform.python_branch()}({platform.python_compiler()})\n") + # 内存信息 + memory = psutil.virtual_memory() + cache.write(f"内存: {memory.free / 1024 / 1024 / 1024:.3f}GB/{memory.total / 1024 / 1024 / 1024:.3f}GB\n") + return cache.getvalue() + +def local_env_image() -> bytes: + img = Image.new("RGB", (800, 120), (255, 255, 255)) + # 往图片上写入一些信息 + draw = ImageDraw.Draw(img) + font = ImageFont.truetype("simkai.ttf", size=25) + draw.text((10, 10), local_env_info(), fill=(0, 0, 0), font=font) + img_cache = io.BytesIO() + img.save(img_cache, format="PNG") + raw_img = img_cache.getvalue() + img_cache.close() + return raw_img + def on_ica_message(msg: IcaNewMessage, client: IcaClient) -> None: if not (msg.is_from_self or msg.is_reply): if msg.content == "/bot": reply = msg.reply_with(f"ica-async-rs({client.version})-sync-py {client.ica_version}") client.send_message(reply) + elif msg.content == "/bot-sys": + datas = local_env_info() + reply = msg.reply_with(datas) + reply.set_img(local_env_image(), "image/png", False) + client.send_message(reply) def on_tailchat_message(msg: TailchatReciveMessage, client: TailchatClient) -> None: - # if not (msg.is_from_self or msg.is_reply): - if not (msg.is_reply): + if not (msg.is_reply or msg.is_from_self): if msg.content == "/bot": reply = msg.reply_with(f"tailchat-async-rs({client.version})-sync-py {client.tailchat_version}") client.send_message(reply) - elif msg.content == "/image": - image = PIL.Image.new("RGB", (100, 100), (255, 255, 255)) - img_cache = io.BytesIO() - image.save(img_cache, format="JPEG") - raw_img = img_cache.getvalue() - img_cache.close() - reply = msg.reply_with("Here is an image") - reply.set_img(raw_img, "just_img.png") + elif msg.content == "/bot-sys": + datas = local_env_info() + reply = msg.reply_with(datas) + reply.set_img(local_env_image(), "just_img.png") client.send_message(reply) diff --git a/ica-rs/src/tailchat/client.rs b/ica-rs/src/tailchat/client.rs index a53eb8a..8fb37de 100644 --- a/ica-rs/src/tailchat/client.rs +++ b/ica-rs/src/tailchat/client.rs @@ -71,9 +71,8 @@ pub async fn send_message(client: &Client, message: &SendingMessage) -> bool { return false; } }; - event!(Level::INFO, "file upload success with data:{}", format!("{:#?}", data).cyan()); let content = format!( - "{}\n{}", + "{}{}", message.content, message.file.gen_markdown(data["url"].as_str().unwrap()) );