喜报, img API

This commit is contained in:
shenjack 2024-05-12 19:43:03 +08:00
parent 1eba64bf9e
commit c65a2229cc
Signed by: shenjack
GPG Key ID: 7B1134A979775551
8 changed files with 69 additions and 5 deletions

2
Cargo.lock generated
View File

@ -653,7 +653,7 @@ dependencies = [
[[package]] [[package]]
name = "ica-rs" name = "ica-rs"
version = "0.6.1" version = "0.6.2"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"base64 0.22.1", "base64 0.22.1",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "ica-rs" name = "ica-rs"
version = "0.6.1" version = "0.6.2"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -2,7 +2,6 @@
from typing import Callable, Tuple from typing import Callable, Tuple
""" """
ica.rs ica.rs
pub type RoomId = i64; pub type RoomId = i64;
@ -78,6 +77,13 @@ class IcaSendMessage:
""" """
self.content = content self.content = content
return self return self
def set_img(self, file: bytes, file_type: str, as_sticker: bool):
"""
设置消息的图片
@param file: 图片文件 (实际上是 vec<u8>)
@param file_type: 图片类型 (MIME) (image/png; image/jpeg)
@param as_sticker: 是否作为贴纸发送
"""
class IcaDeleteMessage: class IcaDeleteMessage:

View File

@ -4,6 +4,8 @@ import time
import requests import requests
import traceback import traceback
# import PIL
from typing import TYPE_CHECKING, TypeVar, Optional, Tuple from typing import TYPE_CHECKING, TypeVar, Optional, Tuple
if TYPE_CHECKING: if TYPE_CHECKING:
@ -198,9 +200,17 @@ def bmcl_rank_general(msg, client):
# 显示前3名 # 显示前3名
ranks = rank_data[:3] ranks = rank_data[:3]
# ranks = rank_data # 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) report_msg = display_rank_full(ranks, req_time)
client.info(report_msg) client.info(report_msg)
reply = msg.reply_with(display_rank_full(ranks, req_time)) reply = msg.reply_with(display_rank_full(ranks, req_time))
# reply.set_img(raw_img, "image/jpeg", False)
client.send_message(reply) client.send_message(reply)

View File

@ -275,7 +275,7 @@ impl NewMessage {
/// 创建一条对这条消息的回复 /// 创建一条对这条消息的回复
pub fn reply_with(&self, content: &String) -> SendMessage { 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)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SendMessage { pub struct SendMessage {
/// 就是消息内容
pub content: String, pub content: String,
/// 发送的房间 id
#[serde(rename = "roomId")] #[serde(rename = "roomId")]
pub room_id: RoomId, pub room_id: RoomId,
/// 回复的消息
#[serde(rename = "replyMessage")] #[serde(rename = "replyMessage")]
pub reply_to: Option<ReplyMessage>, pub reply_to: Option<ReplyMessage>,
/// @ 谁
#[serde(rename = "at")] #[serde(rename = "at")]
pub at: JsonValue, pub at: JsonValue,
/// base64 的图片
#[serde(rename = "b64img")]
file_data: Option<String>,
/// 是否当作表情发送
///
/// 默认 false
pub sticker: bool,
} }
impl SendMessage { impl SendMessage {
pub fn new(content: String, room_id: RoomId, reply_to: Option<ReplyMessage>) -> Self { pub fn new(
content: String,
room_id: RoomId,
reply_to: Option<ReplyMessage>,
sticker: bool,
) -> Self {
Self { Self {
content, content,
room_id, room_id,
reply_to, reply_to,
at: json!([]), at: json!([]),
file_data: None,
sticker,
} }
} }
pub fn as_value(&self) -> JsonValue { serde_json::to_value(self).unwrap() } 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<u8>, 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)] #[derive(Debug, Clone, Serialize, Deserialize)]

View File

@ -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), format!("shenbot v {}\nica-async-rs v{}", crate::VERSION, crate::ICA_VERSION),
*room, *room,
None, None,
false,
); );
tokio::time::sleep(std::time::Duration::from_secs(1)).await; tokio::time::sleep(std::time::Duration::from_secs(1)).await;

View File

@ -141,6 +141,10 @@ impl SendMessagePy {
pub fn get_content(&self) -> String { self.msg.content.clone() } pub fn get_content(&self) -> String { self.msg.content.clone() }
#[setter] #[setter]
pub fn set_content(&mut self, content: String) { self.msg.content = content; } pub fn set_content(&mut self, content: String) { self.msg.content = content; }
/// 设置消息图片
pub fn set_img(&mut self, file: Vec<u8>, file_type: String, as_sticker: bool) {
self.msg.set_img(&file, &file_type, as_sticker);
}
} }
impl SendMessagePy { impl SendMessagePy {

13
news.md
View File

@ -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 ## 0.6.0-dev
- 去除了 matrix 的支持 - 去除了 matrix 的支持