use std::sync::Arc;
pub type OwnerAlert = Arc<dyn Fn(String) + Send + Sync>;
#[cfg(not(feature = "telegram"))]
pub(crate) fn no_alert() -> OwnerAlert {
Arc::new(|_| {})
}
#[cfg(feature = "telegram")]
pub(crate) fn telegram_owner_alert(
telegram: Arc<crate::channels::telegram::TelegramState>,
) -> OwnerAlert {
Arc::new(move |text: String| {
let telegram = telegram.clone();
tokio::spawn(async move {
use teloxide::prelude::Requester;
let (Some(bot), Some(owner)) = (telegram.bot().await, telegram.owner_chat_id().await)
else {
tracing::warn!(
"owner alert not sent (Telegram not connected or no owner chat yet): {text}"
);
return;
};
if let Err(e) = bot
.send_message(teloxide::types::ChatId(owner), text.clone())
.await
{
tracing::error!("owner alert failed over Telegram: {e}; alert was: {text}");
}
});
})
}