diff --git a/CHANGELOG.md b/CHANGELOG.md index a853ed5a9..a8318696d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,11 +5,42 @@ * Added api-proxy config for xtream proxy, to define server info and user credentials * Added Xtream Api Endpoints. * Added multiple input support +* Added Messaging with opt in message types [info, error, stats] * Added Telegram message support * Added Target watch for groups * Fixed TLS problem with docker scratch * Added simple stats +Changes in `config.yml` +```yaml +messaging: + notify_on: + - error + - info + - stats + telegram: + bot_token: '' + chat_ids: + - +schedule: '0 0 0,8,18 * * * *' +``` + +`api-proxy.yml` +```yaml +server: + protocol: http + ip: 192.168.9.3 + http_port: 80 + https_port: + rtmp_port: + timezone: Europe/Paris + message: Welcome to m3u-filter +user: + - target: pl1 + credentials: + - {username: x3452, password: ztrhgrGZrt83hjerter} +``` + # v1.0.1(2023-09-07) * Refactored sorting. Sorting channels inside group now possible diff --git a/api-proxy.yml b/api-proxy.yml index fb47b469a..80e4aa92f 100644 --- a/api-proxy.yml +++ b/api-proxy.yml @@ -1,6 +1,6 @@ server: protocol: http - ip: 102.168.9.3 + ip: 192.168.9.3 http_port: 80 https_port: rtmp_port: diff --git a/config.yml b/config.yml index 677dd9d52..cdc244eba 100644 --- a/config.yml +++ b/config.yml @@ -3,6 +3,15 @@ threads: 0 working_dir: /home/m3u-filter/data # sec min hour day of month month day of week year schedule: "0 0 8,12,16,20,22,1 * * * *" +messaging: + notify_on: + - error + - info + - stats + telegram: + bot_token: '' + chat_ids: + - video_suffix: - .mkv - .mp4 diff --git a/src/messaging.rs b/src/messaging.rs index 58dbd8fc1..54bd3ee42 100644 --- a/src/messaging.rs +++ b/src/messaging.rs @@ -1,17 +1,35 @@ use log::{debug, error}; use crate::model::config::{MessagingConfig}; -pub(crate) fn send_message(cfg: &Option, msg: &str) { +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)] +pub(crate) enum MsgKind { + #[serde(rename = "info")] + Info, + #[serde(rename = "stats")] + Stats, + #[serde(rename = "error")] + Error, +} + + +fn is_enabled(kind: &MsgKind, cfg: &MessagingConfig) -> bool { + cfg.notify_on.contains(kind) +} + +pub(crate) fn send_message(kind: &MsgKind, cfg: &Option, msg: &str) { if let Some(messaging) = cfg { - if let Some(telegram) = &messaging.telegram { - for chat_id in &telegram.chat_ids { - let bot = rustelebot::create_instance(&telegram.bot_token, chat_id); - match rustelebot::send_message(&bot, msg, None) - { - Ok(_) => debug!("Text message sent successfully to {}", chat_id), - Err(e) => error!("Text message wasn't sent to {} because of: {}", chat_id, e) - } - }; + if is_enabled(kind, messaging) { + if let Some(telegram) = &messaging.telegram { + for chat_id in &telegram.chat_ids { + let bot = rustelebot::create_instance(&telegram.bot_token, chat_id); + match rustelebot::send_message(&bot, msg, None) + { + Ok(_) => debug!("Text message sent successfully to {}", chat_id), + Err(e) => error!("Text message wasn't sent to {} because of: {}", chat_id, e) + } + }; + } } } } + diff --git a/src/model/config.rs b/src/model/config.rs index 75bcf03cd..d1462ab3e 100644 --- a/src/model/config.rs +++ b/src/model/config.rs @@ -1,6 +1,5 @@ use std::collections::{HashMap, HashSet}; use path_absolutize::*; -use enum_iterator::Sequence; use log::{debug, error}; use crate::filter::{Filter, get_filter, MockValueProcessor, PatternTemplate, prepare_templates, ValueProvider}; @@ -9,6 +8,7 @@ use crate::model::mapping::Mapping; use crate::model::model_config::{ItemField, ProcessingOrder, SortOrder, TargetType, default_as_zero, default_as_false, default_as_true}; use crate::{utils}; use crate::m3u_filter_error::{M3uFilterError, M3uFilterErrorKind}; +use crate::messaging::MsgKind; use crate::model::api_proxy::ApiProxyConfig; use crate::utils::get_working_path; @@ -216,7 +216,7 @@ pub(crate) struct InputAffix { pub value: String, } -#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Sequence)] +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub(crate) enum InputType { #[serde(rename = "m3u")] M3u, @@ -312,8 +312,13 @@ pub(crate) struct TelegramMessagingConfig { pub chat_ids: Vec, } +fn default_as_empty_list() -> Vec { vec![] } + #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub(crate) struct MessagingConfig { + + #[serde(default = "default_as_empty_list")] + pub notify_on: Vec, pub telegram: Option, } diff --git a/src/processing/playlist_processor.rs b/src/processing/playlist_processor.rs index d7684e643..df6b008b9 100644 --- a/src/processing/playlist_processor.rs +++ b/src/processing/playlist_processor.rs @@ -12,7 +12,7 @@ use crate::{Config, create_m3u_filter_error_result, get_errors_notify_message, m use crate::download::{get_m3u_playlist, get_xtream_playlist}; use crate::filter::{get_field_value, MockValueProcessor, set_field_value, ValueProvider}; use crate::m3u_filter_error::{M3uFilterError, M3uFilterErrorKind}; -use crate::messaging::send_message; +use crate::messaging::{MsgKind, send_message}; use crate::model::config::{ConfigTarget, default_as_default, InputAffix, InputType, ProcessTargets}; use crate::model::mapping::{Mapping, MappingValueProcessor}; use crate::model::model_config::{AFFIX_FIELDS, ItemField, ProcessingOrder, SortOrder::{Asc, Desc}}; @@ -466,10 +466,14 @@ pub(crate) fn process_playlist(playlists: &mut [FetchedPlaylist], pub(crate) fn start_processing(cfg: Arc, targets: Arc) { let (stats, errors) = process_sources(cfg.clone(), targets.clone()); let stats_msg = format!("Stats: {}", stats.iter().map(|stat| stat.to_string()).collect::>().join("\n")); + // print stats info!("{}", stats_msg); - send_message(&cfg.messaging, stats_msg.as_str()); + // send stats + send_message(&MsgKind::Stats, &cfg.messaging, stats_msg.as_str()); + // log errors errors.iter().for_each(|err| error!("{}", err.message)); + // send errors if let Some(message) = get_errors_notify_message!(errors, 255) { - send_message(&cfg.messaging, message.as_str()); + send_message(&MsgKind::Error,&cfg.messaging, message.as_str()); } } \ No newline at end of file diff --git a/src/processing/playlist_watch.rs b/src/processing/playlist_watch.rs index fbb526a7d..0bc9326fc 100644 --- a/src/processing/playlist_watch.rs +++ b/src/processing/playlist_watch.rs @@ -2,7 +2,7 @@ use std::collections::BTreeSet; use std::path::PathBuf; use log::{error, info}; use regex::Regex; -use crate::messaging::send_message; +use crate::messaging::{MsgKind, send_message}; use crate::model::config::Config; use crate::model::model_m3u::PlaylistGroup; use crate::utils; @@ -68,7 +68,7 @@ fn handle_watch_notification(cfg: &Config, added: BTreeSet, removed: BTr if !message.is_empty() { let msg = format!("Changes {}/{}\n{}", target_name, group_name, message.join("")); info!("{}", &msg); - send_message(&cfg.messaging, &msg); + send_message(&MsgKind::Info, &cfg.messaging, &msg); } }