Compare commits

...

3 Commits

Author SHA1 Message Date
c65a2229cc
喜报, img API 2024-05-12 19:43:03 +08:00
1eba64bf9e
感觉还行 2024-05-12 18:54:53 +08:00
99082ccfd7
先bump版本,待会再研究 2024-05-12 17:37:03 +08:00
9 changed files with 88 additions and 14 deletions

3
Cargo.lock generated
View File

@ -653,9 +653,10 @@ dependencies = [
[[package]]
name = "ica-rs"
version = "0.6.0-dev"
version = "0.6.2"
dependencies = [
"anyhow",
"base64 0.22.1",
"chrono",
"colored",
"ed25519",

View File

@ -1,24 +1,31 @@
[package]
name = "ica-rs"
version = "0.6.0-dev"
version = "0.6.2"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["ica", "tailchat"]
ica = ["dep:ed25519", "dep:ed25519-dalek", "dep:hex", "dep:rust_socketio"]
ica = [
"dep:ed25519",
"dep:ed25519-dalek",
"dep:hex",
"dep:rust_socketio",
"dep:base64",
]
tailchat = ["dep:rust_socketio", "dep:md-5", "dep:reqwest"]
[dependencies]
# ica
base64 = { version = "0.22", optional = true }
ed25519 = { version = "2.2", optional = true }
ed25519-dalek = { version = "2.1", optional = true }
hex = { version = "0.4", optional = true }
# tailchat
reqwest = { version = "0.12.2", optional = true }
reqwest = { version = "0.12.4", optional = true }
md-5 = { version = "0.10.6", optional = true }
# ica & tailchat (socketio)

View File

@ -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<u8>)
@param file_type: 图片类型 (MIME) (image/png; image/jpeg)
@param as_sticker: 是否作为贴纸发送
"""
class IcaDeleteMessage:

View File

@ -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)

View File

@ -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<ReplyMessage>,
/// @ 谁
#[serde(rename = "at")]
pub at: JsonValue,
/// base64 的图片
#[serde(rename = "b64img")]
file_data: Option<String>,
/// 是否当作表情发送
///
/// 默认 false
pub sticker: bool,
}
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 {
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<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)]

View File

@ -10,7 +10,7 @@ use crate::config::IcaConfig;
use crate::error::{ClientResult, IcaError};
use crate::{wrap_any_callback, wrap_callback, StopGetter};
const ICA_PROTOCOL_VERSION: &str = "2.11.9";
const ICA_PROTOCOL_VERSION: &str = "2.12.0";
pub async fn start_ica(config: &IcaConfig, stop_reciver: StopGetter) -> ClientResult<(), IcaError> {
let span = span!(Level::INFO, "Icalingua Client");
@ -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;

View File

@ -33,12 +33,14 @@ pub async fn add_message(payload: Payload, client: Client) {
info!("add_message {}", message.to_string().cyan());
// 就在这里处理掉最基本的消息
// 之后的处理交给插件
if message.content().eq("/bot-rs") && !message.is_from_self() && !message.is_reply() {
let reply = message.reply_with(&format!(
"shenbot v{}\nica-async-rs pong v{}",
VERSION, ICA_VERSION
));
send_message(&client, &reply).await;
if !message.is_from_self() && !message.is_reply() {
if message.content() == "/bot-rs" {
let reply = message.reply_with(&format!(
"shenbot v{}\nica-async-rs pong v{}",
VERSION, ICA_VERSION
));
send_message(&client, &reply).await;
}
}
// python 插件
py::call::ica_new_message_py(&message, &client).await;

View File

@ -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<u8>, file_type: String, as_sticker: bool) {
self.msg.set_img(&file, &file_type, as_sticker);
}
}
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
- 去除了 matrix 的支持