diff --git a/README.md b/README.md index cef298278297269d7c7ff5b9a7d41f4763a77d59..7a8595432795dae19325c33ae65da3c22e84f389 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,8 @@ You can use:\ `.deepl <LC>, <String>` - Translate a word, sentence or even small texts with the DeepL translator. `<LC>` is a language code.\ The bot will try to guess the language of `<String>`. Example: `.deepl ES, Where is the library?` Powered by [DeepL](https://www.deepl.com) +`.addSong <Message>` - Just add .addSong tou your normal Spotify Song Message. The bot will do the rest. + ## Crawlers `.genderneutral <German String>` or `.gn <German String>` - Looks if a string (can be more than one sentence) is genderneutral and displays alternatives for genderspecific words. Powered by [geschicktgendern](https://www.geschicktgendern.de) diff --git a/default_config.json b/default_config.json index 9b65419decf32b1c88f08bc8dbab085408082935..780f21ac2044480b7ff76febcf9a5619e8d59436 100644 --- a/default_config.json +++ b/default_config.json @@ -10,21 +10,15 @@ "name_2": "link" } }, - "FUN_DICT": { - "ping": "pong", - "marco": "Polo", - "foo": "bar", - "hello": "world!", - "answer": "42", - "echo": "echo", - "beep": "boop" - }, "GROUPS": [ { - "group_1": "Groupname: doesn't nedd to match the real groupname", - "ID": ["replace this string with the group_id_array"], - "COMMANDS": ["echo", "help", "credits", "remusfeinste", "trueamerican", "links", "yesno", "yn", "dadjoke", "genderneutral", "gn", "freegames", "savequote", "addquote", "createquote" -, "randomquote", "saveevent", "addevent", "createevent", "nextevent", "allevents", "savebirthday", "addbirthday", "createbirthday", "nextbirthday", "allbirthdays", "tex", "sips", "schlücke", "challenge", "roll", "request", "deepl"] , + "group_1": "Groupname: doesn't nedd to match the real groupname", + "ID": [ + "replace this string with the group_id_array" + ], + "COMMANDS": ["echo", "help", "credits", "remusfeinste", "trueamerican", "links", "yesno", "yn", "dadjoke", "genderneutral", "gn", "freegames", "savequote", + "addquote", "createquote", "randomquote", "saveevent", "addevent", "createevent", "nextevent", "allevents", "savebirthday", + "addbirthday", "createbirthday", "nextbirthday", "allbirthdays", "tex", "sips", "schlücke", "challenge", "roll", "request", "deepl"], "LINKS": { "category_1": { "name": "link", @@ -35,15 +29,14 @@ "name_2": "link" } }, - "QUOTESDIR":"/path/to/group_1.csv", - "BIRTHDAYDIR":"/path/to/group_1.csv", - "EVENTDIR":"/path/to/group_1.csv" - + "QUOTESDIR": "/path/to/group_1.csv", + "BIRTHDAYDIR": "/path/to/group_1.csv", + "EVENTDIR": "/path/to/group_1.csv" } ], - "Admins":{ + "Admins": { "bot_admin_1": "phone_number", - "bot_admin_2": "phone_number" + "bot_admin_2": "phone_number" }, "MQTT": { "mqtt.local": { @@ -58,5 +51,10 @@ "group_name": "replace this string with the group_id_array", "person_name": "phone_number" }, + "Spotify": { + "spotify_client_id": "replace this string with the spotify_client_id", + "client_secret": "replace this string with the client_secret", + "redirect_uri": "replace this string with the redirect_uri" + }, "DEEPLAPI": "API-Key" -} +} \ No newline at end of file diff --git a/signalbot.py b/signalbot.py index 6c68b10f3d5bc6a34aff11289d028176654390ad..23a610ef2330c71b8733f0a97b0bd50fd4502cf7 100755 --- a/signalbot.py +++ b/signalbot.py @@ -6,7 +6,6 @@ import json import logging import random import time -import calendar from datetime import date, datetime, timedelta from threading import Thread @@ -60,67 +59,15 @@ commands = None event_reminder = None translator = None today = None -CONFIG = {} -FUN_DICT = {} - -MQTT_USER = "" -MQTT_PWD = "" -MQTT_IP = "" -MQTT_PORT = None -MQTT_TIMEOUT = None - -FREE_GAMES_SUBSCRIBER = {} -GROUPS = {} def load_config(filename): global CONFIG - global FUN_DICT - - global MQTT_USER - global MQTT_PWD - global MQTT_IP - global MQTT_PORT - global MQTT_TIMEOUT - - global FREE_GAMES_SUBSCRIBER - global GROUPS - global translator - - global spotify_client_id - global spotify_client_secret - global spotify_redirect_uri try: - CONFIG = json.load(open(filename, "r")) - if "FUN_DICT" in CONFIG: - FUN_DICT = CONFIG["FUN_DICT"] - if "MQTT" in CONFIG: - if "mqtt.local" in CONFIG["MQTT"]: - if "MQTT_USER" in CONFIG["MQTT"]["mqtt.local"]: - MQTT_USER = CONFIG["MQTT"]["mqtt.local"]["MQTT_USER"] - if "MQTT_PWD" in CONFIG["MQTT"]["mqtt.local"]: - MQTT_PWD = CONFIG["MQTT"]["mqtt.local"]["MQTT_PWD"] - if "MQTT_IP" in CONFIG["MQTT"]["mqtt.local"]: - MQTT_IP = CONFIG["MQTT"]["mqtt.local"]["MQTT_IP"] - if "MQTT_PORT" in CONFIG["MQTT"]["mqtt.local"]: - MQTT_PORT = CONFIG["MQTT"]["mqtt.local"]["MQTT_PORT"] - if "MQTT_TIMEOUT" in CONFIG["MQTT"]["mqtt.local"]: - MQTT_TIMEOUT = CONFIG["MQTT"]["mqtt.local"]["MQTT_TIMEOUT"] - if "FREE_GAMES_SUBSCRIBER" in CONFIG: - FREE_GAMES_SUBSCRIBER = CONFIG["FREE_GAMES_SUBSCRIBER"] - if "GROUPS" in CONFIG: - GROUPS = CONFIG["GROUPS"] - if "DEEPLAPI" in CONFIG: - if CONFIG["DEEPLAPI"] != "API-Key": - translator = deepl.Translator(CONFIG["DEEPLAPI"]) - if "Spotify" in CONFIG: - if "client_id" in CONFIG["Spotify"]: - spotify_client_id = CONFIG["Spotify"]["client_id"] - if "client_secret" in CONFIG["Spotify"]: - spotify_client_secret = CONFIG["Spotify"]["client_secret"] - if "redirect_uri" in CONFIG["Spotify"]: - spotify_redirect_uri = CONFIG["Spotify"]["redirect_uri"] + CONFIG = {} + with open(filename, 'r') as file: + CONFIG = json.load(file) except FileNotFoundError: log.error(f"File:'{filename}' not accessible. Running in reduced mode.") @@ -189,12 +136,9 @@ def send(message, receiver): if "f43e6540-0274-481e-838d-feefbb0686df" in err_message: return - request_group = None - for group in GROUPS: - if group["NAME"] == "Testung": - request_group = group - break - send(err_message, request_group["ID"]) + testung = next((group for group in GROUPS if group["NAME"] == "Testung"), None) + if testung is not None: + send(err_message, testung["ID"]) def send_a(message, receiver, attachment): @@ -210,12 +154,9 @@ def send_a(message, receiver, attachment): log.error(err_message) if "f43e6540-0274-481e-838d-feefbb0686df" in err_message: return - request_group = None - for group in GROUPS: - if group["NAME"] == "Testung": - request_group = group - break - send(err_message, request_group["ID"]) + testung = next((group for group in GROUPS if group["NAME"] == "Testung"), None) + if testung is not None: + send(err_message, testung["ID"]) def echo(msgDict): @@ -384,19 +325,19 @@ def roll(msgDict): def request(msgDict): answer = "" - request_group = None - for group in GROUPS: - if group["NAME"] == "Requests": - request_group = group - break + request_group = next((group for group in GROUPS if group["NAME"] == "Requests"), None) if not msgDict["content"]: answer = "Was soll denn hinzugefügt werden?" else: answer = "Besten Dank! Die Anfrage wurde weitergeleitet." - send(answer, msgDict["receiver"]) - send(msgDict["content"], request_group["ID"]) + if request_group is not None: + send(answer, msgDict["receiver"]) + send(msgDict["content"], request_group["ID"]) + else: + send('Der Befehl Requests funktioniert gerade nicht :(', msgDict["receiver"]) + def get_random_challenge(msgDict): @@ -485,11 +426,12 @@ def get_deepl_translation(msgDict): def add_song_to_playlist(msgDict): + spotify_config = CONFIG.get("Spotify", {}) if ( ModuleSpotify is None - or spotify_client_id is None - or spotify_client_secret is None - or spotify_redirect_uri is None + or spotify_config.get('spotify_client_id', None) is None + or spotify_config.get('spotify_client_secret', None) is None + or spotify_config.get('redirect_uri', None) is None ): # TODO bereits in der INit berücksichtigen, wenn die Secrets nicht am start sind send("There is no Spotify!", msgDict["receiver"]) return @@ -520,19 +462,21 @@ def on_mqtt_message(client, userdata, msg): def mqtt_client(): + mqtt_local = CONFIG.get("MQTT", {}).get("mqtt.local", {}) + client2 = mqtt.Client() client2.on_message = on_mqtt_message - client2.username_pw_set(MQTT_USER, MQTT_PWD) - client2.connect(MQTT_IP, MQTT_PORT, MQTT_TIMEOUT) + client2.username_pw_set(mqtt_local.get("MQTT_USER", ""), mqtt_local.get("MQTT_PWD", '')) + client2.connect(mqtt_local.get("MQTT_IP", ''), mqtt_local.get("MQTT_PORT", None), mqtt_local.get("MQTT_TIMEOUT", None)) # TODO Topics und receiver in config auslagern, um dynamischer zu subscriben client2.subscribe("Sensoren/Arbeitszimmer/temperature") client2.loop_forever() def send_heartbeat(): - for group in GROUPS: - if group["NAME"] == "Testung": - send("Still alive - there will be 🎂", group["ID"]) + testung = next((group for group in GROUPS if group["NAME"] == "Testung"), None) + if testung is not None: + send("Still alive - there will be 🎂", testung["ID"]) def init_schedule_jobs(): @@ -553,20 +497,16 @@ def init_schedule_jobs(): def remind_pflanzen(): - for group in GROUPS: - if group["NAME"] == "TierWG": - send(f"Bitte gebt den armen Pflanzen etwas Lebenselixier!", group["ID"]) - break + tierwg = next((group for group in GROUPS if group["NAME"] == "TierWG"), None) + if tierwg is not None: + send(f"Bitte gebt den armen Pflanzen etwas Lebenselixier!", tierwg["ID"]) + def gg_remind_schichten(): - for group in GROUPS: - if group["NAME"] == "GGOffiziell": - send( - f'Bitte tragt euch in die Planung ein:\n {group["LINKS"]["Planung"]}', - group["ID"], - ) - break + gg_group = next((group for group in GROUPS if group["NAME"] == "GGOffiziell"), None) + if gg_group is not None: + send(f'Bitte tragt euch in die Planung ein:\n {gg_group["LINKS"]["Planung"]}', gg_group["ID"]) def gg_remind_stundenzettel(): @@ -594,13 +534,9 @@ def gg_remind_stundenzettel(): last_day -= datetime.timedelta(days=5) - gg_group = None - for group in GROUPS: - if group["NAME"] == "GGOffiziell": - gg_group = group - break + gg_group = next((group for group in GROUPS if group["NAME"] == "GGOffiziell"), None) - if last_day == date.today().day: + if last_day == date.today().day and gg_group is not None: send( """Bitte schickt eure ausgefüllten Stundenzettel! • ihr müsst eure Abrechnungen jeden Monat sowohl an Sarah, als auch an Ben senden. @@ -627,16 +563,20 @@ def run_threaded(job_func): if __name__ == "__main__": load_config("/home/pi/signalbot/config.json") + # global GROUPS + GROUPS = CONFIG.get("GROUPS", {}) + spotify_config = CONFIG.get("Spotify", {}) + gn = ModuleGenderneutral("gn.json", log) quotes = ModuleQuotes(GROUPS, log) - games = ModuleFreeGames(FREE_GAMES_SUBSCRIBER, send_a, log) + games = ModuleFreeGames(CONFIG.get("FREE_GAMES_SUBSCRIBER", {}), send_a, log) challenges = ModuleChallenge("/home/pi/signalbot/challenge.json", log) birthday_reminder = ModuleBirthdayReminder(GROUPS, send, log) tex = ModuleTex(log) - commands = ModuleCommands("/home/pi/signalbot/commands.json", FUN_DICT, log) + commands = ModuleCommands("/home/pi/signalbot/commands.json", CONFIG.get("FUN_DICT", {}), log) event_reminder = ModuleEventReminder(GROUPS, send, log) spotify = ModuleSpotify( - send, spotify_client_id, spotify_client_secret, spotify_redirect_uri, log + send, spotify_config.get('spotify_client_id', ''), spotify_config.get('spotify_client_secret', ''), spotify_config.get('redirect_uri', ''), log ) today = ModuleToday(log) loop = GLib.MainLoop()