Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add telegram support #39

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
snapraid-runner.conf
snapraid-runner.code-workspace
6 changes: 6 additions & 0 deletions snapraid-runner.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,6 +27,10 @@ to =
; maximum email size in KiB
maxsize = 500

[telegram]
bot_token =
bot_chatID =

[smtp]
host =
; leave empty for default port
Expand Down
34 changes: 28 additions & 6 deletions snapraid-runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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()