mirror of
http://shenjack.top:5100/shenjack/icalingua-python-bot.git
synced 2025-04-20 10:29:55 +08:00
Compare commits
4 Commits
2f535cc960
...
84ced3b30a
Author | SHA1 | Date | |
---|---|---|---|
84ced3b30a | |||
3d81fce13b | |||
427b113312 | |||
b3e2da9df6 |
|
@ -2,16 +2,30 @@
|
||||||
|
|
||||||
from typing import Callable, Tuple
|
from typing import Callable, Tuple
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
ica.rs
|
||||||
pub type RoomId = i64;
|
pub type RoomId = i64;
|
||||||
pub type UserId = i64;
|
pub type UserId = i64;
|
||||||
pub type MessageId = String;
|
pub type MessageId = String;
|
||||||
"""
|
"""
|
||||||
|
class IcaType:
|
||||||
|
RoomId = int
|
||||||
|
UserId = int
|
||||||
|
MessageId = str
|
||||||
|
|
||||||
RoomId = int
|
"""
|
||||||
UserId = int
|
tailchat.rs
|
||||||
MessageId = str
|
pub type GroupId = String;
|
||||||
|
pub type ConverseId = String;
|
||||||
|
pub type UserId = String;
|
||||||
|
pub type MessageId = String;
|
||||||
|
"""
|
||||||
|
class TailchatType:
|
||||||
|
GroupId = str
|
||||||
|
ConverseId = str
|
||||||
|
UserId = str
|
||||||
|
MessageId = str
|
||||||
|
|
||||||
class IcaStatus:
|
class IcaStatus:
|
||||||
"""
|
"""
|
||||||
|
@ -25,7 +39,7 @@ class IcaStatus:
|
||||||
def online(self) -> bool:
|
def online(self) -> bool:
|
||||||
...
|
...
|
||||||
@property
|
@property
|
||||||
def self_id(self) -> UserId:
|
def self_id(self) -> IcaType.UserId:
|
||||||
...
|
...
|
||||||
@property
|
@property
|
||||||
def nick_name(self) -> str:
|
def nick_name(self) -> str:
|
||||||
|
@ -82,13 +96,13 @@ class IcaNewMessage:
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
...
|
...
|
||||||
@property
|
@property
|
||||||
def id(self) -> MessageId:
|
def id(self) -> IcaType.MessageId:
|
||||||
...
|
...
|
||||||
@property
|
@property
|
||||||
def content(self) -> str:
|
def content(self) -> str:
|
||||||
...
|
...
|
||||||
@property
|
@property
|
||||||
def sender_id(self) -> UserId:
|
def sender_id(self) -> IcaType.UserId:
|
||||||
...
|
...
|
||||||
@property
|
@property
|
||||||
def is_from_self(self) -> bool:
|
def is_from_self(self) -> bool:
|
||||||
|
@ -103,7 +117,7 @@ class IcaNewMessage:
|
||||||
def is_chat_msg(self) -> bool:
|
def is_chat_msg(self) -> bool:
|
||||||
"""是否是私聊消息"""
|
"""是否是私聊消息"""
|
||||||
@property
|
@property
|
||||||
def room_id(self) -> RoomId:
|
def room_id(self) -> IcaType.RoomId:
|
||||||
"""
|
"""
|
||||||
如果是群聊消息, 返回 (-群号)
|
如果是群聊消息, 返回 (-群号)
|
||||||
如果是私聊消息, 返回 对面qq
|
如果是私聊消息, 返回 对面qq
|
||||||
|
@ -148,10 +162,45 @@ class IcaClient:
|
||||||
"""向日志中输出警告信息"""
|
"""向日志中输出警告信息"""
|
||||||
|
|
||||||
|
|
||||||
class MatrixClient:
|
class TailchatReciveMessage:
|
||||||
"""
|
"""
|
||||||
Matrix 的客户端
|
Tailchat 接收到的新消息
|
||||||
"""
|
"""
|
||||||
|
@property
|
||||||
|
def id(self) -> TailchatType.MessageId:
|
||||||
|
...
|
||||||
|
@property
|
||||||
|
def content(self) -> str:
|
||||||
|
...
|
||||||
|
@property
|
||||||
|
def sender_id(self) -> TailchatType.UserId:
|
||||||
|
...
|
||||||
|
@property
|
||||||
|
def is_from_self(self) -> bool:
|
||||||
|
...
|
||||||
|
@property
|
||||||
|
def is_reply(self) -> bool:
|
||||||
|
...
|
||||||
|
@property
|
||||||
|
def group_id(self) -> TailchatType.GroupId:
|
||||||
|
...
|
||||||
|
@property
|
||||||
|
def converse_id(self) -> TailchatType.ConverseId:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class TailchatClient:
|
||||||
|
"""
|
||||||
|
Tailchat 的客户端
|
||||||
|
"""
|
||||||
|
|
||||||
|
def debug(self, message: str) -> None:
|
||||||
|
"""向日志中输出调试信息"""
|
||||||
|
def info(self, message: str) -> None:
|
||||||
|
"""向日志中输出信息"""
|
||||||
|
def warn(self, message: str) -> None:
|
||||||
|
"""向日志中输出警告信息"""
|
||||||
|
|
||||||
|
|
||||||
class ConfigData:
|
class ConfigData:
|
||||||
|
@ -169,12 +218,12 @@ on_ica_message = Callable[[IcaNewMessage, IcaClient], None]
|
||||||
# def on_message(msg: NewMessage, client: IcaClient) -> None:
|
# def on_message(msg: NewMessage, client: IcaClient) -> None:
|
||||||
# ...
|
# ...
|
||||||
|
|
||||||
on_ica_delete_message = Callable[[MessageId, IcaClient], None]
|
on_ica_delete_message = Callable[[IcaType.MessageId, IcaClient], None]
|
||||||
# def on_delete_message(msg_id: MessageId, client: IcaClient) -> None:
|
# def on_delete_message(msg_id: MessageId, client: IcaClient) -> None:
|
||||||
# ...
|
# ...
|
||||||
|
|
||||||
# TODO: Matrix adapter
|
# TODO: Tailchat adapter
|
||||||
on_matrix_message = Callable[[], None]
|
on_tailchat_message = Callable[[], None]
|
||||||
|
|
||||||
on_config = Callable[[None], Tuple[str, str]]
|
on_config = Callable[[None], Tuple[str, str]]
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,8 @@ use serde::Deserialize;
|
||||||
use toml::from_str;
|
use toml::from_str;
|
||||||
use tracing::warn;
|
use tracing::warn;
|
||||||
|
|
||||||
|
use crate::data_struct::{ica, tailchat};
|
||||||
|
|
||||||
/// Icalingua bot 的配置
|
/// Icalingua bot 的配置
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
pub struct IcaConfig {
|
pub struct IcaConfig {
|
||||||
|
@ -13,15 +15,33 @@ pub struct IcaConfig {
|
||||||
/// icalingua 服务器地址
|
/// icalingua 服务器地址
|
||||||
pub host: String,
|
pub host: String,
|
||||||
/// bot 的 qq
|
/// bot 的 qq
|
||||||
pub self_id: u64,
|
pub self_id: ica::UserId,
|
||||||
/// 提醒的房间
|
/// 提醒的房间
|
||||||
pub notice_room: Vec<i64>,
|
pub notice_room: Vec<ica::RoomId>,
|
||||||
/// 是否提醒
|
/// 是否提醒
|
||||||
pub notice_start: bool,
|
pub notice_start: bool,
|
||||||
/// 管理员列表
|
/// 管理员列表
|
||||||
pub admin_list: Vec<i64>,
|
pub admin_list: Vec<ica::UserId>,
|
||||||
/// 过滤列表
|
/// 过滤列表
|
||||||
pub filter_list: Vec<i64>,
|
pub filter_list: Vec<ica::UserId>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
pub struct TailchatConfig {
|
||||||
|
/// 服务器地址
|
||||||
|
pub host: String,
|
||||||
|
/// 机器人 App ID
|
||||||
|
pub app_id: String,
|
||||||
|
/// 机器人 App Secret
|
||||||
|
pub app_secret: String,
|
||||||
|
/// 提醒的房间
|
||||||
|
pub notice_room: Vec<(tailchat::GroupId, tailchat::ConverseId)>,
|
||||||
|
/// 是否提醒
|
||||||
|
pub notice_start: bool,
|
||||||
|
/// 管理员列表
|
||||||
|
pub admin_list: Vec<tailchat::UserId>,
|
||||||
|
/// 过滤列表
|
||||||
|
pub filter_list: Vec<tailchat::UserId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
@ -40,6 +60,11 @@ pub struct BotConfig {
|
||||||
/// Ica 配置
|
/// Ica 配置
|
||||||
pub ica: Option<IcaConfig>,
|
pub ica: Option<IcaConfig>,
|
||||||
|
|
||||||
|
/// 是否启用 Tailchat
|
||||||
|
pub enable_tailchat: Option<bool>,
|
||||||
|
/// Tailchat 配置
|
||||||
|
pub tailchat: Option<TailchatConfig>,
|
||||||
|
|
||||||
/// 是否启用 Python 插件
|
/// 是否启用 Python 插件
|
||||||
pub enable_py: Option<bool>,
|
pub enable_py: Option<bool>,
|
||||||
/// Python 插件配置
|
/// Python 插件配置
|
||||||
|
@ -88,6 +113,26 @@ impl BotConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 检查是否启用 Tailchat
|
||||||
|
pub fn check_tailchat(&self) -> bool {
|
||||||
|
match self.enable_tailchat {
|
||||||
|
Some(enable) => {
|
||||||
|
if enable && self.tailchat.is_none() {
|
||||||
|
warn!("enable_tailchat 为 true 但未填写 [tailchat] 配置\n将不启用 Tailchat");
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
if self.tailchat.is_some() {
|
||||||
|
warn!("未填写 enable_tailchat 但填写了 [tailchat] 配置\n将不启用 Tailchat");
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// 检查是否启用 Python 插件
|
/// 检查是否启用 Python 插件
|
||||||
pub fn check_py(&self) -> bool {
|
pub fn check_py(&self) -> bool {
|
||||||
match self.enable_py {
|
match self.enable_py {
|
||||||
|
@ -109,5 +154,8 @@ impl BotConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ica(&self) -> IcaConfig { self.ica.clone().expect("No ica config found") }
|
pub fn ica(&self) -> IcaConfig { self.ica.clone().expect("No ica config found") }
|
||||||
|
pub fn tailchat(&self) -> TailchatConfig {
|
||||||
|
self.tailchat.clone().expect("No tailchat config found")
|
||||||
|
}
|
||||||
pub fn py(&self) -> PyConfig { self.py.clone().expect("No py config found") }
|
pub fn py(&self) -> PyConfig { self.py.clone().expect("No py config found") }
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
pub mod messages;
|
pub mod messages;
|
||||||
|
pub mod status;
|
||||||
|
|
||||||
pub type GroupId = String;
|
pub type GroupId = String;
|
||||||
pub type ConverseId = String;
|
pub type ConverseId = String;
|
||||||
|
|
13
ica-rs/src/data_struct/tailchat/status.rs
Normal file
13
ica-rs/src/data_struct/tailchat/status.rs
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::data_struct::tailchat::UserId;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
pub struct LoginData {
|
||||||
|
pub jwt: String,
|
||||||
|
#[serde(rename = "userId")]
|
||||||
|
pub user_id: UserId,
|
||||||
|
pub email: String,
|
||||||
|
pub nickname: String,
|
||||||
|
pub avatar: String,
|
||||||
|
}
|
|
@ -12,6 +12,8 @@ pub enum IcaError {
|
||||||
pub enum TailchatError {
|
pub enum TailchatError {
|
||||||
/// Socket IO 链接错误
|
/// Socket IO 链接错误
|
||||||
SocketIoError(rust_socketio::error::Error),
|
SocketIoError(rust_socketio::error::Error),
|
||||||
|
/// reqwest 相关错误
|
||||||
|
ReqwestError(reqwest::Error),
|
||||||
/// 登录失败
|
/// 登录失败
|
||||||
LoginFailed(String),
|
LoginFailed(String),
|
||||||
}
|
}
|
||||||
|
@ -35,6 +37,14 @@ impl From<rust_socketio::Error> for IcaError {
|
||||||
fn from(e: rust_socketio::Error) -> Self { IcaError::SocketIoError(e) }
|
fn from(e: rust_socketio::Error) -> Self { IcaError::SocketIoError(e) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<rust_socketio::Error> for TailchatError {
|
||||||
|
fn from(e: rust_socketio::Error) -> Self { TailchatError::SocketIoError(e) }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<reqwest::Error> for TailchatError {
|
||||||
|
fn from(e: reqwest::Error) -> Self { TailchatError::ReqwestError(e) }
|
||||||
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for IcaError {
|
impl std::fmt::Display for IcaError {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
|
@ -48,6 +58,7 @@ impl std::fmt::Display for TailchatError {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
TailchatError::SocketIoError(e) => write!(f, "Socket IO 链接错误: {}", e),
|
TailchatError::SocketIoError(e) => write!(f, "Socket IO 链接错误: {}", e),
|
||||||
|
TailchatError::ReqwestError(e) => write!(f, "Reqwest 错误: {}", e),
|
||||||
TailchatError::LoginFailed(e) => write!(f, "登录失败: {}", e),
|
TailchatError::LoginFailed(e) => write!(f, "登录失败: {}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,6 +96,7 @@ impl std::error::Error for TailchatError {
|
||||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||||
match self {
|
match self {
|
||||||
TailchatError::SocketIoError(e) => Some(e),
|
TailchatError::SocketIoError(e) => Some(e),
|
||||||
|
TailchatError::ReqwestError(e) => Some(e),
|
||||||
TailchatError::LoginFailed(_) => None,
|
TailchatError::LoginFailed(_) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ use tracing::{event, info, span, Level};
|
||||||
pub static mut MAIN_STATUS: status::BotStatus = status::BotStatus {
|
pub static mut MAIN_STATUS: status::BotStatus = status::BotStatus {
|
||||||
config: None,
|
config: None,
|
||||||
ica_status: None,
|
ica_status: None,
|
||||||
|
tailchat_status: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type MainStatus = status::BotStatus;
|
pub type MainStatus = status::BotStatus;
|
||||||
|
@ -80,6 +81,18 @@ async fn main() {
|
||||||
event!(Level::INFO, "未启用 ica");
|
event!(Level::INFO, "未启用 ica");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let (tailchat_send, tailchat_recv) = tokio::sync::oneshot::channel::<()>();
|
||||||
|
|
||||||
|
if bot_config.check_tailchat() {
|
||||||
|
event!(Level::INFO, "启动 Tailchat");
|
||||||
|
let config = bot_config.tailchat();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
tailchat::start_tailchat(config, tailchat_recv).await.unwrap();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
event!(Level::INFO, "未启用 Tailchat");
|
||||||
|
}
|
||||||
|
|
||||||
tokio::time::sleep(Duration::from_secs(2)).await;
|
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||||
// 等待一个输入
|
// 等待一个输入
|
||||||
info!("Press any key to exit");
|
info!("Press any key to exit");
|
||||||
|
@ -87,6 +100,7 @@ async fn main() {
|
||||||
std::io::stdin().read_line(&mut input).unwrap();
|
std::io::stdin().read_line(&mut input).unwrap();
|
||||||
|
|
||||||
ica_send.send(()).ok();
|
ica_send.send(()).ok();
|
||||||
|
tailchat_send.send(()).ok();
|
||||||
|
|
||||||
info!("Disconnected");
|
info!("Disconnected");
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,8 +96,21 @@ pub const ICA_DELETE_MESSAGE_FUNC: &str = "on_ica_delete_message";
|
||||||
pub const TAILCHAT_NEW_MESSAGE_FUNC: &str = "on_tailchat_message";
|
pub const TAILCHAT_NEW_MESSAGE_FUNC: &str = "on_tailchat_message";
|
||||||
|
|
||||||
macro_rules! call_py_func {
|
macro_rules! call_py_func {
|
||||||
($args:expr, $func_name:expr, $client:expr) => {
|
($args:expr, $plugin:expr, $plugin_path:expr, $func_name:expr, $client:expr) => {
|
||||||
|
tokio::spawn(async move {
|
||||||
|
Python::with_gil(|py| {
|
||||||
|
if let Ok(py_func) = get_func($plugin.py_module.bind(py), $func_name) {
|
||||||
|
if let Err(e) = py_func.call1($args) {
|
||||||
|
let e = PyPluginError::FuncCallError(
|
||||||
|
e,
|
||||||
|
$func_name.to_string(),
|
||||||
|
$plugin_path.to_string_lossy().to_string(),
|
||||||
|
);
|
||||||
|
warn!("failed to call function<{}>: {:?}", $func_name, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,20 +125,21 @@ pub async fn ica_new_message_py(message: &ica::messages::NewMessage, client: &Cl
|
||||||
let client = class::ica::IcaClientPy::new(client);
|
let client = class::ica::IcaClientPy::new(client);
|
||||||
let args = (msg, client);
|
let args = (msg, client);
|
||||||
// 甚至实际上压根不需要await这个spawn, 直接让他自己跑就好了(离谱)
|
// 甚至实际上压根不需要await这个spawn, 直接让他自己跑就好了(离谱)
|
||||||
tokio::spawn(async move {
|
call_py_func!(args, plugin, path, ICA_NEW_MESSAGE_FUNC, client);
|
||||||
Python::with_gil(|py| {
|
// tokio::spawn(async move {
|
||||||
if let Ok(py_func) = get_func(plugin.py_module.bind(py), ICA_NEW_MESSAGE_FUNC) {
|
// Python::with_gil(|py| {
|
||||||
if let Err(e) = py_func.call1(args) {
|
// if let Ok(py_func) = get_func(plugin.py_module.bind(py), ICA_NEW_MESSAGE_FUNC) {
|
||||||
let e = PyPluginError::FuncCallError(
|
// if let Err(e) = py_func.call1(args) {
|
||||||
e,
|
// let e = PyPluginError::FuncCallError(
|
||||||
ICA_NEW_MESSAGE_FUNC.to_string(),
|
// e,
|
||||||
path.to_string_lossy().to_string(),
|
// ICA_NEW_MESSAGE_FUNC.to_string(),
|
||||||
);
|
// path.to_string_lossy().to_string(),
|
||||||
warn!("failed to call function<{}>: {:?}", ICA_NEW_MESSAGE_FUNC, e);
|
// );
|
||||||
}
|
// warn!("failed to call function<{}>: {:?}", ICA_NEW_MESSAGE_FUNC, e);
|
||||||
}
|
// }
|
||||||
})
|
// }
|
||||||
});
|
// })
|
||||||
|
// });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,23 +151,31 @@ pub async fn ica_delete_message_py(msg_id: ica::MessageId, client: &Client) {
|
||||||
let msg_id = msg_id.clone();
|
let msg_id = msg_id.clone();
|
||||||
let client = class::ica::IcaClientPy::new(client);
|
let client = class::ica::IcaClientPy::new(client);
|
||||||
let args = (msg_id.clone(), client);
|
let args = (msg_id.clone(), client);
|
||||||
tokio::spawn(async move {
|
call_py_func!(args, plugin, path, ICA_DELETE_MESSAGE_FUNC, client);
|
||||||
Python::with_gil(|py| {
|
// tokio::spawn(async move {
|
||||||
if let Ok(py_func) = get_func(plugin.py_module.bind(py), ICA_DELETE_MESSAGE_FUNC) {
|
// Python::with_gil(|py| {
|
||||||
if let Err(e) = py_func.call1(args) {
|
// if let Ok(py_func) = get_func(plugin.py_module.bind(py), ICA_DELETE_MESSAGE_FUNC) {
|
||||||
let e = PyPluginError::FuncCallError(
|
// if let Err(e) = py_func.call1(args) {
|
||||||
e,
|
// let e = PyPluginError::FuncCallError(
|
||||||
ICA_DELETE_MESSAGE_FUNC.to_string(),
|
// e,
|
||||||
path.to_string_lossy().to_string(),
|
// ICA_DELETE_MESSAGE_FUNC.to_string(),
|
||||||
);
|
// path.to_string_lossy().to_string(),
|
||||||
warn!("failed to call function<{}>: {:?}", ICA_DELETE_MESSAGE_FUNC, e);
|
// );
|
||||||
}
|
// warn!("failed to call function<{}>: {:?}", ICA_DELETE_MESSAGE_FUNC, e);
|
||||||
}
|
// }
|
||||||
})
|
// }
|
||||||
});
|
// })
|
||||||
|
// });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn tailchat_new_message_py(message: tailchat::messages::ReciveMessage, client: &Client) {
|
pub async fn tailchat_new_message_py(message: &tailchat::messages::ReciveMessage, client: &Client) {
|
||||||
|
verify_plugins();
|
||||||
|
|
||||||
|
let plugins = PyStatus::get_files();
|
||||||
|
for (path, plugin) in plugins.iter() {
|
||||||
|
// let msg = class::tailchat::
|
||||||
|
let args = ();
|
||||||
|
call_py_func!(args, plugin, path, TAILCHAT_NEW_MESSAGE_FUNC, client);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,7 +96,8 @@ impl TryFrom<RawPyPlugin> for PyPlugin {
|
||||||
};
|
};
|
||||||
match config_value {
|
match config_value {
|
||||||
Ok(config) => {
|
Ok(config) => {
|
||||||
let py_config = Bound::new(py, class::ConfigDataPy::new(config)).unwrap();
|
let py_config =
|
||||||
|
Bound::new(py, class::ConfigDataPy::new(config)).unwrap();
|
||||||
module.setattr("CONFIG_DATA", py_config).unwrap();
|
module.setattr("CONFIG_DATA", py_config).unwrap();
|
||||||
Ok(PyPlugin {
|
Ok(PyPlugin {
|
||||||
file_path: path,
|
file_path: path,
|
||||||
|
|
|
@ -5,6 +5,7 @@ use crate::MAIN_STATUS;
|
||||||
pub struct BotStatus {
|
pub struct BotStatus {
|
||||||
pub config: Option<BotConfig>,
|
pub config: Option<BotConfig>,
|
||||||
pub ica_status: Option<ica::MainStatus>,
|
pub ica_status: Option<ica::MainStatus>,
|
||||||
|
pub tailchat_status: Option<tailchat::MainStatus>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BotStatus {
|
impl BotStatus {
|
||||||
|
@ -18,6 +19,11 @@ impl BotStatus {
|
||||||
MAIN_STATUS.ica_status = Some(status);
|
MAIN_STATUS.ica_status = Some(status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn update_tailchat_status(status: tailchat::MainStatus) {
|
||||||
|
unsafe {
|
||||||
|
MAIN_STATUS.tailchat_status = Some(status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn static_init(config: BotConfig) {
|
pub fn static_init(config: BotConfig) {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -33,13 +39,20 @@ impl BotStatus {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn global_config() -> &'static BotConfig { unsafe { MAIN_STATUS.config.as_ref().unwrap() } }
|
pub fn global_config() -> &'static BotConfig { unsafe { MAIN_STATUS.config.as_ref().unwrap() } }
|
||||||
|
|
||||||
pub fn global_ica_status() -> &'static ica::MainStatus {
|
pub fn global_ica_status() -> &'static ica::MainStatus {
|
||||||
unsafe { MAIN_STATUS.ica_status.as_ref().unwrap() }
|
unsafe { MAIN_STATUS.ica_status.as_ref().unwrap() }
|
||||||
}
|
}
|
||||||
|
pub fn global_tailchat_status() -> &'static tailchat::MainStatus {
|
||||||
|
unsafe { MAIN_STATUS.tailchat_status.as_ref().unwrap() }
|
||||||
|
}
|
||||||
|
|
||||||
pub fn global_ica_status_mut() -> &'static mut ica::MainStatus {
|
pub fn global_ica_status_mut() -> &'static mut ica::MainStatus {
|
||||||
unsafe { MAIN_STATUS.ica_status.as_mut().unwrap() }
|
unsafe { MAIN_STATUS.ica_status.as_mut().unwrap() }
|
||||||
}
|
}
|
||||||
|
pub fn global_tailchat_status_mut() -> &'static mut tailchat::MainStatus {
|
||||||
|
unsafe { MAIN_STATUS.tailchat_status.as_mut().unwrap() }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod ica {
|
pub mod ica {
|
||||||
|
@ -65,3 +78,33 @@ pub mod ica {
|
||||||
pub fn update_online_status(&mut self, status: OnlineData) { self.online_status = status; }
|
pub fn update_online_status(&mut self, status: OnlineData) { self.online_status = status; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod tailchat {
|
||||||
|
use crate::data_struct::tailchat::UserId;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct MainStatus {
|
||||||
|
/// 是否启用 tailchat
|
||||||
|
pub enable: bool,
|
||||||
|
/// 是否登录
|
||||||
|
pub login: bool,
|
||||||
|
/// 用户 ID
|
||||||
|
pub user_id: UserId,
|
||||||
|
/// 昵称
|
||||||
|
pub nick_name: String,
|
||||||
|
/// 邮箱
|
||||||
|
pub email: String,
|
||||||
|
/// JWT Token
|
||||||
|
pub jwt_token: String,
|
||||||
|
/// avatar
|
||||||
|
pub avatar: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MainStatus {
|
||||||
|
pub fn update_user_id(&mut self, user_id: UserId) { self.user_id = user_id; }
|
||||||
|
pub fn update_nick_name(&mut self, nick_name: String) { self.nick_name = nick_name; }
|
||||||
|
pub fn update_email(&mut self, email: String) { self.email = email; }
|
||||||
|
pub fn update_jwt_token(&mut self, jwt_token: String) { self.jwt_token = jwt_token; }
|
||||||
|
pub fn update_avatar(&mut self, avatar: String) { self.avatar = avatar; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,23 +1,104 @@
|
||||||
|
pub mod client;
|
||||||
pub mod events;
|
pub mod events;
|
||||||
|
|
||||||
use futures_util::FutureExt;
|
use futures_util::FutureExt;
|
||||||
|
use md5::{Digest, Md5};
|
||||||
|
use reqwest::{Body, ClientBuilder as reqwest_ClientBuilder};
|
||||||
use rust_socketio::asynchronous::{Client, ClientBuilder};
|
use rust_socketio::asynchronous::{Client, ClientBuilder};
|
||||||
use rust_socketio::{Event, Payload, TransportType};
|
use rust_socketio::{Event, Payload, TransportType};
|
||||||
|
use serde_json::{from_str, from_value, json, Value};
|
||||||
use tracing::{event, span, Level};
|
use tracing::{event, span, Level};
|
||||||
|
|
||||||
// use crate::config::IcaConfig;
|
use crate::config::TailchatConfig;
|
||||||
|
use crate::data_struct::tailchat::status::LoginData;
|
||||||
use crate::error::{ClientResult, TailchatError};
|
use crate::error::{ClientResult, TailchatError};
|
||||||
|
use crate::{wrap_any_callback, wrap_callback, StopGetter};
|
||||||
|
|
||||||
pub async fn start_tailchat() -> ClientResult<(), TailchatError> {
|
pub async fn start_tailchat(
|
||||||
|
config: TailchatConfig,
|
||||||
|
stop_reciver: StopGetter,
|
||||||
|
) -> ClientResult<(), TailchatError> {
|
||||||
let span = span!(Level::INFO, "Tailchat Client");
|
let span = span!(Level::INFO, "Tailchat Client");
|
||||||
let _enter = span.enter();
|
let _enter = span.enter();
|
||||||
|
|
||||||
event!(Level::INFO, "tailchat-async-rs v{} initing", crate::TAILCHAT_VERSION);
|
event!(Level::INFO, "tailchat-async-rs v{} initing", crate::TAILCHAT_VERSION);
|
||||||
|
|
||||||
// let socket = match ClientBuilder::new() {
|
let mut hasher = Md5::new();
|
||||||
|
hasher.update(config.app_id.as_bytes());
|
||||||
|
hasher.update(config.app_secret.as_bytes());
|
||||||
|
|
||||||
// };
|
let token = format!("{:x}", hasher.finalize());
|
||||||
|
|
||||||
Ok(())
|
let mut header_map = reqwest::header::HeaderMap::new();
|
||||||
|
header_map.append("Content-Type", "application/json".parse().unwrap());
|
||||||
|
|
||||||
|
let client = reqwest_ClientBuilder::new().default_headers(header_map.clone()).build()?;
|
||||||
|
let status = match client
|
||||||
|
.post(&format!("{}/api/openapi/bot/login", config.host))
|
||||||
|
.body(json! {{"appId": config.app_id, "token": token}}.to_string())
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(resp) => {
|
||||||
|
if resp.status().is_success() {
|
||||||
|
let raw_data = resp.text().await?;
|
||||||
|
let json_data = serde_json::from_str::<Value>(&raw_data).unwrap();
|
||||||
|
let login_data = serde_json::from_value::<LoginData>(json_data["data"].clone());
|
||||||
|
match login_data {
|
||||||
|
Ok(data) => data,
|
||||||
|
Err(e) => {
|
||||||
|
event!(Level::ERROR, "login failed: {}|{}", e, raw_data);
|
||||||
|
return Err(TailchatError::LoginFailed(e.to_string()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(TailchatError::LoginFailed(resp.text().await?));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => return Err(TailchatError::LoginFailed(e.to_string())),
|
||||||
|
};
|
||||||
|
|
||||||
|
header_map.append("X-Token", status.jwt.clone().parse().unwrap());
|
||||||
|
|
||||||
|
let client = reqwest_ClientBuilder::new().default_headers(header_map).build()?;
|
||||||
|
|
||||||
|
let socket = ClientBuilder::new(config.host)
|
||||||
|
.auth(json!({"token": status.jwt.clone()}))
|
||||||
|
.transport_type(TransportType::Websocket)
|
||||||
|
.on_any(wrap_any_callback!(events::any_event))
|
||||||
|
.on("chat.message.sendMessage", wrap_callback!(events::on_message))
|
||||||
|
.connect()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
// notify:chat.message.delete
|
||||||
|
// notify:chat.message.add
|
||||||
|
|
||||||
|
stop_reciver.await.ok();
|
||||||
|
event!(Level::INFO, "socketio client stopping");
|
||||||
|
match socket.disconnect().await {
|
||||||
|
Ok(_) => {
|
||||||
|
event!(Level::INFO, "socketio client stopped");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
// 单独处理 SocketIoError(IncompleteResponseFromEngineIo(WebsocketError(AlreadyClosed)))
|
||||||
|
match e {
|
||||||
|
rust_socketio::Error::IncompleteResponseFromEngineIo(inner_e) => {
|
||||||
|
if inner_e.to_string().contains("AlreadyClosed") {
|
||||||
|
event!(Level::INFO, "socketio client stopped");
|
||||||
|
return Ok(());
|
||||||
|
} else {
|
||||||
|
event!(Level::ERROR, "socketio client stopped with error: {:?}", inner_e);
|
||||||
|
Err(TailchatError::SocketIoError(
|
||||||
|
rust_socketio::Error::IncompleteResponseFromEngineIo(inner_e),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
e => {
|
||||||
|
event!(Level::ERROR, "socketio client stopped with error: {}", e);
|
||||||
|
Err(TailchatError::SocketIoError(e))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
1
ica-rs/src/tailchat/client.rs
Normal file
1
ica-rs/src/tailchat/client.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
use colored::Colorize;
|
||||||
|
use rust_socketio::asynchronous::Client;
|
||||||
|
use rust_socketio::{Event, Payload};
|
||||||
|
use tracing::info;
|
||||||
|
|
||||||
|
/// 所有
|
||||||
|
pub async fn any_event(event: Event, payload: Payload, _client: Client) {
|
||||||
|
let handled = vec![
|
||||||
|
// 真正处理过的
|
||||||
|
"chat.message.sendMessage", // 也许以后会用到
|
||||||
|
|
||||||
|
// 忽略的
|
||||||
|
];
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn on_message(payload: Payload, client: Client) {
|
||||||
|
match payload {
|
||||||
|
Payload::Text(values) => {
|
||||||
|
if let Some(value) = values.first() {
|
||||||
|
info!("收到消息 {}", value.to_string().yellow());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user