修改了部分bot代码
This commit is contained in:
1 parent
821168a770
commit
bbbdd9eac5
10 files changed
+164
-19
No files matched your search
+29
-4
@@ -1,4 +1,5 @@
|
||||
use crate::bot::define::{AttendanceBot, MessageEnum};
|
||||
use crate::bot::command_parser::parse_command;
|
||||
use crate::bot::define::{AttendanceHandler, MessageEnum};
|
||||
use anyhow::{Result, anyhow};
|
||||
use async_trait::async_trait;
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
@@ -17,9 +18,9 @@ fn global_channel() -> &'static (MessageTx, Mutex<Option<MessageRx>>) {
|
||||
})
|
||||
}
|
||||
|
||||
pub struct Bot;
|
||||
pub struct CommandHandler;
|
||||
|
||||
impl Bot {
|
||||
impl CommandHandler {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
@@ -29,8 +30,32 @@ impl Bot {
|
||||
}
|
||||
}
|
||||
|
||||
async fn bot_command_processor(command: AsRef<str>) -> Result<()> {
|
||||
let command = command.as_ref();
|
||||
|
||||
let ast = parse_command(command)?;
|
||||
|
||||
let command = ast.command.as_str();
|
||||
match command {
|
||||
"!echo" => {}
|
||||
_ => {
|
||||
return Err(anyhow!("unknown command: {}", command));
|
||||
}
|
||||
}
|
||||
let arguments = ast.arguments;
|
||||
let options = ast.options;
|
||||
for argument in arguments {
|
||||
log::trace!("Argument: {}", argument);
|
||||
}
|
||||
|
||||
for (k, v) in options {
|
||||
log::trace!("Option: {}={}", k, v);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl AttendanceBot for Bot {
|
||||
impl AttendanceHandler for CommandHandler {
|
||||
async fn handle_commands(&self) -> Result<JoinHandle<()>> {
|
||||
let rx = {
|
||||
let (_tx, rx_slot) = global_channel();
|
||||
|
||||
+7
-2
@@ -1,8 +1,9 @@
|
||||
use std::{sync::OnceLock, time::Duration};
|
||||
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
|
||||
#[async_trait]
|
||||
pub trait Sender {
|
||||
async fn send(&self, message: OutboundMessage) -> Result<String>;
|
||||
@@ -36,7 +37,7 @@ pub struct InputBatch {
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait AttendanceBot {
|
||||
pub trait AttendanceHandler {
|
||||
async fn handle_commands(&self) -> Result<JoinHandle<()>>;
|
||||
}
|
||||
|
||||
@@ -45,3 +46,7 @@ pub enum MessageEnum {
|
||||
TelegramMessage(InboundMessage),
|
||||
StopMessage,
|
||||
}
|
||||
#[async_trait]
|
||||
pub trait Bot {
|
||||
async fn start(&self) -> Result<()>;
|
||||
}
|
||||
+65
-2
@@ -1,3 +1,66 @@
|
||||
struct MatrixBot {}
|
||||
use std::{sync::Arc, time::Duration};
|
||||
|
||||
impl MatrixBot {}
|
||||
use matrix_sdk::{
|
||||
Client,
|
||||
config::SyncSettings,
|
||||
ruma::{OwnedUserId, UserId, events::room::message::SyncRoomMessageEvent, user_id},
|
||||
};
|
||||
|
||||
use crate::bot::define::Bot;
|
||||
|
||||
struct MatrixBot {
|
||||
client: Arc<Client>,
|
||||
user_id: OwnedUserId,
|
||||
password: String,
|
||||
}
|
||||
|
||||
impl MatrixBot {
|
||||
pub async fn new(user_id: &str, password: &str) -> Self {
|
||||
let user_id = UserId::parse(user_id).expect("用户ID不合法");
|
||||
Self {
|
||||
client: Arc::new(
|
||||
Client::builder()
|
||||
.server_name(user_id.server_name())
|
||||
.build()
|
||||
.await
|
||||
.expect("创建Matrix客户端失败"),
|
||||
),
|
||||
user_id: user_id.to_owned(),
|
||||
password: password.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Bot for MatrixBot {
|
||||
async fn start(&self) -> anyhow::Result<()> {
|
||||
let client = Arc::clone(&self.client);
|
||||
log::info!("Matrix 机器人已启动 {}", self.user_id);
|
||||
tokio::spawn(tokio_retry::Retry::spawn(
|
||||
tokio_retry::strategy::FibonacciBackoff::from_millis(100)
|
||||
.max_delay(Duration::from_secs(120)),
|
||||
move || {
|
||||
let client = Arc::clone(&client);
|
||||
async move { client.sync(SyncSettings::default()).await }
|
||||
},
|
||||
));
|
||||
log::info!("Matrix 机器人已登陆 {}", self.user_id);
|
||||
dbg!("Matrix 机器人已登陆 {}", &self.user_id);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_matrix_bot() {
|
||||
let userid_str = std::env::var("MATRIX_USER").expect("请设置环境变量 MATRIX_USER");
|
||||
let userid = UserId::parse(&userid_str).expect("用户ID不合法");
|
||||
let password = std::env::var("MATRIX_PASSWORD").expect("请设置环境变量 MATRIX_PASSWORD");
|
||||
let bot = MatrixBot::new(userid.as_ref(), &password).await;
|
||||
bot.start().await.unwrap();
|
||||
tokio::signal::ctrl_c().await.unwrap();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user