From 345142454422200429a9131f1b0c8c6d0d69d8fe Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Wed, 5 Jun 2024 00:03:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=A0=E4=B9=8E=E5=AE=8C=E6=B4=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ica-rs/ica_typing.py | 14 +++++- ica-rs/plugins/base.py | 9 ++++ ica-rs/src/data_struct/tailchat/messages.rs | 17 +++++++- ica-rs/src/ica/events.rs | 48 +++++++++------------ ica-rs/src/main.rs | 2 +- ica-rs/src/py/class/ica.rs | 2 +- ica-rs/src/py/class/tailchat.rs | 9 ++-- ica-rs/src/tailchat/events.rs | 14 +++++- 8 files changed, 79 insertions(+), 36 deletions(-) diff --git a/ica-rs/ica_typing.py b/ica-rs/ica_typing.py index a772881..b9998ae 100644 --- a/ica-rs/ica_typing.py +++ b/ica-rs/ica_typing.py @@ -200,6 +200,12 @@ class TailchatReciveMessage: @property def converse_id(self) -> TailchatType.ConverseId: ... + def reply_with(self, message: str) -> "TailchatSendingMessage": + """回复这条消息""" + ... + def as_reply(self, message: str) -> "TailchatSendingMessage": + """回复这条消息""" + ... class TailchatSendingMessage: @@ -237,7 +243,13 @@ class TailchatClient: """发送消息, 并在日志中输出警告信息""" self.warn(message.content) return self.send_message(message) - + @property + def version(self) -> str: + ... + @property + def tailchat_version(self) -> str: + """tailchat 的版本号""" + ... def debug(self, message: str) -> None: """向日志中输出调试信息""" def info(self, message: str) -> None: diff --git a/ica-rs/plugins/base.py b/ica-rs/plugins/base.py index efbce69..6cf644b 100644 --- a/ica-rs/plugins/base.py +++ b/ica-rs/plugins/base.py @@ -2,9 +2,12 @@ from typing import TYPE_CHECKING, TypeVar if TYPE_CHECKING: from ica_typing import IcaNewMessage, IcaClient + from ica_typing import TailchatReciveMessage, TailchatClient else: IcaNewMessage = TypeVar("NewMessage") IcaClient = TypeVar("IcaClient") + TailchatReciveMessage = TypeVar("TailchatReciveMessage") + TailchatClient = TypeVar("TailchatClient") def on_ica_message(msg: IcaNewMessage, client: IcaClient) -> None: if not (msg.is_from_self or msg.is_reply): @@ -13,3 +16,9 @@ def on_ica_message(msg: IcaNewMessage, client: IcaClient) -> None: 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 msg.content == "/bot": + reply = msg.reply_with(f"ica-async-rs({client.version})-sync-py {client.tailchat_version}") + client.send_message(reply) diff --git a/ica-rs/src/data_struct/tailchat/messages.rs b/ica-rs/src/data_struct/tailchat/messages.rs index d35f782..bb32935 100644 --- a/ica-rs/src/data_struct/tailchat/messages.rs +++ b/ica-rs/src/data_struct/tailchat/messages.rs @@ -1,3 +1,5 @@ +use std::fmt::Display; + use serde::{Deserialize, Serialize}; use serde_json::{json, Value as JsonValue}; @@ -52,9 +54,9 @@ impl ReciveMessage { } /// 回复这条消息 - pub fn reply_with(&self, content: String) -> SendingMessage { + pub fn reply_with(&self, content: &String) -> SendingMessage { SendingMessage::new( - content, + content.clone(), self.converse_id.clone(), self.group_id.clone(), Some(ReplyMeta::from_recive_message(self)), @@ -62,6 +64,17 @@ impl ReciveMessage { } } +impl Display for ReciveMessage { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + // msgid|groupid-converseid|senderid|content + write!( + f, + "{}|{}-{}|{}|{}", + self.msg_id, self.group_id, self.converse_id, self.sender_id, self.content + ) + } +} + #[derive(Debug, Clone, Serialize)] /// 将要发送的消息 /// diff --git a/ica-rs/src/ica/events.rs b/ica-rs/src/ica/events.rs index 6c2e8fb..5b8aaba 100644 --- a/ica-rs/src/ica/events.rs +++ b/ica-rs/src/ica/events.rs @@ -133,16 +133,13 @@ pub async fn any_event(event: Event, payload: Payload, _client: Client) { } } Event::Message => { - match payload { - Payload::Text(values) => { - if let Some(value) = values.first() { - if handled.contains(&value.as_str().unwrap()) { - return; - } - info!("收到消息 {}", value.to_string().yellow()); + if let Payload::Text(values) = payload { + if let Some(value) = values.first() { + if handled.contains(&value.as_str().unwrap()) { + return; } + info!("收到消息 {}", value.to_string().yellow()); } - _ => (), } return; } @@ -165,27 +162,24 @@ pub async fn any_event(event: Event, payload: Payload, _client: Client) { pub async fn connect_callback(payload: Payload, _client: Client) { let span = span!(Level::INFO, "ica connect_callback"); let _enter = span.enter(); - match payload { - Payload::Text(values) => { - if let Some(value) = values.first() { - match value.as_str() { - Some("authSucceed") => { - event!(Level::INFO, "{}", "已经登录到 icalingua!".green()) - } - Some("authFailed") => { - event!(Level::ERROR, "{}", "登录到 icalingua 失败!".red()); - panic!("登录失败") - } - Some("authRequired") => { - event!(Level::INFO, "{}", "需要登录到 icalingua!".yellow()) - } - Some(msg) => { - event!(Level::INFO, "{}{}", "未知消息".yellow(), msg); - } - None => (), + if let Payload::Text(values) = payload { + if let Some(value) = values.first() { + match value.as_str() { + Some("authSucceed") => { + event!(Level::INFO, "{}", "已经登录到 icalingua!".green()) } + Some("authFailed") => { + event!(Level::ERROR, "{}", "登录到 icalingua 失败!".red()); + panic!("登录失败") + } + Some("authRequired") => { + event!(Level::INFO, "{}", "需要登录到 icalingua!".yellow()) + } + Some(msg) => { + event!(Level::INFO, "{}{}", "未知消息".yellow(), msg); + } + None => (), } } - _ => (), } } diff --git a/ica-rs/src/main.rs b/ica-rs/src/main.rs index b91711e..fc1308b 100644 --- a/ica-rs/src/main.rs +++ b/ica-rs/src/main.rs @@ -26,7 +26,7 @@ pub type StopGetter = tokio::sync::oneshot::Receiver<()>; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub const ICA_VERSION: &str = "1.5.0"; -pub const TAILCHAT_VERSION: &str = "0.2.0"; +pub const TAILCHAT_VERSION: &str = "1.0.0"; #[macro_export] macro_rules! wrap_callback { diff --git a/ica-rs/src/py/class/ica.rs b/ica-rs/src/py/class/ica.rs index 0df2b8c..32c70c2 100644 --- a/ica-rs/src/py/class/ica.rs +++ b/ica-rs/src/py/class/ica.rs @@ -99,7 +99,7 @@ impl NewMessagePy { #[getter] pub fn get_is_chat_msg(&self) -> bool { self.msg.room_id.is_chat() } #[getter] - pub fn get_room_id(&self) -> RoomId { self.msg.room_id.clone() } + pub fn get_room_id(&self) -> RoomId { self.msg.room_id } } impl NewMessagePy { diff --git a/ica-rs/src/py/class/tailchat.rs b/ica-rs/src/py/class/tailchat.rs index 1e85511..fb4f355 100644 --- a/ica-rs/src/py/class/tailchat.rs +++ b/ica-rs/src/py/class/tailchat.rs @@ -3,7 +3,7 @@ use pyo3::prelude::*; use rust_socketio::asynchronous::Client; use tracing::{debug, info, warn}; -use crate::data_struct::tailchat::messages::{ReciveMessage, ReplyMeta, SendingMessage}; +use crate::data_struct::tailchat::messages::{ReciveMessage, SendingMessage}; use crate::data_struct::tailchat::{ConverseId, GroupId, MessageId, UserId}; use crate::tailchat::client::send_message; @@ -60,7 +60,10 @@ impl TailchatClientPy { warn!("{}", message.message.content); self.send_message(message) } - + #[getter] + pub fn get_version(&self) -> String { crate::VERSION.to_string() } + #[getter] + pub fn get_tailchat_version(&self) -> String { crate::TAILCHAT_VERSION.to_string() } pub fn debug(&self, content: String) { debug!("{}", content); } @@ -94,7 +97,7 @@ impl TailchatReciveMessagePy { } pub fn reply_with(&self, content: String) -> TailchatSendingMessagePy { TailchatSendingMessagePy { - message: self.message.reply_with(content), + message: self.message.reply_with(&content), } } } diff --git a/ica-rs/src/tailchat/events.rs b/ica-rs/src/tailchat/events.rs index 51b00e6..b70400b 100644 --- a/ica-rs/src/tailchat/events.rs +++ b/ica-rs/src/tailchat/events.rs @@ -4,6 +4,7 @@ use rust_socketio::{Event, Payload}; use tracing::info; use crate::data_struct::tailchat::messages::ReciveMessage; +use crate::tailchat::client::send_message; /// 所有 pub async fn any_event(event: Event, payload: Payload, _client: Client) { @@ -60,7 +61,18 @@ pub async fn on_message(payload: Payload, client: Client) { if let Payload::Text(values) = payload { if let Some(value) = values.first() { let message: ReciveMessage = serde_json::from_value(value.clone()).unwrap(); - info!("收到消息 {:?}", message); + info!("tailchat_msg {}", message.to_string().cyan()); + + if !message.is_reply() { + if message.content == "/bot-rs" { + let reply = message.reply_with(&format!( + "shenbot v{}\ntailchat-async-rs pong v{}", + crate::VERSION, + crate::TAILCHAT_VERSION + )); + send_message(&client, &reply).await; + } + } crate::py::call::tailchat_new_message_py(&message, &client).await; } }