mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Delete bots if wrong token
This commit is contained in:
@@ -30,3 +30,18 @@ pub async fn get_bots() -> Result<Vec<BotData>, reqwest::Error> {
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub async fn delete_bot(id: u32) -> Result<(), reqwest::Error> {
|
||||
let client = reqwest::Client::new();
|
||||
let response = client
|
||||
.delete(&format!("{}/{}/", config::CONFIG.manager_url, id))
|
||||
.header("Authorization", &config::CONFIG.manager_api_key)
|
||||
.send()
|
||||
.await;
|
||||
|
||||
match response {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ use url::Url;
|
||||
|
||||
use std::convert::Infallible;
|
||||
|
||||
use crate::bots_manager::bot_manager_client::delete_bot;
|
||||
use crate::bots_manager::BOTS_ROUTES;
|
||||
use crate::config;
|
||||
|
||||
@@ -64,6 +65,18 @@ pub async fn set_webhook(bot_data: &BotData) -> bool {
|
||||
match bot.set_webhook(url.clone()).await {
|
||||
Ok(_) => true,
|
||||
Err(err) => {
|
||||
match err {
|
||||
teloxide::RequestError::Api(ref err) => {
|
||||
match err {
|
||||
teloxide::ApiError::NotFound => {
|
||||
let _ = delete_bot(bot_data.id).await;
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
},
|
||||
_ => ()
|
||||
};
|
||||
|
||||
log::error!("Webhook set error: {}", err);
|
||||
false
|
||||
}
|
||||
|
||||
@@ -92,17 +92,19 @@ impl BotsManager {
|
||||
continue;
|
||||
}
|
||||
|
||||
BOTS_DATA
|
||||
.insert(bot_data.token.clone(), bot_data.clone())
|
||||
.await;
|
||||
|
||||
let bot_data = bot_data.clone();
|
||||
let bot_data: BotData = bot_data.clone();
|
||||
|
||||
let semphore = semaphore.clone();
|
||||
set_webhook_tasks.spawn(async move {
|
||||
let _permit = semphore.acquire().await.unwrap();
|
||||
|
||||
set_webhook(&bot_data).await;
|
||||
let webhook_status = set_webhook(&bot_data).await;
|
||||
|
||||
if webhook_status {
|
||||
BOTS_DATA
|
||||
.insert(bot_data.token.clone(), bot_data.clone())
|
||||
.await;
|
||||
}
|
||||
|
||||
drop(_permit);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user