initial commit

master
Hofei90 5 years ago committed by Hofei
parent c7fa94e2e4
commit 1fb735c102

3
.gitmodules vendored

@ -0,0 +1,3 @@
[submodule "telegram_api"]
path = telegram_api
url = https://github.com/Hofei90/telegram_api.git

@ -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])

@ -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()

@ -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

@ -0,0 +1 @@
Subproject commit bed76104df17b489552a7b0f8b1fb1f5c45165df
Loading…
Cancel
Save