From 62950d70ed4515b9dc96b6508624b3736e663203 Mon Sep 17 00:00:00 2001 From: Osh Date: Wed, 20 Jan 2021 23:54:16 +0200 Subject: [PATCH 1/7] Update snapraid-runner.conf.example Changed conf file --- snapraid-runner.conf.example | 2 ++ 1 file changed, 2 insertions(+) diff --git a/snapraid-runner.conf.example b/snapraid-runner.conf.example index ed64810..9b1b49f 100644 --- a/snapraid-runner.conf.example +++ b/snapraid-runner.conf.example @@ -7,6 +7,8 @@ config = snapraid.conf deletethreshold = 40 ; if you want touch to be ran each time touch = false +; report mode (telegram, mail) +report = telegram [logging] ; logfile to write to, leave empty to disable From b281de5fcf4e45356fa0287eba660a22c422ec81 Mon Sep 17 00:00:00 2001 From: Osh Date: Thu, 21 Jan 2021 22:40:34 +0200 Subject: [PATCH 2/7] Update snapraid-runner.conf.example Add bot parameters to conf file --- snapraid-runner.conf.example | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/snapraid-runner.conf.example b/snapraid-runner.conf.example index 9b1b49f..25f9581 100644 --- a/snapraid-runner.conf.example +++ b/snapraid-runner.conf.example @@ -27,6 +27,10 @@ to = ; maximum email size in KiB maxsize = 500 +[telegram] +bot_token = 1307164393:AAEvw7Oe8hdNlRs3pRDTwfNMGgCHFIOZ1uo +bot_chatID = 44713803 + [smtp] host = ; leave empty for default port From bbaffec7909013bec98e17098a7a10cdc785614a Mon Sep 17 00:00:00 2001 From: Osh Date: Thu, 21 Jan 2021 23:00:24 +0200 Subject: [PATCH 3/7] Update snapraid-runner.py Added send_telegram and modified conf reader --- snapraid-runner.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/snapraid-runner.py b/snapraid-runner.py index d7051f1..1cb2bad 100644 --- a/snapraid-runner.py +++ b/snapraid-runner.py @@ -71,6 +71,18 @@ def snapraid_command(command, args={}, *, allow_statuscodes=[]): else: raise subprocess.CalledProcessError(ret, "snapraid " + command) +def send_telegram(success): + import requests + + if success: + msg = "SnapRAID job completed successfully:\n" + else: + msg = "Error during SnapRAID job:\n" + + send_text = 'https://api.telegram.org/bot' + config(["telegram"]["bot_token"]) + '/sendMessage?chat_id=' + config(["telegram"]["bot_chatID"]) + '&parse_mode=text=' + send_text = send_text + msg + log + + response = requests.get(send_text) def send_email(success): import smtplib @@ -140,7 +152,7 @@ def load_config(args): global config parser = configparser.RawConfigParser() parser.read(args.conf) - sections = ["snapraid", "logging", "email", "smtp", "scrub"] + sections = ["snapraid", "logging", "email", "telegram", "smtp", "scrub"] config = dict((x, defaultdict(lambda: "")) for x in sections) for section in parser.sections(): for (k, v) in parser.items(section): From 991975dfaef3a31cdb447185cee96dcabe06eed3 Mon Sep 17 00:00:00 2001 From: Osh Date: Thu, 21 Jan 2021 23:05:06 +0200 Subject: [PATCH 4/7] Update snapraid-runner.conf.example Clear confusion between conf and conf.example --- snapraid-runner.conf.example | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snapraid-runner.conf.example b/snapraid-runner.conf.example index 25f9581..b8012c0 100644 --- a/snapraid-runner.conf.example +++ b/snapraid-runner.conf.example @@ -28,8 +28,8 @@ to = maxsize = 500 [telegram] -bot_token = 1307164393:AAEvw7Oe8hdNlRs3pRDTwfNMGgCHFIOZ1uo -bot_chatID = 44713803 +bot_token = +bot_chatID = [smtp] host = From 8f25fca64bff1e626dfbdd4b0294b72c68c32be6 Mon Sep 17 00:00:00 2001 From: Osh Date: Thu, 21 Jan 2021 23:15:16 +0200 Subject: [PATCH 5/7] Update snapraid-runner.py Updated reporting function --- snapraid-runner.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/snapraid-runner.py b/snapraid-runner.py index 1cb2bad..cf6cefe 100644 --- a/snapraid-runner.py +++ b/snapraid-runner.py @@ -136,11 +136,14 @@ def send_email(success): def finish(is_success): - if ("error", "success")[is_success] in config["email"]["sendon"]: - try: - send_email(is_success) - except Exception: - logging.exception("Failed to send email") + if config["snapraid"]["report"] = 'email' + if ("error", "success")[is_success] in config["email"]["sendon"]: + try: + send_email(is_success) + except Exception: + logging.exception("Failed to send email") + else: + send_telegram(is_success) if is_success: logging.info("Run finished successfully") else: From b64b0b916442d5d93331b88d36ed3afd43e1fe4e Mon Sep 17 00:00:00 2001 From: Osh Date: Thu, 21 Jan 2021 23:54:19 +0200 Subject: [PATCH 6/7] Update snapraid-runner.py First message was sent --- snapraid-runner.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/snapraid-runner.py b/snapraid-runner.py index cf6cefe..bad83f7 100644 --- a/snapraid-runner.py +++ b/snapraid-runner.py @@ -79,8 +79,9 @@ def send_telegram(success): else: msg = "Error during SnapRAID job:\n" - send_text = 'https://api.telegram.org/bot' + config(["telegram"]["bot_token"]) + '/sendMessage?chat_id=' + config(["telegram"]["bot_chatID"]) + '&parse_mode=text=' - send_text = send_text + msg + log + send_text = 'https://api.telegram.org/bot' + config["telegram"]["bot_token"] + '/sendMessage?chat_id=' + config["telegram"]["bot_chatid"] + '&text=' + + send_text = send_text + msg + email_log.getvalue() response = requests.get(send_text) @@ -136,7 +137,7 @@ def send_email(success): def finish(is_success): - if config["snapraid"]["report"] = 'email' + if (config["snapraid"]["report"].lower() == "email"): if ("error", "success")[is_success] in config["email"]["sendon"]: try: send_email(is_success) @@ -315,5 +316,11 @@ def run(): logging.info("All done") finish(True) +def test_run(): + logging.info("=" * 60) + logging.info("Run started") + logging.info("=" * 60) + logging.info("All done") + finish(True) main() From c8cc31f555bd0099ea9aec67bb332cb746edf5f3 Mon Sep 17 00:00:00 2001 From: Osh Date: Sat, 30 Jan 2021 15:44:17 +0200 Subject: [PATCH 7/7] Update gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index c0fde79..700d9c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ snapraid-runner.conf +snapraid-runner.code-workspace