You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.1 KiB
Python

5 years ago
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()