Telegram markdown support for structured json messages

This commit is contained in:
euzu
2025-10-22 11:09:43 +02:00
parent 86d8409391
commit 8245bc4c85
14 changed files with 160 additions and 48 deletions
+18 -16
View File
@@ -52,24 +52,26 @@ pub fn process_group_watch(client: &Arc<reqwest::Client>, cfg: &Config, target_n
}
}
#[derive(Debug, serde::Serialize)]
struct WatchChanges {
pub target: String,
pub group: String,
pub added: Vec<String>,
pub removed: Vec<String>,
}
fn handle_watch_notification(client: &Arc<reqwest::Client>, cfg: &Config, added: &BTreeSet<String>, removed: &BTreeSet<String>, target_name: &str, group_name: &str) {
let added_entries = added.iter().map(std::string::ToString::to_string).collect::<Vec<String>>().join("\n\t");
let removed_entries = removed.iter().map(std::string::ToString::to_string).collect::<Vec<String>>().join("\n\t");
let added = added.iter().map(std::string::ToString::to_string).collect::<Vec<String>>();
let removed = removed.iter().map(std::string::ToString::to_string).collect::<Vec<String>>();
if !added.is_empty() || !removed.is_empty() {
let changes = WatchChanges {
target: target_name.to_string(),
group: group_name.to_string(),
added,
removed
};
let mut message = vec![];
if !added_entries.is_empty() {
message.push("added: [\n\t".to_string());
message.push(added_entries);
message.push("\n]\n".to_string());
}
if !removed_entries.is_empty() {
message.push("removed: [\n\t".to_string());
message.push(removed_entries);
message.push("\n]\n".to_string());
}
if !message.is_empty() {
let msg = format!("Changes {}/{}\n{}", target_name, group_name, message.join(""));
let msg = serde_json::to_string_pretty(&changes).unwrap_or_else(|_| "Error: Failed to serialize watch changes".to_string());
info!("{}", &msg);
send_message(client, &MsgKind::Watch, cfg.messaging.as_ref(), &msg);
}
+9 -7
View File
@@ -9,7 +9,7 @@ use std::sync::Arc;
use std::thread;
use tokio::sync::Mutex;
use crate::messaging::send_message;
use crate::messaging::{send_message_json};
use crate::model::Epg;
use crate::model::{ConfigTarget, ProcessTargets};
use crate::model::{Mapping};
@@ -376,8 +376,9 @@ async fn process_sources(client: Arc<reqwest::Client>, config: &Arc<AppConfig>,
let (input_stats, target_stats, mut res_errors) =
process_source(Arc::clone(&http_client), cfg, index, usr_trgts, event_manager, playlist_state.as_ref()).await;
shared_errors.lock().await.append(&mut res_errors);
let process_stats = SourceStats::new(input_stats, target_stats);
shared_stats.lock().await.push(process_stats);
if let Some(process_stats) = SourceStats::new(input_stats, target_stats) {
shared_stats.lock().await.push(process_stats);
}
});
},
Err(err) => error!("Could not create runtime !!! {err}"),
@@ -391,8 +392,9 @@ async fn process_sources(client: Arc<reqwest::Client>, config: &Arc<AppConfig>,
let (input_stats, target_stats, mut res_errors) =
process_source(Arc::clone(&client), cfg, index, usr_trgts, event_manager, playlist_state).await;
shared_errors.lock().await.append(&mut res_errors);
let process_stats = SourceStats::new(input_stats, target_stats);
shared_stats.lock().await.push(process_stats);
if let Some(process_stats) = SourceStats::new(input_stats, target_stats) {
shared_stats.lock().await.push(process_stats);
}
}
drop(update_lock);
}
@@ -607,7 +609,7 @@ pub async fn exec_processing(client: Arc<reqwest::Client>, app_config: Arc<AppCo
// print stats
info!("{stats_msg}");
// send stats
send_message(&client, &MsgKind::Stats, messaging, stats_msg.as_str());
send_message_json(&client, &MsgKind::Stats, messaging, stats_msg.as_str());
}
// send errors
if let Some(message) = get_errors_notify_message!(errors, 255) {
@@ -615,7 +617,7 @@ pub async fn exec_processing(client: Arc<reqwest::Client>, app_config: Arc<AppCo
events.send_event(EventMessage::PlaylistUpdate(PlaylistUpdateState::Failure));
}
if let Ok(error_msg) = serde_json::to_string(&serde_json::Value::Object(serde_json::map::Map::from_iter([("errors".to_string(), serde_json::Value::String(message))]))) {
send_message(&client, &MsgKind::Error, messaging, error_msg.as_str());
send_message_json(&client, &MsgKind::Error, messaging, error_msg.as_str());
}
} else if let Some(events) = event_manager {
events.send_event(EventMessage::PlaylistUpdate(PlaylistUpdateState::Success));