no requerir telegram para auto

This commit is contained in:
Cat /dev/Nulo 2024-01-25 16:56:27 -03:00
parent f7bc0a9db8
commit cce34571f1

View file

@ -296,11 +296,16 @@ async fn scrap_url(
} }
} }
#[derive(Clone)]
struct AutoTelegram {
token: String,
chat_id: String,
}
#[derive(Clone)] #[derive(Clone)]
struct Auto { struct Auto {
pool: Pool, pool: Pool,
telegram_token: String, telegram: Option<AutoTelegram>,
telegram_chat_id: String,
} }
impl Auto { impl Auto {
async fn download_supermercado(self: Self, supermercado: Supermercado) -> anyhow::Result<()> { async fn download_supermercado(self: Self, supermercado: Supermercado) -> anyhow::Result<()> {
@ -383,28 +388,35 @@ impl Auto {
async fn inform(self: &Self, msg: &str) { async fn inform(self: &Self, msg: &str) {
println!("{}", msg); println!("{}", msg);
if let Some(telegram) = &self.telegram {
let u = Url::parse_with_params( let u = Url::parse_with_params(
&format!( &format!("https://api.telegram.org/bot{}/sendMessage", telegram.token),
"https://api.telegram.org/bot{}/sendMessage",
self.telegram_token
),
&[ &[
("chat_id", self.telegram_chat_id.clone()), ("chat_id", telegram.chat_id.clone()),
("text", msg.to_string()), ("text", msg.to_string()),
], ],
) )
.unwrap(); .unwrap();
reqwest::get(u).await.unwrap(); reqwest::get(u).await.unwrap();
} }
}
} }
async fn auto_cli() -> anyhow::Result<()> { async fn auto_cli() -> anyhow::Result<()> {
let db = connect_db(); let db = connect_db();
let auto = Auto { let telegram = {
pool: db, match (
telegram_token: env::var("TELEGRAM_BOT_TOKEN")?, env::var("TELEGRAM_BOT_TOKEN"),
telegram_chat_id: env::var("TELEGRAM_BOT_CHAT_ID")?, env::var("TELEGRAM_BOT_CHAT_ID"),
) {
(Ok(token), Ok(chat_id)) => Some(AutoTelegram { token, chat_id }),
_ => {
tracing::warn!("No token or chat_id for telegram");
None
}
}
}; };
let auto = Auto { pool: db, telegram };
auto.inform("[auto] Empezando scrap").await; auto.inform("[auto] Empezando scrap").await;
let handles: Vec<_> = Supermercado::value_variants() let handles: Vec<_> = Supermercado::value_variants()
.iter() .iter()