Changeset - a7ec834c972d
[Not reviewed]
Merge default
0 1 0
Dennis Fink - 3 years ago 2022-07-18 23:41:16
dennis.fink@c3l.lu
Merge notifyEmail
1 file changed with 41 insertions and 2 deletions:
0 comments (0 inline, 0 general)
spaceapi/utils.py
Show inline comments
 
import email
 
import email.policy
 
import json
 
import os.path
 
import random
 
import smtplib
 
import ssl
 
from functools import wraps
 
from time import time
 

	
 
@@ -16,9 +20,11 @@ if not os.path.exists(default_json_file_
 
elif not os.path.isfile(default_json_file_v14):
 
    raise RuntimeError("default_v14.json is not a file!")
 

	
 
standard_open_message = "The space is now open!"
 
standard_close_message = "The space is now closed!"
 

	
 
possible_open_tweets = (
 
    "The space is now open!",
 
    standard_open_message,
 
    "The space is open! Come in and hack something!",
 
    "Yes, we're open! Come in and create something!",
 
    "Come by and hack something! We've just opened!",
 
@@ -29,7 +35,7 @@ possible_open_tweets = (
 
)
 

	
 
possible_closed_tweets = (
 
    "The space is now closed!",
 
    standard_close_message,
 
    "We're closed now! See you soon.",
 
    "Sorry, we are closed now!",
 
    "The ChaosStuff is now closed! Come back another time!",
 
@@ -68,6 +74,26 @@ def post_toot(toot):
 
        api.status_post(toot, visibility="unlisted")
 

	
 

	
 
def post_email(subject, body):
 
    if "EMAIL_PASS" in current_app.config:
 
        smtp_conn = smtplib.SMTP(
 
            current_app.config["EMAIL_HOST"], port=current_app.config["EMAIL_PORT"]
 
        )
 
        ssl_context = ssl.create_default_context()
 
        smtp_conn.starttls(context=ssl_context)
 
        smtp_conn.login(
 
            current_app.config["EMAIL_USER"], current_app.config["EMAIL_PASS"]
 
        )
 
        msg = email.message.EmailMessage(policy=email.policy.default)
 
        msg["To"] = current_app.config["EMAIL_ANNOUNCE_ADDRESS"]
 
        msg["From"] = "spaceapibot <{email}>".format(
 
            email=current_app.config["EMAIL_USER"]
 
        )
 
        msg["Subject"] = subject
 
        msg.set_content(body)
 
        smtp_conn.send_message(msg)
 

	
 

	
 
class Singleton:
 
    def __new__(cls, *args, **kwargs):
 
        key = str(hash(cls))
 
@@ -174,12 +200,25 @@ class ActiveStatusv14(Singleton, dict):
 
        except Exception as e:
 
            current_app.logger.error("Sending toot failed! %s" % e, exc_info=True)
 

	
 
    def send_email(self, value):
 
        subject = standard_open_message if value else standard_close_message
 
        message = (
 
            random.choice(possible_open_tweets)
 
            if value
 
            else random.choice(possible_closed_tweets)
 
        )
 
        try:
 
            post_email(subject, message)
 
        except Exception as e:
 
            current_app.logger.error("Sending email failed! %s" % e, exc_info=True)
 

	
 
    def set_new_state(self, value=None, trigger_person=None, message=None):
 

	
 
        if value is not None and value != self["state"]["open"]:
 
            self["state"]["open"] = value
 

	
 
            self.send_tweet(value)
 
            self.send_email(value)
 

	
 
            if not value:
 
                if "people_now_present" in self["sensors"]:
0 comments (0 inline, 0 general)