几乎完活

This commit is contained in:
shenjack 2024-06-05 00:03:19 +08:00
parent ff255426f6
commit 3451424544
Signed by: shenjack
GPG Key ID: 7B1134A979775551
8 changed files with 79 additions and 36 deletions

View File

@ -200,6 +200,12 @@ class TailchatReciveMessage:
@property @property
def converse_id(self) -> TailchatType.ConverseId: def converse_id(self) -> TailchatType.ConverseId:
... ...
def reply_with(self, message: str) -> "TailchatSendingMessage":
"""回复这条消息"""
...
def as_reply(self, message: str) -> "TailchatSendingMessage":
"""回复这条消息"""
...
class TailchatSendingMessage: class TailchatSendingMessage:
@ -237,7 +243,13 @@ class TailchatClient:
"""发送消息, 并在日志中输出警告信息""" """发送消息, 并在日志中输出警告信息"""
self.warn(message.content) self.warn(message.content)
return self.send_message(message) return self.send_message(message)
@property
def version(self) -> str:
...
@property
def tailchat_version(self) -> str:
"""tailchat 的版本号"""
...
def debug(self, message: str) -> None: def debug(self, message: str) -> None:
"""向日志中输出调试信息""" """向日志中输出调试信息"""
def info(self, message: str) -> None: def info(self, message: str) -> None:

View File

@ -2,9 +2,12 @@ from typing import TYPE_CHECKING, TypeVar
if TYPE_CHECKING: if TYPE_CHECKING:
from ica_typing import IcaNewMessage, IcaClient from ica_typing import IcaNewMessage, IcaClient
from ica_typing import TailchatReciveMessage, TailchatClient
else: else:
IcaNewMessage = TypeVar("NewMessage") IcaNewMessage = TypeVar("NewMessage")
IcaClient = TypeVar("IcaClient") IcaClient = TypeVar("IcaClient")
TailchatReciveMessage = TypeVar("TailchatReciveMessage")
TailchatClient = TypeVar("TailchatClient")
def on_ica_message(msg: IcaNewMessage, client: IcaClient) -> None: def on_ica_message(msg: IcaNewMessage, client: IcaClient) -> None:
if not (msg.is_from_self or msg.is_reply): 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) 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)

View File

@ -1,3 +1,5 @@
use std::fmt::Display;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::{json, Value as JsonValue}; 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( SendingMessage::new(
content, content.clone(),
self.converse_id.clone(), self.converse_id.clone(),
self.group_id.clone(), self.group_id.clone(),
Some(ReplyMeta::from_recive_message(self)), 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)] #[derive(Debug, Clone, Serialize)]
/// 将要发送的消息 /// 将要发送的消息
/// ///

View File

@ -133,16 +133,13 @@ pub async fn any_event(event: Event, payload: Payload, _client: Client) {
} }
} }
Event::Message => { Event::Message => {
match payload { if let Payload::Text(values) = payload {
Payload::Text(values) => { if let Some(value) = values.first() {
if let Some(value) = values.first() { if handled.contains(&value.as_str().unwrap()) {
if handled.contains(&value.as_str().unwrap()) { return;
return;
}
info!("收到消息 {}", value.to_string().yellow());
} }
info!("收到消息 {}", value.to_string().yellow());
} }
_ => (),
} }
return; 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) { pub async fn connect_callback(payload: Payload, _client: Client) {
let span = span!(Level::INFO, "ica connect_callback"); let span = span!(Level::INFO, "ica connect_callback");
let _enter = span.enter(); let _enter = span.enter();
match payload { if let Payload::Text(values) = payload {
Payload::Text(values) => { if let Some(value) = values.first() {
if let Some(value) = values.first() { match value.as_str() {
match value.as_str() { Some("authSucceed") => {
Some("authSucceed") => { event!(Level::INFO, "{}", "已经登录到 icalingua!".green())
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 => (),
} }
Some("authFailed") => {
event!(Level::ERROR, "{}", "登录到 icalingua 失败!".red());
panic!("登录失败")
}
Some("authRequired") => {
event!(Level::INFO, "{}", "需要登录到 icalingua!".yellow())
}
Some(msg) => {
event!(Level::INFO, "{}{}", "未知消息".yellow(), msg);
}
None => (),
} }
} }
_ => (),
} }
} }

View File

@ -26,7 +26,7 @@ pub type StopGetter = tokio::sync::oneshot::Receiver<()>;
pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const ICA_VERSION: &str = "1.5.0"; 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_export]
macro_rules! wrap_callback { macro_rules! wrap_callback {

View File

@ -99,7 +99,7 @@ impl NewMessagePy {
#[getter] #[getter]
pub fn get_is_chat_msg(&self) -> bool { self.msg.room_id.is_chat() } pub fn get_is_chat_msg(&self) -> bool { self.msg.room_id.is_chat() }
#[getter] #[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 { impl NewMessagePy {

View File

@ -3,7 +3,7 @@ use pyo3::prelude::*;
use rust_socketio::asynchronous::Client; use rust_socketio::asynchronous::Client;
use tracing::{debug, info, warn}; 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::data_struct::tailchat::{ConverseId, GroupId, MessageId, UserId};
use crate::tailchat::client::send_message; use crate::tailchat::client::send_message;
@ -60,7 +60,10 @@ impl TailchatClientPy {
warn!("{}", message.message.content); warn!("{}", message.message.content);
self.send_message(message) 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) { pub fn debug(&self, content: String) {
debug!("{}", content); debug!("{}", content);
} }
@ -94,7 +97,7 @@ impl TailchatReciveMessagePy {
} }
pub fn reply_with(&self, content: String) -> TailchatSendingMessagePy { pub fn reply_with(&self, content: String) -> TailchatSendingMessagePy {
TailchatSendingMessagePy { TailchatSendingMessagePy {
message: self.message.reply_with(content), message: self.message.reply_with(&content),
} }
} }
} }

View File

@ -4,6 +4,7 @@ use rust_socketio::{Event, Payload};
use tracing::info; use tracing::info;
use crate::data_struct::tailchat::messages::ReciveMessage; use crate::data_struct::tailchat::messages::ReciveMessage;
use crate::tailchat::client::send_message;
/// 所有 /// 所有
pub async fn any_event(event: Event, payload: Payload, _client: Client) { 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 Payload::Text(values) = payload {
if let Some(value) = values.first() { if let Some(value) = values.first() {
let message: ReciveMessage = serde_json::from_value(value.clone()).unwrap(); 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; crate::py::call::tailchat_new_message_py(&message, &client).await;
} }
} }