Skip to content
Snippets Groups Projects
Commit 0edc0242 authored by wupo's avatar wupo
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
venv/
store/
.idea/
.env
# nbspplenumbot
This is a mail bot to send plenum invitation messages.
## Structure
There are five relevant files in this repository making the bot run:
### requirements.txt
Contains all the dependencies required to run the bot.py script.
### bot.py
This file contains the core bot logic and some hardcoded values.
If you want to use the bot in different setups, these values must be changed accordingly.
### nbspplenumbot.sh
This file is only needed to set up the environment for the bot.
The bot won't run (be comfy) without these environment parameters set.
The MAIL_* environment variables must contain valid login data for the mail server.
### nbspplenumbot.service / nbspplenumbot.timer
These files contain systemd unit configurations to make the bot run at discrete points in time.
## Setup
- Create a new directory on your machine e.g. ```mkdir /home/bots/scripts/nbspplenumbot```
- install python if not already installed, create and activate a new virtual environment
sudo apt install python3.8-venv
python3 -m venv /home/bots/scripts/nbspplenumbot/venv
. /home/bots/scripts/nbspplenumbot/venv/bin/activate
- Copy all five project files to the created directory
- Install requirements ```pip install -r requirements.txt```
- Move the systemd unit files to systemd
sudo mv /home/bots/scripts/nbspplenumbot/nbspplenumbot.service /etc/systemd/system/
sudo mv /home/bots/scripts/nbspplenumbot/nbspplenumbot.timer /etc/systemd/system/
- Enable the systemd timer unit ```sudo systemctl enable nbspplenumbot.timer```
bot.py 0 → 100644
import datetime
import os
import random
import asyncio
import mail.utils as mail_utils
from dotenv import load_dotenv
load_dotenv()
MAIL_SERVER = os.getenv('MAIL_SERVER')
MAIL_USER = os.getenv('MAIL_USER')
MAIL_PASSWORD = os.getenv('MAIL_PASSWORD')
def n_days_prior_plenum(n):
today = datetime.date.today()
n_days = datetime.timedelta(days=n)
future_date = today + n_days
thursday = 3
if 7 < future_date.day <= 14 and future_date.weekday() == thursday:
return future_date
def get_important_message():
important_message = [
"die Revolution",
"die Evolution",
"die Bodylotion",
"die Integration",
"die Supervision",
"die Automation",
"die Definition",
"die Zukunftsvision",
"die Superlation"
]
return random.choice(important_message)
async def main():
plenum_date = n_days_prior_plenum(3)
if plenum_date:
smtp_connection = mail_utils.get_smtp_connection(MAIL_SERVER, MAIL_USER, MAIL_PASSWORD)
mail_utils.send_plenum_message(smtp_connection,
['wupo@chaotikum.org'],
plenum_date,
MAIL_USER,
f'Plenum am {plenum_date.isoformat()} um 19 Uhr',
f'{MAIL_USER}@chaotikum.org',
get_important_message())
smtp_connection.quit()
if __name__ == "__main__":
asyncio.run(main())
File added
File added
import smtplib
from email.mime.text import MIMEText
def get_smtp_connection(host, user, password):
connection = smtplib.SMTP(host=host)
connection.starttls()
connection.login(user, password)
return connection
def send_plenum_message(smtp_connection,
recipients,
date,
sending_user,
subject,
from_address,
important_message):
message_text = f'''Moin zusammen,
am {date} um 19 Uhr ist wieder Plenum.
Die Themen findet ihr wie immer im pad unter: https://md.chaotikum.org/plenum-{date.isoformat()}
Plenum ist wichtig für {important_message}!
Beste Grüße
wupo
'''
mail = MIMEText(message_text.encode('utf-8'), _charset='utf-8')
mail['Subject'] = subject
mail['Sender'] = sending_user
mail['From'] = from_address
smtp_connection.sendmail(sending_user,
recipients,
mail.as_string())
[Unit]
Description=runs nbspplenumbot
After=network.target
[Service]
Type=oneshot
ExecStart=/home/bots/scripts/nbspplenumbot/nbspplenumbot.sh
[Install]
WantedBy=multiuser.target
#!/bin/sh
export MAIL_SERVER='mail.chaotikum.net';
export MAIL_USER='noreply';
export MAIL_PASSWORD='<MAIL_PASSWORD>';
. /home/bots/scripts/nbspplenumbot/venv/bin/activate;
python /home/bots/scripts/nbspplenumbot/bot.py;
exit 0
\ No newline at end of file
[Unit]
Description=Run custom command periodically
[Timer]
Unit=nbspplenumbot.service
OnCalendar=*-*-* 19:00:00
Persistent=true
[Install]
WantedBy=timers.target
requests==2.28.2
python-dotenv==1.0.0
\ No newline at end of file
b'gAAAAABj3BQxr1iH0nYiJLcFy6DJBtO3RMQJ45IFI4GKMsbZ6cdKwSYySwXmipS4dsSNwVOEaLe06Cm8NR3KPu7IfbKeRGPCgjCuAu-VVFCG25F0WDCnkq7OD7Mi6xMx8pgSu9NP0fia7Of3hSa7u6VFo_15kysdI6RzZWoTFz3rbBcnUQJql6o='
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment