修改了部分代码
This commit is contained in:
1 parent
d358ed3ddc
commit
47c3e908ec
6 files changed
+46
-16
No files matched your search
+20
-10
@@ -1,5 +1,6 @@
|
|||||||
use crate::bot::command_parser::parse_command;
|
use crate::bot::command_parser::parse_command;
|
||||||
use crate::bot::define::{AttendanceHandler, Bot, InboundMessage, MessageEnum, OutboundMessage};
|
use crate::bot::define::{AttendanceHandler, Bot, InboundMessage, MessageEnum, OutboundMessage};
|
||||||
|
use crate::bot::filters::support_chinese_command;
|
||||||
use crate::db::crud_handler;
|
use crate::db::crud_handler;
|
||||||
use anyhow::{Result, anyhow};
|
use anyhow::{Result, anyhow};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
@@ -23,7 +24,7 @@ fn global_channel() -> &'static (MessageTx, Mutex<Option<MessageRx>>) {
|
|||||||
pub struct ChannelManager {
|
pub struct ChannelManager {
|
||||||
tx: MessageTx,
|
tx: MessageTx,
|
||||||
rx: &'static Mutex<Option<MessageRx>>,
|
rx: &'static Mutex<Option<MessageRx>>,
|
||||||
filters: Option<Vec<Box<dyn Fn(&MessageEnum) -> MessageEnum + Send + Sync>>>,
|
filters: Option<Vec<Box<dyn Fn(&InboundMessage) -> InboundMessage + Send + Sync>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
//channel的包装,提供过滤功能,后续可以考虑增加更多功能,比如广播等
|
//channel的包装,提供过滤功能,后续可以考虑增加更多功能,比如广播等
|
||||||
@@ -31,14 +32,16 @@ impl ChannelManager {
|
|||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let (tx, rx) = global_channel();
|
let (tx, rx) = global_channel();
|
||||||
|
|
||||||
Self {
|
let mut _self = Self {
|
||||||
tx: tx.clone(),
|
tx: tx.clone(),
|
||||||
rx,
|
rx,
|
||||||
filters: None,
|
filters: None,
|
||||||
}
|
};
|
||||||
|
_self.reg_filters();
|
||||||
|
_self
|
||||||
}
|
}
|
||||||
|
|
||||||
fn filter_message(&self, msg: &MessageEnum) -> MessageEnum {
|
fn filter_message(&self, msg: &InboundMessage) -> InboundMessage {
|
||||||
let mut result_msg = msg.clone();
|
let mut result_msg = msg.clone();
|
||||||
if let Some(filters) = &self.filters {
|
if let Some(filters) = &self.filters {
|
||||||
for filter in filters {
|
for filter in filters {
|
||||||
@@ -47,15 +50,22 @@ impl ChannelManager {
|
|||||||
}
|
}
|
||||||
result_msg
|
result_msg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn reg_filters(&mut self) {
|
||||||
|
let cn_filter = Box::new(support_chinese_command)
|
||||||
|
as Box<dyn Fn(&InboundMessage) -> InboundMessage + Send + Sync>;
|
||||||
|
self.filters.get_or_insert_with(Vec::new).push(cn_filter);
|
||||||
|
//后续可以考虑增加更多过滤器
|
||||||
|
}
|
||||||
//这里先过滤,在发送,后续可以考虑在接收时过滤
|
//这里先过滤,在发送,后续可以考虑在接收时过滤
|
||||||
pub async fn send(&self, value: MessageEnum) -> Result<()> {
|
pub async fn send(&self, value: MessageEnum) -> Result<()> {
|
||||||
let tx = self.tx.clone();
|
let tx = self.tx.clone();
|
||||||
let msg = self.filter_message(&value);
|
if let MessageEnum::MatrixMessage(inbound_msg) = value {
|
||||||
|
let filtered_msg = self.filter_message(&inbound_msg);
|
||||||
tx.send(msg)
|
tx.send(MessageEnum::MatrixMessage(filtered_msg)).await?;
|
||||||
.await
|
} else {
|
||||||
.map_err(|e| anyhow!("Failed to send message: {}", e))?;
|
tx.send(value).await?;
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
use std::{sync::OnceLock, time::Duration};
|
|
||||||
|
|
||||||
use serde::{Deserialize, de::DeserializeOwned};
|
|
||||||
use tokio::task::JoinHandle;
|
use tokio::task::JoinHandle;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
// filters: Option<Vec<Box<dyn Fn(&MessageEnum) -> MessageEnum + Send + Sync>>>,
|
||||||
|
use crate::bot::define::InboundMessage;
|
||||||
|
|
||||||
|
//支持中文指令
|
||||||
|
pub fn support_chinese_command(msg: &InboundMessage) -> InboundMessage {
|
||||||
|
//支持中文指令的简单实现,可以根据需要扩展
|
||||||
|
let keywords = vec![("\n", ""), ("!", "!"), ("签到", "checkin")];
|
||||||
|
let mut new_msg = msg.clone();
|
||||||
|
for (cn, en) in keywords {
|
||||||
|
if new_msg.text.contains(cn) {
|
||||||
|
new_msg.text = new_msg.text.replace(cn, en);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
new_msg
|
||||||
|
}
|
||||||
|
|
||||||
|
//template
|
||||||
|
//pub fn support_chinese_command(msg: &InboundMessage) -> InboundMessage {
|
||||||
|
// unimplemented!()
|
||||||
|
//}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
pub mod bot_handler;
|
pub mod bot_handler;
|
||||||
pub mod command_parser;
|
pub mod command_parser;
|
||||||
pub mod define;
|
pub mod define;
|
||||||
|
pub mod filters;
|
||||||
pub mod matrix;
|
pub mod matrix;
|
||||||
pub mod schedule;
|
pub mod schedule;
|
||||||
+4
-3
@@ -1,18 +1,19 @@
|
|||||||
use anyhow::{Ok, Result};
|
use anyhow::{Ok, Result};
|
||||||
use tokio_cron_scheduler::{Job, JobScheduler};
|
|
||||||
pub struct ScheduleHandler {
|
pub struct ScheduleHandler {
|
||||||
//这里可以添加一些定时任务相关的字段,比如定时器,任务列表等
|
sche: tokio_cron_scheduler::JobScheduler, //这里可以添加一些定时任务相关的字段,比如定时器,任务列表等
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ScheduleHandler {
|
impl ScheduleHandler {
|
||||||
pub fn new() -> Self {
|
pub async fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
sche: tokio_cron_scheduler::JobScheduler::new().await.unwrap(),
|
||||||
//初始化字段
|
//初始化字段
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn start(&self) -> Result<()> {
|
pub async fn start(&self) -> Result<()> {
|
||||||
//这里可以启动定时任务,比如使用tokio的定时器,或者其他的定时任务库
|
//这里可以启动定时任务,比如使用tokio的定时器,或者其他的定时任务库
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
use matrix_sdk::ruma::{OwnedRoomId, OwnedUserId, UserId};
|
use matrix_sdk::ruma::{OwnedRoomId, OwnedUserId};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{path::Path, sync::OnceLock};
|
use std::{path::Path, sync::OnceLock};
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user