Compare commits
2
Commits
275ac9d609
...
5586bd78a4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5586bd78a4 | ||
|
|
743d0b5d3d |
No files matched your search
Generated
+2962
-23
File diff suppressed because it is too large.
Load diff
@@ -6,8 +6,14 @@ edition = "2024"
|
||||
[dependencies]
|
||||
anyhow = "1.0.102"
|
||||
async-trait = "0.1.89"
|
||||
env_logger = "0.11.9"
|
||||
reqwest = "0.13.2"
|
||||
serde = {version="1.0.228",features=["derive"]}
|
||||
sled = "0.34.7"
|
||||
tokio = {version="1.50.0",features=["full"]}
|
||||
toml = "1.0.7"
|
||||
|
||||
[dependencies.matrix-sdk]
|
||||
version="0.16.0"
|
||||
default-features = false
|
||||
features=["anyhow","markdown","e2e-encryption","automatic-room-key-forwarding","sqlite","rustls-tls"]
|
||||
@@ -0,0 +1,61 @@
|
||||
use crate::bot::define::{AttendanceBot, MessageEnum};
|
||||
use anyhow::{Result, anyhow};
|
||||
use async_trait::async_trait;
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
type MessageTx = mpsc::Sender<MessageEnum>;
|
||||
type MessageRx = mpsc::Receiver<MessageEnum>;
|
||||
|
||||
static GLOBAL_CHANNEL: OnceLock<(MessageTx, Mutex<Option<MessageRx>>)> = OnceLock::new();
|
||||
|
||||
fn global_channel() -> &'static (MessageTx, Mutex<Option<MessageRx>>) {
|
||||
GLOBAL_CHANNEL.get_or_init(|| {
|
||||
let (tx, rx) = mpsc::channel(100);
|
||||
(tx, Mutex::new(Some(rx)))
|
||||
})
|
||||
}
|
||||
|
||||
pub struct Bot;
|
||||
|
||||
impl Bot {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
|
||||
pub fn global_sender() -> MessageTx {
|
||||
global_channel().0.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl AttendanceBot for Bot {
|
||||
async fn handle_commands(&self) -> Result<JoinHandle<()>> {
|
||||
let rx = {
|
||||
let (_tx, rx_slot) = global_channel();
|
||||
rx_slot
|
||||
.lock()
|
||||
.map_err(|_| anyhow!("global receiver lock poisoned"))?
|
||||
.take()
|
||||
.ok_or_else(|| anyhow!("global receiver has already been taken"))?
|
||||
};
|
||||
|
||||
let mut rx = rx;
|
||||
let handle = tokio::spawn(async move {
|
||||
while let Some(msg) = rx.recv().await {
|
||||
match msg {
|
||||
MessageEnum::StopMessage => break,
|
||||
MessageEnum::MatrixMessage(_msg) => {
|
||||
// TODO: route matrix message to command processor.
|
||||
}
|
||||
MessageEnum::TelegramMessage(_msg) => {
|
||||
// TODO: route telegram message to command processor.
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(handle)
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
|
||||
@@ -8,8 +10,7 @@ pub trait Sender {
|
||||
|
||||
#[async_trait]
|
||||
pub trait Receiver {
|
||||
// Pull user inputs from upstream and return a cursor for incremental sync.
|
||||
async fn poll_inputs(&self, cursor: Option<&str>, limit: usize) -> Result<InputBatch>;
|
||||
async fn recv(&self, msg: InboundMessage) -> Result<()>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -33,3 +34,14 @@ pub struct InputBatch {
|
||||
pub messages: Vec<InboundMessage>,
|
||||
pub next_cursor: Option<String>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait AttendanceBot {
|
||||
async fn handle_commands(&self) -> Result<JoinHandle<()>>;
|
||||
}
|
||||
|
||||
pub enum MessageEnum {
|
||||
MatrixMessage(InboundMessage),
|
||||
TelegramMessage(InboundMessage),
|
||||
StopMessage,
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
struct MatrixBot {}
|
||||
|
||||
impl MatrixBot {}
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod bot_handler;
|
||||
pub mod define;
|
||||
pub mod matrix;
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
use crate::config::get_config;
|
||||
use anyhow::{Ok, Result};
|
||||
pub async fn init() -> Result<()> {
|
||||
let _ = get_config();//初始化日志系统
|
||||
get_config(); //初始化配置
|
||||
env_logger::init(); //初始化日志
|
||||
Ok(())
|
||||
}
|
||||
+1
-1
@@ -2,4 +2,4 @@ pub mod api;
|
||||
pub mod config;
|
||||
pub mod db;
|
||||
pub mod init;
|
||||
pub mod sender;
|
||||
pub mod bot;
|
||||
Whitespace-only changes.
Reference in new issue
Block a user