Reworked telegram notifications
Build and Publish / Build and Publish Docker Image (push) Successful in 1m50s
Build and Publish / Build and Publish Docker Image (push) Successful in 1m50s
This commit is contained in:
+28
-14
@@ -1,8 +1,8 @@
|
||||
use cot::db::{Database, query};
|
||||
|
||||
use crate::models::Setting;
|
||||
use crate::models::{Setting, User};
|
||||
|
||||
/// Send a Telegram message using bot settings from DB.
|
||||
/// Send a Telegram notification to all admins with notifications enabled.
|
||||
/// Silently ignores errors (missing config, network issues) — notifications are best-effort.
|
||||
pub async fn notify_new_lead(
|
||||
db: &Database,
|
||||
@@ -14,10 +14,6 @@ pub async fn notify_new_lead(
|
||||
Some(t) if !t.is_empty() => t,
|
||||
_ => return,
|
||||
};
|
||||
let chat_id = match get_setting(db, "telegram_chat_id").await {
|
||||
Some(c) if !c.is_empty() => c,
|
||||
_ => return,
|
||||
};
|
||||
|
||||
let mut text = format!("📋 Новая заявка!\n\nИмя: {name}");
|
||||
if let Some(phone) = phone.filter(|s| !s.is_empty()) {
|
||||
@@ -27,15 +23,33 @@ pub async fn notify_new_lead(
|
||||
text.push_str(&format!("\nКомментарий: {comment}"));
|
||||
}
|
||||
|
||||
let active = "active".to_string();
|
||||
let users = match query!(User, $status == active).all(db).await {
|
||||
Ok(u) => u,
|
||||
Err(_) => return,
|
||||
};
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("https://api.telegram.org/bot{token}/sendMessage");
|
||||
let _ = reqwest::Client::new()
|
||||
.post(&url)
|
||||
.json(&serde_json::json!({
|
||||
"chat_id": chat_id,
|
||||
"text": text,
|
||||
}))
|
||||
.send()
|
||||
.await;
|
||||
|
||||
for user in &users {
|
||||
if user.telegram_notifications != Some(true) {
|
||||
continue;
|
||||
}
|
||||
let chat_id = match &user.telegram_chat_id {
|
||||
Some(id) if !id.is_empty() => id,
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
let _ = client
|
||||
.post(&url)
|
||||
.json(&serde_json::json!({
|
||||
"chat_id": chat_id,
|
||||
"text": text,
|
||||
}))
|
||||
.send()
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_setting(db: &Database, key_name: &str) -> Option<String> {
|
||||
|
||||
Reference in New Issue
Block a user