From 231b93e851de4a0f01a1ea114b7c00483f7f5b7c Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 30 May 2021 17:07:19 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20email=20and=20tweet=20post?= =?UTF-8?q?ing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nest_crawler/alert_trigger.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/nest_crawler/alert_trigger.py b/nest_crawler/alert_trigger.py index 036aef8..6be945c 100644 --- a/nest_crawler/alert_trigger.py +++ b/nest_crawler/alert_trigger.py @@ -2,7 +2,12 @@ from datetime import datetime, timedelta from nest_backend.database import * from .authentication import authenticate import smtplib +import os import tweepy as tw +import datetime + + +MESSAGE = "❗ {alert_name}: la soglia di allerta è stata superata alle {now}!" def is_repo_alert_triggered(repository_id): @@ -37,20 +42,20 @@ def is_repo_alert_triggered(repository_id): ext.session.commit() print("alert triggered") alerts_triggered.append(alert) - #send_notification_email(alert) + send_notification_email(alert) send_notification_tweet(alert) def send_notification_email(alert): - owner_repo = alert.repository.owner - conditions_string = '' - for condition in alert.conditions: - conditions_string += condition.condition.content + ',' - conditions_string = conditions_string[:-1] - smtpObj = None try: - with smtplib.SMTP(host='localhost') as smtpObj: - smtpObj.sendmail("alert@nest.com", owner_repo.email, "Alert triggered") + with smtplib.SMTP_SSL(host=os.environ["SMTP_HOST"], port=587) as smtpObj: + smtpObj.ehlo() + smtpObj.starttls() + smtpObj.ehlo() + smtpObj.login(os.environ["SMTP_USERNAME"], os.environ["SMTP_PASSWORD"]) + smtpObj.sendmail(os.environ["SMTP_FROM_EMAIL"], + alert.repository.owner.email, + MESSAGE.format(alert_name=alert.name, now=datetime.datetime.now().isoformat())) print("Successfully sent email") except smtplib.SMTPException: print("Error: unable to send email") @@ -64,7 +69,7 @@ def send_notification_tweet(alert): conditions_string = conditions_string[:-1] print(conditions_string) try: - api.update_status(f"L'alert {alert.name} è stato attivato! C'è stato un incremento di popolarità negli argomenti di ricerca {conditions_string}") + api.update_status(MESSAGE.format(alert_name=alert.name, now=datetime.datetime.now().isoformat())) except tw.errors.Forbidden: print("Il tweet e' gia' stato pubblicato")