Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

Commit

Permalink
fix: telegram token regex, email starttls
Browse files Browse the repository at this point in the history
  • Loading branch information
sibalzer committed Jun 7, 2021
1 parent 06092d0 commit bc0f07f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
25 changes: 17 additions & 8 deletions src/alerts.py
Expand Up @@ -6,6 +6,7 @@
import logging
from email.message import EmailMessage
from email.utils import formatdate
import ssl

from common import APPOINTMENT_URL
from settings import settings
Expand All @@ -29,7 +30,6 @@ def alert(msg: str) -> None:
log.debug("[TELEGRAM] Try to send telegram message")
try:
send_telegram(msg)
log.info("[TELEGRAM] Sending telegram message was successful")
except Exception as ex:
log.error(f"[TELEGRAM] Couldn't send Telegram message: {ex}")
else:
Expand All @@ -51,8 +51,6 @@ def alert(msg: str) -> None:
log.debug(f"[APPRISE] try to send Apprise Notification")
try:
send_apprise(msg)
log.info(
f"[APPRISE] sending Apprise Notification was successful")
except Exception as ex:
log.error(f"Couldn't send Apprise Notification: {ex}")
else:
Expand All @@ -70,9 +68,19 @@ def send_mail(msg: str) -> None:
mail['subject'] = msg
mail.set_content(APPOINTMENT_URL)

with smtplib.SMTP(settings.EMAIL_SERVER, settings.EMAIL_PORT) as smtp:
smtp.login(settings.EMAIL_USER, settings.EMAIL_PASSWORD)
smtp.send_message(mail)
if settings.EMAIL_PORT == 465:
with smtplib.SMTP_SSL(settings.EMAIL_SERVER, settings.EMAIL_PORT)as smtp:
smtp.login(settings.EMAIL_USER, settings.EMAIL_PASSWORD)
smtp.send_message(mail)
elif settings.EMAIL_PORT == 587:
with smtplib.SMTP(settings.EMAIL_SERVER, settings.EMAIL_PORT)as smtp:
smtp.starttls(context=ssl.create_default_context())
smtp.login(settings.EMAIL_USER, settings.EMAIL_PASSWORD)
smtp.send_message(mail)
else:
with smtplib.SMTP(settings.EMAIL_SERVER, settings.EMAIL_PORT)as smtp:
smtp.login(settings.EMAIL_USER, settings.EMAIL_PASSWORD)
smtp.send_message(mail)


def send_telegram(msg: str) -> None:
Expand All @@ -82,12 +90,13 @@ def send_telegram(msg: str) -> None:
url = f"tgram://{settings.TELEGRAM_TOKEN}"
for chat_id in settings.TELEGRAM_CHAT_IDS:
url += f"/{chat_id}"
url += "?format=markdown"

appobj.add(url)

appobj.notify(
body=f"{APPOINTMENT_URL}",
title=f"*{msg}*",
title=f"**{msg}**",
)


Expand All @@ -100,5 +109,5 @@ def send_apprise(msg: str) -> None:

appobj.notify(
body=f"{APPOINTMENT_URL}",
title=f"*{msg}*",
title=f"{msg}",
)
8 changes: 4 additions & 4 deletions src/common.py
Expand Up @@ -20,13 +20,13 @@
"user": r"^[^ ]*$", # match anything execpt space
"password": r"^[^ ]*$", # match anything execpt space
# match alphanumeric characters, dash, and dot
"server": r"\b^[a-zA-Z0-9-\.]+$",
"server": r"^[a-zA-Z0-9-\.]+$",
"port": r"^\d{2,}$",
"receivers": r"\b" + MAIL_REGEX + r"(," + MAIL_REGEX + r")*\b",
"receivers": r"^" + MAIL_REGEX + r"(," + MAIL_REGEX + r")*$",
# I hope this covers all possible tokens
"token": r"^[a-zA-Z0-9\:\-]+$",
"token": r"^[0-9]+:[a-zA-Z0-9\-_]+$",
"chat_ids": r"^\d{5,}(,\d{5,})*$", # matches a list of numbers
"service_uris": r"\b[^ ]+\b", # match anything not a space
"service_uris": r"^[^ ]+$", # match anything not a space
}


Expand Down

0 comments on commit bc0f07f

Please sign in to comment.