Compare commits
No commits in common. "345142454422200429a9131f1b0c8c6d0d69d8fe" and "5a07286d2d04f744ec5affa8aeca2ae5237728f7" have entirely different histories.
3451424544
...
5a07286d2d
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -659,7 +659,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ica-rs"
|
name = "ica-rs"
|
||||||
version = "0.6.6"
|
version = "0.6.5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"base64 0.22.1",
|
"base64 0.22.1",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "ica-rs"
|
name = "ica-rs"
|
||||||
version = "0.6.6"
|
version = "0.6.5"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
@ -188,9 +188,9 @@ class TailchatReciveMessage:
|
|||||||
@property
|
@property
|
||||||
def sender_id(self) -> TailchatType.UserId:
|
def sender_id(self) -> TailchatType.UserId:
|
||||||
...
|
...
|
||||||
# @property
|
@property
|
||||||
# def is_from_self(self) -> bool:
|
def is_from_self(self) -> bool:
|
||||||
# ...
|
...
|
||||||
@property
|
@property
|
||||||
def is_reply(self) -> bool:
|
def is_reply(self) -> bool:
|
||||||
...
|
...
|
||||||
@ -200,56 +200,14 @@ 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:
|
|
||||||
"""
|
|
||||||
Tailchat 将要发送的信息
|
|
||||||
"""
|
|
||||||
@property
|
|
||||||
def content(self) -> str:
|
|
||||||
...
|
|
||||||
@content.setter
|
|
||||||
def content(self, value: str) -> None:
|
|
||||||
...
|
|
||||||
def with_content(self, content: str) -> "TailchatSendingMessage":
|
|
||||||
"""
|
|
||||||
为了链式调用, 返回自身
|
|
||||||
"""
|
|
||||||
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 TailchatClient:
|
class TailchatClient:
|
||||||
"""
|
"""
|
||||||
Tailchat 的客户端
|
Tailchat 的客户端
|
||||||
"""
|
"""
|
||||||
def send_message(self, message: TailchatSendingMessage) -> bool:
|
|
||||||
...
|
|
||||||
def send_and_warn(self, message: TailchatSendingMessage) -> bool:
|
|
||||||
"""发送消息, 并在日志中输出警告信息"""
|
|
||||||
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 debug(self, message: str) -> None:
|
||||||
"""向日志中输出调试信息"""
|
"""向日志中输出调试信息"""
|
||||||
def info(self, message: str) -> None:
|
def info(self, message: str) -> None:
|
||||||
@ -277,9 +235,8 @@ 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:
|
||||||
# ...
|
# ...
|
||||||
|
|
||||||
on_tailchat_message = Callable[[TailchatClient, TailchatReciveMessage], None]
|
# TODO: Tailchat adapter
|
||||||
# def on_tailchat_message(client: TailchatClient, msg: TailchatReciveMessage) -> None:
|
on_tailchat_message = Callable[[], None]
|
||||||
# ...
|
|
||||||
|
|
||||||
on_config = Callable[[None], Tuple[str, str]]
|
on_config = Callable[[None], Tuple[str, str]]
|
||||||
|
|
||||||
|
@ -2,23 +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):
|
||||||
if msg.content == "/bot":
|
if msg.content == "/bot":
|
||||||
reply = msg.reply_with(f"ica-async-rs({client.version})-sync-py {client.ica_version}")
|
reply = msg.reply_with(f"ica-async-rs({client.version})-sync-py {client.ica_version}")
|
||||||
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)
|
|
||||||
|
@ -1,10 +1,20 @@
|
|||||||
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};
|
||||||
|
|
||||||
use crate::data_struct::tailchat::{ConverseId, GroupId, MessageId, UserId};
|
use crate::data_struct::tailchat::{ConverseId, GroupId, MessageId, UserId};
|
||||||
|
|
||||||
|
/*{'_id': '6606b3075163504389a6fc47',
|
||||||
|
'content': '光速!(',
|
||||||
|
'author': '6602e20d7b8d10675758e36b',
|
||||||
|
'groupId': '6602e331d31fd31b04aa0693',
|
||||||
|
'converseId': '6602f785928c4254f17726b2',
|
||||||
|
'hasRecall': False,
|
||||||
|
'meta': {'mentions': []},
|
||||||
|
'reactions': [],
|
||||||
|
'createdAt': ExtType(code=0, data=b'\x00\x00\x01\x8e\x8a+TJ'),
|
||||||
|
'updatedAt': ExtType(code=0, data=b'\x00\x00\x01\x8e\x8a+TJ'),
|
||||||
|
'__v': 0} */
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub struct ReciveMessage {
|
pub struct ReciveMessage {
|
||||||
/// 消息ID
|
/// 消息ID
|
||||||
@ -25,7 +35,6 @@ pub struct ReciveMessage {
|
|||||||
#[serde(rename = "hasRecall")]
|
#[serde(rename = "hasRecall")]
|
||||||
pub has_recall: bool,
|
pub has_recall: bool,
|
||||||
/// 暂时懒得解析这玩意
|
/// 暂时懒得解析这玩意
|
||||||
/// 准确来说是不确定内容, 毕竟没细看 API
|
|
||||||
pub meta: JsonValue,
|
pub meta: JsonValue,
|
||||||
/// 也懒得解析这玩意
|
/// 也懒得解析这玩意
|
||||||
pub reactions: Vec<JsonValue>,
|
pub reactions: Vec<JsonValue>,
|
||||||
@ -41,57 +50,19 @@ pub struct ReciveMessage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ReciveMessage {
|
impl ReciveMessage {
|
||||||
pub fn is_reply(&self) -> bool { self.meta.get("reply").is_some() }
|
pub fn as_reply(&self) -> ReplyMessage {
|
||||||
|
ReplyMessage {
|
||||||
/// 创建一个对这条消息的回复
|
content: self.content.clone(),
|
||||||
pub fn as_reply(&self) -> SendingMessage {
|
converse_id: self.converse_id.clone(),
|
||||||
SendingMessage::new(
|
group_id: self.group_id.clone(),
|
||||||
"".to_string(),
|
reply_id: self.msg_id.clone(),
|
||||||
self.converse_id.clone(),
|
}
|
||||||
self.group_id.clone(),
|
|
||||||
Some(ReplyMeta::from_recive_message(self)),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 回复这条消息
|
|
||||||
pub fn reply_with(&self, content: &String) -> SendingMessage {
|
|
||||||
SendingMessage::new(
|
|
||||||
content.clone(),
|
|
||||||
self.converse_id.clone(),
|
|
||||||
self.group_id.clone(),
|
|
||||||
Some(ReplyMeta::from_recive_message(self)),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for ReciveMessage {
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
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)]
|
|
||||||
/// 将要发送的消息
|
|
||||||
///
|
|
||||||
/// 发送时:
|
|
||||||
/// - `content`: 回复的消息内容
|
|
||||||
/// - `converseId`: 会话ID
|
|
||||||
/// - `groupId`: 服务器ID
|
|
||||||
/// - `meta`: 回复的消息的元数据 ( 可能为空 )
|
|
||||||
/// - `mentions`: 被回复的人的ID (可以是多个)
|
|
||||||
/// - `reply`: 被回复的消息
|
|
||||||
/// - `_id`: 被回复的消息ID
|
|
||||||
/// - `author`: 被回复的消息的发送者ID
|
|
||||||
/// - `content`: 被回复的消息内容
|
|
||||||
pub struct SendingMessage {
|
pub struct SendingMessage {
|
||||||
/// 消息内容
|
/// 消息内容
|
||||||
///
|
|
||||||
/// 其实还有个 plain, 就是不知道干啥的
|
|
||||||
pub content: String,
|
pub content: String,
|
||||||
/// 会话ID
|
/// 会话ID
|
||||||
#[serde(rename = "converseId")]
|
#[serde(rename = "converseId")]
|
||||||
@ -99,38 +70,11 @@ pub struct SendingMessage {
|
|||||||
/// 服务器ID
|
/// 服务器ID
|
||||||
#[serde(rename = "groupId")]
|
#[serde(rename = "groupId")]
|
||||||
pub group_id: GroupId,
|
pub group_id: GroupId,
|
||||||
/// 消息的元数据
|
|
||||||
pub meta: Option<ReplyMeta>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SendingMessage {
|
|
||||||
pub fn new(
|
|
||||||
content: String,
|
|
||||||
converse_id: ConverseId,
|
|
||||||
group_id: GroupId,
|
|
||||||
meta: Option<ReplyMeta>,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
|
||||||
content,
|
|
||||||
converse_id,
|
|
||||||
group_id,
|
|
||||||
meta,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn new_without_meta(content: String, converse_id: ConverseId, group_id: GroupId) -> Self {
|
|
||||||
Self {
|
|
||||||
content,
|
|
||||||
converse_id,
|
|
||||||
group_id,
|
|
||||||
meta: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn as_value(&self) -> JsonValue { serde_json::to_value(self).unwrap() }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ReplyMeta {
|
pub struct ReplyMeta {
|
||||||
/// 被回复的人的ID (可以是多个)
|
/// 被回复的人的ID (可以是多个?)
|
||||||
pub mentions: Vec<UserId>,
|
pub mentions: Vec<UserId>,
|
||||||
/// 被回复的消息ID
|
/// 被回复的消息ID
|
||||||
pub reply_id: MessageId,
|
pub reply_id: MessageId,
|
||||||
@ -140,19 +84,6 @@ pub struct ReplyMeta {
|
|||||||
pub reply_content: String,
|
pub reply_content: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ReplyMeta {
|
|
||||||
pub fn from_recive_message(msg: &ReciveMessage) -> Self {
|
|
||||||
Self {
|
|
||||||
mentions: vec![msg.sender_id.clone()],
|
|
||||||
reply_id: msg.msg_id.clone(),
|
|
||||||
reply_author: msg.sender_id.clone(),
|
|
||||||
reply_content: msg.content.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn add_mention(&mut self, user_id: UserId) { self.mentions.push(user_id); }
|
|
||||||
pub fn replace_content(&mut self, content: String) { self.reply_content = content; }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Serialize for ReplyMeta {
|
impl Serialize for ReplyMeta {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@ -160,9 +91,9 @@ impl Serialize for ReplyMeta {
|
|||||||
{
|
{
|
||||||
let reply = json! {
|
let reply = json! {
|
||||||
{
|
{
|
||||||
"_id": self.reply_id,
|
"replyId": self.reply_id,
|
||||||
"author": self.reply_author,
|
"replyAuthor": self.reply_author,
|
||||||
"content": self.reply_content,
|
"replyContent": self.reply_content,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let mut map = serde_json::Map::new();
|
let mut map = serde_json::Map::new();
|
||||||
@ -171,3 +102,18 @@ impl Serialize for ReplyMeta {
|
|||||||
map.serialize(serializer)
|
map.serialize(serializer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct ReplyMessage {
|
||||||
|
/// 消息内容
|
||||||
|
pub content: String,
|
||||||
|
/// 会话ID
|
||||||
|
#[serde(rename = "converseId")]
|
||||||
|
pub converse_id: ConverseId,
|
||||||
|
/// 服务器ID
|
||||||
|
#[serde(rename = "groupId")]
|
||||||
|
pub group_id: GroupId,
|
||||||
|
/// 回复的消息ID
|
||||||
|
#[serde(rename = "replyId")]
|
||||||
|
pub reply_id: MessageId,
|
||||||
|
}
|
||||||
|
@ -133,13 +133,16 @@ pub async fn any_event(event: Event, payload: Payload, _client: Client) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::Message => {
|
Event::Message => {
|
||||||
if let Payload::Text(values) = payload {
|
match payload {
|
||||||
if let Some(value) = values.first() {
|
Payload::Text(values) => {
|
||||||
if handled.contains(&value.as_str().unwrap()) {
|
if let Some(value) = values.first() {
|
||||||
return;
|
if handled.contains(&value.as_str().unwrap()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
info!("收到消息 {}", value.to_string().yellow());
|
||||||
}
|
}
|
||||||
info!("收到消息 {}", value.to_string().yellow());
|
|
||||||
}
|
}
|
||||||
|
_ => (),
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -162,24 +165,27 @@ 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();
|
||||||
if let Payload::Text(values) = payload {
|
match payload {
|
||||||
if let Some(value) = values.first() {
|
Payload::Text(values) => {
|
||||||
match value.as_str() {
|
if let Some(value) = values.first() {
|
||||||
Some("authSucceed") => {
|
match value.as_str() {
|
||||||
event!(Level::INFO, "{}", "已经登录到 icalingua!".green())
|
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 => (),
|
||||||
}
|
}
|
||||||
Some("authFailed") => {
|
|
||||||
event!(Level::ERROR, "{}", "登录到 icalingua 失败!".red());
|
|
||||||
panic!("登录失败")
|
|
||||||
}
|
|
||||||
Some("authRequired") => {
|
|
||||||
event!(Level::INFO, "{}", "需要登录到 icalingua!".yellow())
|
|
||||||
}
|
|
||||||
Some(msg) => {
|
|
||||||
event!(Level::INFO, "{}{}", "未知消息".yellow(), msg);
|
|
||||||
}
|
|
||||||
None => (),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,8 +25,8 @@ pub type MainStatus = status::BotStatus;
|
|||||||
pub type StopGetter = tokio::sync::oneshot::Receiver<()>;
|
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.4.1";
|
||||||
pub const TAILCHAT_VERSION: &str = "1.0.0";
|
pub const TAILCHAT_VERSION: &str = "0.1.0";
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! wrap_callback {
|
macro_rules! wrap_callback {
|
||||||
|
@ -174,9 +174,8 @@ pub async fn tailchat_new_message_py(message: &tailchat::messages::ReciveMessage
|
|||||||
|
|
||||||
let plugins = PyStatus::get_files();
|
let plugins = PyStatus::get_files();
|
||||||
for (path, plugin) in plugins.iter() {
|
for (path, plugin) in plugins.iter() {
|
||||||
let msg = class::tailchat::TailchatReciveMessagePy::from_recive_message(&message);
|
// let msg = class::tailchat::
|
||||||
let client = class::tailchat::TailchatClientPy::new(client);
|
let args = ();
|
||||||
let args = (msg, client);
|
|
||||||
call_py_func!(args, plugin, path, TAILCHAT_NEW_MESSAGE_FUNC, client);
|
call_py_func!(args, plugin, path, TAILCHAT_NEW_MESSAGE_FUNC, client);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
pub mod ica;
|
pub mod ica;
|
||||||
pub mod tailchat;
|
|
||||||
|
|
||||||
use pyo3::prelude::*;
|
use pyo3::prelude::*;
|
||||||
use toml::Value as TomlValue;
|
use toml::Value as TomlValue;
|
||||||
|
@ -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 }
|
pub fn get_room_id(&self) -> RoomId { self.msg.room_id.clone() }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NewMessagePy {
|
impl NewMessagePy {
|
||||||
@ -185,7 +185,10 @@ impl IcaClientPy {
|
|||||||
|
|
||||||
pub fn send_and_warn(&self, message: SendMessagePy) -> bool {
|
pub fn send_and_warn(&self, message: SendMessagePy) -> bool {
|
||||||
warn!(message.msg.content);
|
warn!(message.msg.content);
|
||||||
self.send_message(message)
|
tokio::task::block_in_place(|| {
|
||||||
|
let rt = Runtime::new().unwrap();
|
||||||
|
rt.block_on(send_message(&self.client, &message.msg))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete_message(&self, message: DeleteMessagePy) -> bool {
|
pub fn delete_message(&self, message: DeleteMessagePy) -> bool {
|
||||||
@ -219,9 +222,11 @@ impl IcaClientPy {
|
|||||||
pub fn debug(&self, content: String) {
|
pub fn debug(&self, content: String) {
|
||||||
debug!("{}", content);
|
debug!("{}", content);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn info(&self, content: String) {
|
pub fn info(&self, content: String) {
|
||||||
info!("{}", content);
|
info!("{}", content);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn warn(&self, content: String) {
|
pub fn warn(&self, content: String) {
|
||||||
warn!("{}", content);
|
warn!("{}", content);
|
||||||
}
|
}
|
||||||
|
@ -1,119 +0,0 @@
|
|||||||
use pyo3::prelude::*;
|
|
||||||
|
|
||||||
use rust_socketio::asynchronous::Client;
|
|
||||||
use tracing::{debug, info, warn};
|
|
||||||
|
|
||||||
use crate::data_struct::tailchat::messages::{ReciveMessage, SendingMessage};
|
|
||||||
use crate::data_struct::tailchat::{ConverseId, GroupId, MessageId, UserId};
|
|
||||||
use crate::tailchat::client::send_message;
|
|
||||||
|
|
||||||
#[pyclass]
|
|
||||||
#[pyo3(name = "TailchatClient")]
|
|
||||||
pub struct TailchatClientPy {
|
|
||||||
pub client: Client,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TailchatClientPy {
|
|
||||||
pub fn new(client: &Client) -> Self {
|
|
||||||
Self {
|
|
||||||
client: client.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[pyclass]
|
|
||||||
#[pyo3(name = "TailchatStatus")]
|
|
||||||
/// 预留?
|
|
||||||
pub struct TailchatStatusPy {}
|
|
||||||
|
|
||||||
#[pyclass]
|
|
||||||
#[pyo3(name = "TailchatReciveMessage")]
|
|
||||||
pub struct TailchatReciveMessagePy {
|
|
||||||
pub message: ReciveMessage,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TailchatReciveMessagePy {
|
|
||||||
pub fn from_recive_message(msg: &ReciveMessage) -> Self {
|
|
||||||
Self {
|
|
||||||
message: msg.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
#[pyclass]
|
|
||||||
#[pyo3(name = "TailchatSendingMessage")]
|
|
||||||
pub struct TailchatSendingMessagePy {
|
|
||||||
pub message: SendingMessage,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[pymethods]
|
|
||||||
impl TailchatClientPy {
|
|
||||||
pub fn send_message(&self, message: TailchatSendingMessagePy) -> bool {
|
|
||||||
tokio::task::block_in_place(|| {
|
|
||||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
|
||||||
rt.block_on(send_message(&self.client, &message.message))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn send_and_warn(&self, message: TailchatSendingMessagePy) -> bool {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
pub fn info(&self, content: String) {
|
|
||||||
info!("{}", content);
|
|
||||||
}
|
|
||||||
pub fn warn(&self, content: String) {
|
|
||||||
warn!("{}", content);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[pymethods]
|
|
||||||
impl TailchatReciveMessagePy {
|
|
||||||
#[getter]
|
|
||||||
pub fn get_is_reply(&self) -> bool { self.message.is_reply() }
|
|
||||||
#[getter]
|
|
||||||
pub fn get_msg_id(&self) -> MessageId { self.message.msg_id.clone() }
|
|
||||||
#[getter]
|
|
||||||
pub fn get_content(&self) -> String { self.message.content.clone() }
|
|
||||||
#[getter]
|
|
||||||
pub fn get_sender_id(&self) -> UserId { self.message.sender_id.clone() }
|
|
||||||
#[getter]
|
|
||||||
pub fn get_group_id(&self) -> GroupId { self.message.group_id.clone() }
|
|
||||||
#[getter]
|
|
||||||
pub fn get_converse_id(&self) -> ConverseId { self.message.converse_id.clone() }
|
|
||||||
/// 作为回复
|
|
||||||
pub fn as_reply(&self) -> TailchatSendingMessagePy {
|
|
||||||
TailchatSendingMessagePy {
|
|
||||||
message: self.message.as_reply(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn reply_with(&self, content: String) -> TailchatSendingMessagePy {
|
|
||||||
TailchatSendingMessagePy {
|
|
||||||
message: self.message.reply_with(&content),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[pymethods]
|
|
||||||
impl TailchatSendingMessagePy {
|
|
||||||
#[getter]
|
|
||||||
pub fn get_content(&self) -> String { self.message.content.clone() }
|
|
||||||
#[setter]
|
|
||||||
pub fn set_content(&mut self, content: String) { self.message.content = content; }
|
|
||||||
#[getter]
|
|
||||||
pub fn get_converse_id(&self) -> ConverseId { self.message.converse_id.clone() }
|
|
||||||
#[getter]
|
|
||||||
pub fn get_group_id(&self) -> GroupId { self.message.group_id.clone() }
|
|
||||||
pub fn with_content(&mut self, content: String) -> Self {
|
|
||||||
self.message.content = content;
|
|
||||||
self.clone()
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +1 @@
|
|||||||
use crate::data_struct::tailchat::messages::SendingMessage;
|
|
||||||
// use crate::data_struct::tailchat::{ConverseId, GroupId, MessageId, UserId};
|
|
||||||
|
|
||||||
use rust_socketio::asynchronous::Client;
|
|
||||||
|
|
||||||
use colored::Colorize;
|
|
||||||
use serde_json::Value;
|
|
||||||
use tracing::{debug, info, span, warn, Level};
|
|
||||||
|
|
||||||
pub async fn send_message(client: &Client, message: &SendingMessage) -> bool {
|
|
||||||
let value: Value = message.as_value();
|
|
||||||
match client.emit("chat.message.sendMessage", value).await {
|
|
||||||
Ok(_) => {
|
|
||||||
debug!("send_message {}", format!("{:#?}", message).cyan());
|
|
||||||
true
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
warn!("send_message faild:{}", format!("{:#?}", e).red());
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -4,7 +4,6 @@ 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) {
|
||||||
@ -57,30 +56,24 @@ pub async fn any_event(event: Event, payload: Payload, _client: Client) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn on_message(payload: Payload, client: Client) {
|
pub async fn on_message(payload: Payload, _client: Client) {
|
||||||
if let Payload::Text(values) = payload {
|
match payload {
|
||||||
if let Some(value) = values.first() {
|
Payload::Text(values) => {
|
||||||
let message: ReciveMessage = serde_json::from_value(value.clone()).unwrap();
|
if let Some(value) = values.first() {
|
||||||
info!("tailchat_msg {}", message.to_string().cyan());
|
let message: ReciveMessage = serde_json::from_value(value.clone()).unwrap();
|
||||||
|
info!("收到消息 {:?}", message);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub async fn on_msg_delete(payload: Payload, _client: Client) {
|
pub async fn on_msg_delete(payload: Payload, _client: Client) {
|
||||||
if let Payload::Text(values) = payload {
|
match payload {
|
||||||
if let Some(value) = values.first() {
|
Payload::Text(values) => {
|
||||||
info!("删除消息 {}", value.to_string().red());
|
if let Some(value) = values.first() {
|
||||||
|
info!("删除消息 {}", value.to_string().red());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user