mirror of
http://shenjack.top:5100/shenjack/icalingua-python-bot.git
synced 2025-04-20 10:19:53 +08:00
Compare commits
No commits in common. "aa20b7f1c343c0ffd60bb2160a48b319e1100f8d" and "2ab3f0d77bb7a7f385fbf16dc51e72498707797b" have entirely different histories.
aa20b7f1c3
...
2ab3f0d77b
|
@ -72,11 +72,8 @@ class IcaDeleteMessage:
|
||||||
|
|
||||||
|
|
||||||
class IcaNewMessage:
|
class IcaNewMessage:
|
||||||
"""
|
|
||||||
Icalingua 接收到新消息
|
|
||||||
"""
|
|
||||||
def reply_with(self, message: str) -> IcaSendMessage:
|
def reply_with(self, message: str) -> IcaSendMessage:
|
||||||
"""回复这条消息"""
|
...
|
||||||
def as_deleted(self) -> IcaDeleteMessage:
|
def as_deleted(self) -> IcaDeleteMessage:
|
||||||
...
|
...
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
|
@ -98,22 +95,16 @@ class IcaNewMessage:
|
||||||
...
|
...
|
||||||
@property
|
@property
|
||||||
def is_room_msg(self) -> bool:
|
def is_room_msg(self) -> bool:
|
||||||
"""是否是群聊消息"""
|
...
|
||||||
@property
|
@property
|
||||||
def is_chat_msg(self) -> bool:
|
def is_chat_msg(self) -> bool:
|
||||||
"""是否是私聊消息"""
|
...
|
||||||
@property
|
@property
|
||||||
def room_id(self) -> RoomId:
|
def room_id(self) -> RoomId:
|
||||||
"""
|
...
|
||||||
如果是群聊消息, 返回 (-群号)
|
|
||||||
如果是私聊消息, 返回 对面qq
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
class IcaClient:
|
class IcaClient:
|
||||||
"""
|
|
||||||
Icalingua 的客户端
|
|
||||||
"""
|
|
||||||
# @staticmethod
|
# @staticmethod
|
||||||
# async def send_message_a(client: "IcaClient", message: SendMessage) -> bool:
|
# async def send_message_a(client: "IcaClient", message: SendMessage) -> bool:
|
||||||
# """
|
# """
|
||||||
|
@ -151,12 +142,6 @@ class IcaClient:
|
||||||
"""向日志中输出警告信息"""
|
"""向日志中输出警告信息"""
|
||||||
|
|
||||||
|
|
||||||
class MatrixClient:
|
|
||||||
"""
|
|
||||||
Matrix 的客户端
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
class ConfigData:
|
class ConfigData:
|
||||||
def __getitem__(self, key: str):
|
def __getitem__(self, key: str):
|
||||||
...
|
...
|
||||||
|
@ -177,7 +162,7 @@ on_ica_delete_message = Callable[[MessageId, IcaClient], None]
|
||||||
# ...
|
# ...
|
||||||
|
|
||||||
# TODO: Matrix adapter
|
# TODO: Matrix adapter
|
||||||
on_matrix_message = Callable[[], None]
|
# on_matrix_room_message = Callable[[RoomId, NewMessage, IcaClient], None]
|
||||||
|
|
||||||
on_config = Callable[[None], Tuple[str, str]]
|
on_config = Callable[[None], Tuple[str, str]]
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use rust_socketio::asynchronous::Client;
|
use rust_socketio::asynchronous::Client;
|
||||||
use rust_socketio::{Event, Payload};
|
use rust_socketio::{Event, Payload};
|
||||||
use tracing::{event, info, span, warn, Level};
|
use tracing::{info, warn};
|
||||||
|
|
||||||
use crate::data_struct::ica::all_rooms::Room;
|
use crate::data_struct::ica::all_rooms::Room;
|
||||||
use crate::data_struct::ica::messages::{Message, MessageTrait, NewMessage};
|
use crate::data_struct::ica::messages::{Message, MessageTrait, NewMessage};
|
||||||
|
@ -122,7 +122,7 @@ pub async fn any_event(event: Event, payload: Payload, _client: Client) {
|
||||||
"notify",
|
"notify",
|
||||||
"closeLoading", // 发送消息/加载新聊天 有一个 loading
|
"closeLoading", // 发送消息/加载新聊天 有一个 loading
|
||||||
"updateRoom",
|
"updateRoom",
|
||||||
"syncRead", // 同步已读
|
// "syncRead",
|
||||||
];
|
];
|
||||||
match &event {
|
match &event {
|
||||||
Event::Custom(event_name) => {
|
Event::Custom(event_name) => {
|
||||||
|
@ -161,24 +161,22 @@ 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 _enter = span.enter();
|
|
||||||
match payload {
|
match payload {
|
||||||
Payload::Text(values) => {
|
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())
|
info!("{}", "已经登录到 icalingua!".green())
|
||||||
}
|
}
|
||||||
Some("authFailed") => {
|
Some("authFailed") => {
|
||||||
event!(Level::ERROR, "{}", "登录到 icalingua 失败!".red());
|
info!("{}", "登录到 icalingua 失败!".red());
|
||||||
panic!("登录失败")
|
panic!("登录失败")
|
||||||
}
|
}
|
||||||
Some("authRequired") => {
|
Some("authRequired") => {
|
||||||
event!(Level::INFO, "{}", "需要登录到 icalingua!".yellow())
|
warn!("{}", "需要登录到 icalingua!".yellow())
|
||||||
}
|
}
|
||||||
Some(msg) => {
|
Some(msg) => {
|
||||||
event!(Level::INFO, "{}", "未知消息".yellow());
|
warn!("未知消息 {}", msg.yellow())
|
||||||
}
|
}
|
||||||
None => (),
|
None => (),
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,18 @@
|
||||||
pub mod events;
|
pub mod events;
|
||||||
|
|
||||||
use std::{str::FromStr, time::Duration};
|
use std::str::FromStr;
|
||||||
|
|
||||||
use futures_util::StreamExt;
|
|
||||||
use matrix_sdk::{
|
use matrix_sdk::{
|
||||||
config::SyncSettings,
|
config::SyncSettings,
|
||||||
ruma::{
|
ruma::{
|
||||||
api::client::message::send_message_event, events::room::message::RoomMessageEventContent,
|
api::client::message::send_message_event,
|
||||||
|
events::room::message::{
|
||||||
|
AddMentions, ForwardThread, MessageType, OriginalSyncRoomMessageEvent,
|
||||||
|
RoomMessageEventContent,
|
||||||
|
},
|
||||||
OwnedRoomId, TransactionId,
|
OwnedRoomId, TransactionId,
|
||||||
},
|
},
|
||||||
Client,
|
Client, Room, RoomState,
|
||||||
};
|
};
|
||||||
use tracing::{event, span, Level};
|
use tracing::{event, span, Level};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
@ -20,7 +23,7 @@ use crate::StopGetter;
|
||||||
|
|
||||||
pub async fn start_matrix(
|
pub async fn start_matrix(
|
||||||
config: &MatrixConfig,
|
config: &MatrixConfig,
|
||||||
mut stop_reciver: StopGetter,
|
stop_reciver: StopGetter,
|
||||||
) -> ClientResult<(), MatrixError> {
|
) -> ClientResult<(), MatrixError> {
|
||||||
let span = span!(Level::INFO, "Matrix Client");
|
let span = span!(Level::INFO, "Matrix Client");
|
||||||
let _enter = span.enter();
|
let _enter = span.enter();
|
||||||
|
@ -90,11 +93,9 @@ pub async fn start_matrix(
|
||||||
event!(Level::INFO, "未启用启动消息");
|
event!(Level::INFO, "未启用启动消息");
|
||||||
}
|
}
|
||||||
|
|
||||||
client.add_event_handler(events::on_room_message);
|
client.add_event_handler(on_room_message);
|
||||||
|
|
||||||
let init_sync_setting = SyncSettings::new().timeout(Duration::from_mins(10));
|
match client.sync_once(SyncSettings::new()).await {
|
||||||
|
|
||||||
match client.sync_once(init_sync_setting).await {
|
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
event!(Level::INFO, "Synced");
|
event!(Level::INFO, "Synced");
|
||||||
}
|
}
|
||||||
|
@ -104,37 +105,48 @@ pub async fn start_matrix(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut stream_sync =
|
client.sync(SyncSettings::default()).await?;
|
||||||
Box::pin(client.sync_stream(SyncSettings::new().timeout(Duration::from_mins(10))).await);
|
|
||||||
|
|
||||||
while let Some(Ok(response)) = stream_sync.next().await {
|
// while stop_reciver.await.is_err() {
|
||||||
for room in response.rooms.join.values() {
|
// event!(Level::INFO, "Matrix client is running");
|
||||||
for e in &room.timeline.events {
|
// tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||||
if let Ok(event) = e.event.deserialize() {
|
|
||||||
println!("Received event {:?}", event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if stop_reciver.try_recv().is_ok() {
|
|
||||||
event!(Level::INFO, "Matrix client stopping");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// loop {
|
|
||||||
// match stop_reciver.try_recv() {
|
|
||||||
// Ok(_) => {
|
|
||||||
// event!(Level::INFO, "Matrix client stopping");
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// Err(tokio::sync::oneshot::error::TryRecvError::Empty) => {
|
|
||||||
|
|
||||||
// }
|
|
||||||
// Err(tokio::sync::oneshot::error::TryRecvError::Closed) => {
|
|
||||||
// event!(Level::INFO, "Matrix client stopping");
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
event!(Level::INFO, "Matrix is not implemented yet");
|
||||||
|
stop_reciver.await.ok();
|
||||||
|
event!(Level::INFO, "Matrix client stopping");
|
||||||
|
// some stop
|
||||||
|
event!(Level::INFO, "Matrix client stopped");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) {
|
||||||
|
// We only want to listen to joined rooms.
|
||||||
|
if room.state() != RoomState::Joined {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We only want to log text messages.
|
||||||
|
let MessageType::Text(msgtype) = &event.content.msgtype else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 匹配消息
|
||||||
|
|
||||||
|
// /bot
|
||||||
|
if msgtype.body == "/bot" {
|
||||||
|
let pong = format!("shenbot v {}\nmatrix-rs v{}", crate::VERSION, crate::MATRIX_VERSION);
|
||||||
|
|
||||||
|
let reply = RoomMessageEventContent::text_plain(pong);
|
||||||
|
let reply = reply.make_reply_to(
|
||||||
|
&event.into_full_event(room.room_id().to_owned()),
|
||||||
|
ForwardThread::No,
|
||||||
|
AddMentions::No,
|
||||||
|
);
|
||||||
|
|
||||||
|
room.send(reply).await.expect("Failed to send message");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发给 Python 处理剩下的
|
||||||
|
}
|
||||||
|
|
|
@ -1,42 +1 @@
|
||||||
use matrix_sdk::{
|
|
||||||
ruma::events::room::message::{
|
|
||||||
AddMentions, ForwardThread, MessageType, OriginalSyncRoomMessageEvent,
|
|
||||||
RoomMessageEventContent,
|
|
||||||
},
|
|
||||||
Room, RoomState,
|
|
||||||
};
|
|
||||||
use tracing::{event, span, Level};
|
|
||||||
|
|
||||||
use crate::py::call::matrix_new_message_py;
|
|
||||||
|
|
||||||
pub async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) {
|
|
||||||
// We only want to listen to joined rooms.
|
|
||||||
if room.state() != RoomState::Joined {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We only want to log text messages.
|
|
||||||
let MessageType::Text(msgtype) = &event.content.msgtype else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
// 匹配消息
|
|
||||||
|
|
||||||
// /bot
|
|
||||||
if msgtype.body == "/bot" {
|
|
||||||
let pong = format!("shenbot v {}\nmatrix-rs v{}", crate::VERSION, crate::MATRIX_VERSION);
|
|
||||||
|
|
||||||
let reply = RoomMessageEventContent::text_plain(pong);
|
|
||||||
let reply = reply.make_reply_to(
|
|
||||||
&event.into_full_event(room.room_id().to_owned()),
|
|
||||||
ForwardThread::Yes,
|
|
||||||
AddMentions::No,
|
|
||||||
);
|
|
||||||
|
|
||||||
room.send(reply).await.expect("Failed to send message");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 发给 Python 处理剩下的
|
|
||||||
matrix_new_message_py().await;
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
pub mod ica;
|
pub mod ica;
|
||||||
pub mod matrix;
|
|
||||||
|
|
||||||
use pyo3::prelude::*;
|
use pyo3::prelude::*;
|
||||||
use toml::Value as TomlValue;
|
use toml::Value as TomlValue;
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
|
|
Loading…
Reference in New Issue
Block a user