-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
1,118 additions
and
56 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::rpt::RepeaterStates; | ||
use once_cell::sync::Lazy; | ||
use std::sync::Arc; | ||
use tokio::sync::Mutex; | ||
use url_track_cleaner::UrlTrackCleaner; | ||
|
||
#[derive(Clone, Debug)] | ||
pub(crate) struct BotApp { | ||
pub repeater_state: Arc<RepeaterStates>, | ||
pub url_track_cleaner: Arc<UrlTrackCleaner>, | ||
} | ||
|
||
impl BotApp { | ||
pub fn new(states: RepeaterStates, cleaner: UrlTrackCleaner) -> Self { | ||
BotApp { | ||
repeater_state: Arc::new(states), | ||
url_track_cleaner: Arc::new(cleaner), | ||
} | ||
} | ||
} | ||
|
||
impl Default for BotApp { | ||
fn default() -> Self { | ||
BotApp { | ||
repeater_state: Arc::new(RepeaterStates::default()), | ||
url_track_cleaner: Arc::new(UrlTrackCleaner::default()), | ||
} | ||
} | ||
} | ||
|
||
static APP: Lazy<Mutex<BotApp>> = Lazy::new(|| Mutex::new(BotApp::default())); | ||
|
||
/// Set the global bot app instance | ||
/// | ||
/// This function can only be called once when program starts | ||
pub(crate) async fn set(app: BotApp) { | ||
APP.lock().await.clone_from(&app); | ||
} | ||
|
||
/// Get the global bot app instance | ||
pub(crate) async fn get() -> BotApp { | ||
APP.lock().await.clone() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,31 @@ | ||
use teloxide::macros::BotCommands; | ||
use teloxide::prelude::*; | ||
use teloxide::utils::command::{BotCommands, ParseError}; | ||
|
||
#[derive(BotCommands, Clone)] | ||
#[command(rename_rule = "lowercase", description = "支持以下命令:")] | ||
#[command(rename_rule = "snake_case", description = "支持以下命令:")] | ||
pub(crate) enum Command { | ||
#[command(description = "查看机器人使用说明")] | ||
Help, | ||
#[command(description = "清理 URL", parse_with = CleanUrlCommand::parse_to_command)] | ||
CleanUrl(CleanUrlCommand), | ||
} | ||
|
||
#[derive(Clone)] | ||
pub(crate) struct CleanUrlCommand { | ||
pub url: String, | ||
} | ||
|
||
impl CleanUrlCommand { | ||
fn parse_to_command(s: String) -> Result<(Self,), ParseError> { | ||
Ok((CleanUrlCommand { | ||
url: s.trim().to_string(), | ||
},)) | ||
} | ||
} | ||
|
||
pub(crate) async fn handle_help_cmd(bot: Bot, msg: Message, _: Command) -> ResponseResult<()> { | ||
bot.send_message(msg.chat.id, "我的主人很懒!!!!不如直接问问他怎么用") | ||
let descriptions = Command::descriptions(); | ||
bot.send_message(msg.chat.id, format!("{}", descriptions)) | ||
.await?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use crate::commands::CleanUrlCommand; | ||
use crate::{app, msgfmt}; | ||
use log::{debug, error}; | ||
use teloxide::prelude::*; | ||
use teloxide::types::ParseMode; | ||
use teloxide::utils::markdown; | ||
use teloxide::Bot; | ||
use url_track_cleaner::{RedirectPolicy, ReserveRule, UrlTrackCleanerBuilder}; | ||
|
||
/// 生成默认的 URL 清理器 | ||
pub(crate) fn default_cleaner() -> url_track_cleaner::UrlTrackCleaner { | ||
UrlTrackCleanerBuilder::new() | ||
.follow_redirect(RedirectPolicy::All) | ||
.reserve_rules(vec![ReserveRule::new_with_regex( | ||
r#"^http(s)?://www.bilibili.com/.*"#, | ||
vec!["t".to_string()], | ||
) | ||
.expect("failed to create reserve rule")]) | ||
.build() | ||
} | ||
|
||
/// 处理 `CleanUrl` (/clean_url)命令 | ||
pub(crate) async fn handle_clean_url_cmd( | ||
bot: Bot, | ||
msg: Message, | ||
cmd: CleanUrlCommand, | ||
) -> ResponseResult<()> { | ||
if cmd.url.is_empty() { | ||
bot.send_message(msg.chat.id, "URL 不能为空") | ||
.reply_to_message_id(msg.id) | ||
.await?; | ||
return Ok(()); | ||
} | ||
let cleaner = app::get().await.url_track_cleaner; | ||
match cleaner.do_clean(&cmd.url).await { | ||
Ok(res) => { | ||
let res = res.as_str().strip_suffix("?").unwrap_or(""); | ||
debug!("cleaned url: {} (original={})", res, cmd.url); | ||
bot.send_message( | ||
msg.chat.id, | ||
format!("清理后的 URL:{}", msgfmt::plain_link(res)), | ||
) | ||
} | ||
Err(e) => { | ||
let err = e.to_string(); | ||
error!("failed to clean url: {}", err); | ||
bot.send_message( | ||
msg.chat.id, | ||
format!("清理 URL 时出现错误:{}", markdown::code_block(&err)), | ||
) | ||
} | ||
} | ||
.parse_mode(ParseMode::MarkdownV2) | ||
.reply_to_message_id(msg.id) | ||
.await?; | ||
Ok(()) | ||
} |