socket-bot/ica-rs/src/tailchat/events.rs

113 lines
3.8 KiB
Rust
Raw Normal View History

2024-03-30 18:30:43 +08:00
use colored::Colorize;
use rust_socketio::asynchronous::Client;
use rust_socketio::{Event, Payload};
use tracing::info;
2024-03-30 14:24:19 +08:00
2024-06-04 00:22:36 +08:00
use crate::data_struct::tailchat::messages::ReciveMessage;
2024-06-10 22:08:37 +08:00
use crate::data_struct::tailchat::status::UpdateDMConverse;
use crate::tailchat::client::{emit_join_room, send_message};
2024-06-04 00:22:36 +08:00
2024-03-30 18:30:43 +08:00
/// 所有
pub async fn any_event(event: Event, payload: Payload, _client: Client) {
2024-06-02 23:52:12 +08:00
let handled = [
2024-03-30 18:30:43 +08:00
// 真正处理过的
2024-06-04 00:22:36 +08:00
"notify:chat.message.add",
"notify:chat.message.delete",
2024-06-10 16:05:24 +08:00
"notify:chat.converse.updateDMConverse",
2024-06-02 23:52:12 +08:00
// 也许以后会用到
2024-06-04 00:22:36 +08:00
"notify:chat.message.update",
"notify:chat.message.addReaction",
"notify:chat.message.removeReaction",
2024-06-02 23:52:12 +08:00
// 忽略的
2024-06-04 00:22:36 +08:00
"notify:chat.inbox.append", // 被 @ 之类的事件
2024-03-30 18:30:43 +08:00
];
match &event {
Event::Custom(event_name) => {
if handled.contains(&event_name.as_str()) {
return;
}
}
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());
}
}
2024-06-02 23:52:12 +08:00
_ => {
return;
}
2024-03-30 18:30:43 +08:00
}
return;
}
_ => (),
}
match payload {
Payload::Binary(ref data) => {
println!("event: {} |{:?}", event, data)
}
Payload::Text(ref data) => {
print!("event: {}", event.as_str().purple());
for value in data {
println!("|{}", value);
}
}
_ => (),
}
}
2024-06-04 23:48:01 +08:00
pub async fn on_message(payload: Payload, client: Client) {
if let Payload::Text(values) = payload {
if let Some(value) = values.first() {
2024-06-10 16:05:24 +08:00
let message: ReciveMessage = match serde_json::from_value(value.clone()) {
Ok(v) => v,
Err(e) => {
info!("tailchat_msg {}", value.to_string().red());
info!("tailchat_msg {}", format!("{:?}", e).red());
return;
}
};
2024-06-05 00:03:19 +08:00
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;
}
}
2024-06-04 23:48:01 +08:00
crate::py::call::tailchat_new_message_py(&message, &client).await;
2024-03-30 18:30:43 +08:00
}
}
}
2024-06-04 00:22:36 +08:00
pub async fn on_msg_delete(payload: Payload, _client: Client) {
2024-06-04 23:48:01 +08:00
if let Payload::Text(values) = payload {
if let Some(value) = values.first() {
info!("删除消息 {}", value.to_string().red());
2024-06-04 00:22:36 +08:00
}
}
2024-06-02 23:52:12 +08:00
}
2024-06-10 16:05:24 +08:00
2024-06-10 22:08:37 +08:00
pub async fn on_converse_update(payload: Payload, client: Client) {
2024-06-10 16:05:24 +08:00
if let Payload::Text(values) = payload {
if let Some(value) = values.first() {
2024-06-10 22:08:37 +08:00
emit_join_room(&client).await;
let update_info: UpdateDMConverse = match serde_json::from_value(value.clone()) {
Ok(value) => value,
Err(e) => {
info!("tailchat updateDMConverse {}", value.to_string().red());
info!("tailchat updateDMConverse {}", format!("{:?}", e).red());
return;
}
};
info!("更新会话 {}", format!("{:?}", update_info).cyan());
2024-06-10 16:05:24 +08:00
}
}
}