Changeset - 964b9fccf759
[Not reviewed]
notifyEmail
0 1 0
x - 3 years ago 2022-06-24 17:07:59
xbr@c3l.lu
Add initial email sending
1 file changed with 32 insertions and 2 deletions:
0 comments (0 inline, 0 general)
spaceapi/utils.py
Show inline comments
 
import json
 
import os.path
 
import random
 
import smtplib
 
import email
 
from functools import wraps
 
from time import time
 

	
 
import mastodon
 
import tweepy
 
from flask import current_app, request
 
@@ -13,26 +15,28 @@ last_state_file_v14 = os.path.abspath("l
 

	
 
if not os.path.exists(default_json_file_v14):
 
    raise RuntimeError("default_v14.json does not exists!")
 
elif not os.path.isfile(default_json_file):
 
    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!",
 
    "The ChaosStuff is now open for everyone!",
 
    "Let the Chaos begin! We're open!",
 
    "What do we hack now? Come and see, we've just opened!",
 
    "TUWAT! Come and hack. We are open!",
 
)
 

	
 
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!",
 
    "Poweroff process finished! We're closed!",
 
    "Singularity reached! The space is closed!",
 
    "Dream of electric sheeps! We are closed!",
 
@@ -65,12 +69,26 @@ def post_toot(toot):
 
            access_token=current_app.config["MASTODON_USERCRED_FILE"],
 
            api_base_url="https://chaos.social",
 
        )
 
        api.status_post(toot, visibility="unlisted")
 

	
 

	
 
def post_email(subject, body):
 
    if "EMAIL_PASS" in current_app.config:
 
        smtpConn = smtplib.SMTP("EMAIL_HOST", port="EMAIL_PORT")
 
        sslContext = ssl.create_default_context(Purpose.SERVER_AUTH)
 
        smtpConn.starttls(sslContext)
 
        smtpConn.login("EMAIL_USER", "EMAIL_PASS")
 
        msg = email.message.EmailMessage(email.policy.SMTP)
 
        msg['To'] = "EMAIL_ANNOUNCE_ADDRESS"
 
        msg['From'] = "EMAIL_USER"
 
        msg['Subject'] = subject
 
        msg.set_content(body)
 
        smtpConn.send_message(msg)
 

	
 

	
 
class Singleton:
 
    def __new__(cls, *args, **kwargs):
 
        key = str(hash(cls))
 

	
 
        if not hasattr(cls, "_instance_dict"):
 
            cls._instance_dict = {}
 
@@ -171,18 +189,30 @@ class ActiveStatusv14(Singleton, dict):
 

	
 
        try:
 
            post_toot(tweet)
 
        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
 
        )
 
        try:
 
            post_email(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"]:
 
                    self.clear_user_present()
 

	
 
                if "message" in self["state"]:
0 comments (0 inline, 0 general)