diff --git a/.gitignore b/.gitignore index c0fde79..700d9c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ snapraid-runner.conf +snapraid-runner.code-workspace diff --git a/snapraid-runner.conf.example b/snapraid-runner.conf.example index ed64810..b8012c0 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 @@ -25,6 +27,10 @@ to = ; maximum email size in KiB maxsize = 500 +[telegram] +bot_token = +bot_chatID = + [smtp] host = ; leave empty for default port diff --git a/snapraid-runner.py b/snapraid-runner.py index d7051f1..bad83f7 100644 --- a/snapraid-runner.py +++ b/snapraid-runner.py @@ -71,6 +71,19 @@ 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"] + '&text=' + + send_text = send_text + msg + email_log.getvalue() + + response = requests.get(send_text) def send_email(success): import smtplib @@ -124,11 +137,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"].lower() == "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: @@ -140,7 +156,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): @@ -300,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()