From c65a2229cce21eeb3a1dc2b1f2a2b38cd58449b6 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sun, 12 May 2024 19:43:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=96=9C=E6=8A=A5,=20img=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 2 +- ica-rs/Cargo.toml | 2 +- ica-rs/ica_typing.py | 8 +++++- ica-rs/plugins/bmcl.py | 10 ++++++++ ica-rs/src/data_struct/ica/messages.rs | 34 ++++++++++++++++++++++++-- ica-rs/src/ica.rs | 1 + ica-rs/src/py/class/ica.rs | 4 +++ news.md | 13 ++++++++++ 8 files changed, 69 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 93e3db3..e179f32 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -653,7 +653,7 @@ dependencies = [ [[package]] name = "ica-rs" -version = "0.6.1" +version = "0.6.2" dependencies = [ "anyhow", "base64 0.22.1", diff --git a/ica-rs/Cargo.toml b/ica-rs/Cargo.toml index 98f22ab..dcb38bb 100644 --- a/ica-rs/Cargo.toml +++ b/ica-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ica-rs" -version = "0.6.1" +version = "0.6.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/ica-rs/ica_typing.py b/ica-rs/ica_typing.py index 8f08f45..f710feb 100644 --- a/ica-rs/ica_typing.py +++ b/ica-rs/ica_typing.py @@ -2,7 +2,6 @@ from typing import Callable, Tuple - """ ica.rs pub type RoomId = i64; @@ -78,6 +77,13 @@ class IcaSendMessage: """ self.content = content return self + def set_img(self, file: bytes, file_type: str, as_sticker: bool): + """ + 设置消息的图片 + @param file: 图片文件 (实际上是 vec) + @param file_type: 图片类型 (MIME) (image/png; image/jpeg) + @param as_sticker: 是否作为贴纸发送 + """ class IcaDeleteMessage: diff --git a/ica-rs/plugins/bmcl.py b/ica-rs/plugins/bmcl.py index 359318e..4d3f300 100644 --- a/ica-rs/plugins/bmcl.py +++ b/ica-rs/plugins/bmcl.py @@ -4,6 +4,8 @@ import time import requests import traceback +# import PIL + from typing import TYPE_CHECKING, TypeVar, Optional, Tuple if TYPE_CHECKING: @@ -198,9 +200,17 @@ def bmcl_rank_general(msg, client): # 显示前3名 ranks = rank_data[:3] # ranks = rank_data + + # 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() + report_msg = display_rank_full(ranks, req_time) client.info(report_msg) reply = msg.reply_with(display_rank_full(ranks, req_time)) + # reply.set_img(raw_img, "image/jpeg", False) client.send_message(reply) diff --git a/ica-rs/src/data_struct/ica/messages.rs b/ica-rs/src/data_struct/ica/messages.rs index 2207338..fc843c3 100644 --- a/ica-rs/src/data_struct/ica/messages.rs +++ b/ica-rs/src/data_struct/ica/messages.rs @@ -275,7 +275,7 @@ impl NewMessage { /// 创建一条对这条消息的回复 pub fn reply_with(&self, content: &String) -> SendMessage { - SendMessage::new(content.clone(), self.room_id, Some(self.msg.as_reply())) + SendMessage::new(content.clone(), self.room_id, Some(self.msg.as_reply()), false) } /// 作为被删除的消息 @@ -289,26 +289,56 @@ impl NewMessage { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct SendMessage { + /// 就是消息内容 pub content: String, + /// 发送的房间 id #[serde(rename = "roomId")] pub room_id: RoomId, + /// 回复的消息 #[serde(rename = "replyMessage")] pub reply_to: Option, + /// @ 谁 #[serde(rename = "at")] pub at: JsonValue, + /// base64 的图片 + #[serde(rename = "b64img")] + file_data: Option, + /// 是否当作表情发送 + /// + /// 默认 false + pub sticker: bool, } impl SendMessage { - pub fn new(content: String, room_id: RoomId, reply_to: Option) -> Self { + pub fn new( + content: String, + room_id: RoomId, + reply_to: Option, + sticker: bool, + ) -> Self { Self { content, room_id, reply_to, at: json!([]), + file_data: None, + sticker, } } pub fn as_value(&self) -> JsonValue { serde_json::to_value(self).unwrap() } + + /// 设置消息的图片 + /// + /// as_sticker: 是否当作表情发送 + /// file: 图片数据 + /// file_type: 图片类型(MIME) (image/png; image/jpeg) + pub fn set_img(&mut self, file: &Vec, file_type: &str, as_sticker: bool) { + self.sticker = as_sticker; + use base64::{engine::general_purpose, Engine as _}; + let base64_data = general_purpose::STANDARD.encode(file); + self.file_data = Some(format!("data:{};base64,{}", file_type, base64_data)); + } } #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/ica-rs/src/ica.rs b/ica-rs/src/ica.rs index 0847297..f01bbc7 100644 --- a/ica-rs/src/ica.rs +++ b/ica-rs/src/ica.rs @@ -51,6 +51,7 @@ pub async fn start_ica(config: &IcaConfig, stop_reciver: StopGetter) -> ClientRe format!("shenbot v {}\nica-async-rs v{}", crate::VERSION, crate::ICA_VERSION), *room, None, + false, ); tokio::time::sleep(std::time::Duration::from_secs(1)).await; diff --git a/ica-rs/src/py/class/ica.rs b/ica-rs/src/py/class/ica.rs index 0628c32..8477ac2 100644 --- a/ica-rs/src/py/class/ica.rs +++ b/ica-rs/src/py/class/ica.rs @@ -141,6 +141,10 @@ impl SendMessagePy { pub fn get_content(&self) -> String { self.msg.content.clone() } #[setter] pub fn set_content(&mut self, content: String) { self.msg.content = content; } + /// 设置消息图片 + pub fn set_img(&mut self, file: Vec, file_type: String, as_sticker: bool) { + self.msg.set_img(&file, &file_type, as_sticker); + } } impl SendMessagePy { diff --git a/news.md b/news.md index 3a69dc1..9ef1f01 100644 --- a/news.md +++ b/news.md @@ -1,5 +1,18 @@ # 更新日志 +## 0.6.2 + +- 添加 API + - `NewMessage.set_img` 用于设置消息的图片 + - `IcaSendMessage.set_img` 用于设置消息的图片 (python) + +## 0.6.1 + +还是没写完 tailchat 支持 +因为 rust_socketio 还是没写好 serdelizer 的支持 + +- 正在添加发送图片的 api + ## 0.6.0-dev - 去除了 matrix 的支持