From 1fb735c102189521221069d2d9656f67df4db8d7 Mon Sep 17 00:00:00 2001 From: Hofei90 <29521028+hofei90@users.noreply.github.com> Date: Thu, 19 Sep 2019 19:58:44 +0200 Subject: [PATCH] initial commit --- .gitmodules | 3 + db_modell.py | 27 ++++++++ fotoablage_bot.py | 111 +++++++++++++++++++++++++++++++++ systemd/fotoablage_bot.service | 12 ++++ telegram_api | 1 + 5 files changed, 154 insertions(+) create mode 100644 .gitmodules create mode 100644 db_modell.py create mode 100644 fotoablage_bot.py create mode 100644 systemd/fotoablage_bot.service create mode 160000 telegram_api diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..ab39f2c --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "telegram_api"] + path = telegram_api + url = https://github.com/Hofei90/telegram_api.git diff --git a/db_modell.py b/db_modell.py new file mode 100644 index 0000000..eeb9f05 --- /dev/null +++ b/db_modell.py @@ -0,0 +1,27 @@ +import peewee + + +DB_PROXY = peewee.Proxy() + + +class BaseModel(peewee.Model): + class Meta: + database = DB_PROXY + + +class Ordner(BaseModel): + id = peewee.AutoField() + parent = peewee.IntegerField() + name = peewee.TextField() + zuletzt_verwendet = peewee.TimestampField() + + +class Foto(BaseModel): + id = peewee.AutoField() + ordner = peewee.IntegerField() + name = peewee.CharField() + notiz = peewee.CharField() # 255? + + +def db_create_table(): + DB_PROXY.create_tables([Ordner, Foto]) diff --git a/fotoablage_bot.py b/fotoablage_bot.py new file mode 100644 index 0000000..40df79b --- /dev/null +++ b/fotoablage_bot.py @@ -0,0 +1,111 @@ +from telegram_api import telegram_bot_api as api +import os +import toml +import time + + +def load_config(): + configfile = os.path.join(SKRIPTPFAD, "config_fotobot.toml") + with open(configfile) as conffile: + config = toml.loads(conffile.read()) + return config + + +SKRIPTPFAD = os.path.abspath(os.path.dirname(__file__)) +CONFIG = load_config() + + +class FotoBot(api.Bot): + def __init__(self, token, root_verzeichnis): + super().__init__(token) + self.root = root_verzeichnis + self.aktuelles_verzeichnis = self.root + + self.menue = None + self.umenue = None + + self.bot_kommando = { + "alarme": self.alarm + } + + def message_handler(self, message): + if self.menue is not None: + self.menue() + return + + def verzeichnis_wechseln(self, verzeichnisse): + self.aktuelles_verzeichnis = os.path.join(self.root, *verzeichnisse) + + def bot_kommando_exec(self, kommando): + self.menue = self.bot_kommando.get(kommando, self.kommando_unbekannt) + self.menue() + + def alarm(self): + print("Hello World") + + def kommando_unbekannt(self): + print("Unbekannt") + + +def get_biggest_size(photos): + max_file_size = 0 + max_index = None + for index, photo in enumerate(photos): + if photo["file_size"] > max_file_size: + max_index = index + return max_index + + +def datei_speichern(bot, file_name, inhalt): + file = os.path.join(bot.aktuelles_verzeichnis, file_name) + with open(file, "wb") as file_: + file_.write(inhalt) + + +def get_file_name(file_path): + filename = file_path.split("/")[1] + + +def nachricht_auswerten(bot, message): + print(message) + absender = message["message"]["from"].get("id", 0) + print(absender) + if absender not in CONFIG["erlaubte_telegram_ids"]: + bot.send_message(absender, "User nicht freigegeben") + return + if bot.menue is None: + if "entities" in message["message"]: + if "bot_command" in message["message"]["entities"][0]["type"]: + text = message["message"]["text"] + kommando = text.replace("/", "") + bot.bot_kommando_exec(kommando) + + if "photo" in message["message"]: + max_index = get_biggest_size(message["message"]["photo"]) + file, file_path = bot.get_file(message["message"]["photo"][max_index]["file_id"]) + #file_name = get_file_name() + #datei_speichern(bot, file_name, file) + + if "document" in message["message"]: + file, file_path = bot.get_file(message["message"]["document"]["file_id"]) + + else: + bot.menue() + + +def main(): + bot = FotoBot(CONFIG["token"], CONFIG["root_verzeichnis"]) + + while True: + nachricht_vorhanden = False + for message in bot.get_updates(): + nachricht_vorhanden = True + nachricht_auswerten(bot, message) + + if not nachricht_vorhanden and bot.menue is None: + break + time.sleep(0.1) + + +if __name__ == "__main__": + main() diff --git a/systemd/fotoablage_bot.service b/systemd/fotoablage_bot.service new file mode 100644 index 0000000..6e52e0f --- /dev/null +++ b/systemd/fotoablage_bot.service @@ -0,0 +1,12 @@ +[Unit] +Description=Telegram Foto Ablage Bot starten + + +[Service] +Type=simple +ExecStart=/usr/bin/python3 /home/pi/foto_ablage_telegrambot/fotoablage_bot.py +User=pi + + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/telegram_api b/telegram_api new file mode 160000 index 0000000..bed7610 --- /dev/null +++ b/telegram_api @@ -0,0 +1 @@ +Subproject commit bed76104df17b489552a7b0f8b1fb1f5c45165df