diff --git a/.gitignore b/.gitignore index ab96697..bcea61f 100644 --- a/.gitignore +++ b/.gitignore @@ -81,7 +81,6 @@ modules.xml # End of https://www.gitignore.io/api/pycharm+iml *.log -__pycache__/ config.json config-devel.json -bot_test.py +bot_test.py \ No newline at end of file diff --git a/HSMensaW_botA.py b/HSMensaW_botA.py index b911540..5343508 100644 --- a/HSMensaW_botA.py +++ b/HSMensaW_botA.py @@ -1,15 +1,6 @@ #!/usr/bin/python3 # -*- coding: utf-8 -*- - -# since 3.5: type hints -# def ( : - import asyncio -# write concurrent code (not threading. not multiprocessing) -# it's cooperative multitasking, no parallelism -# coroutines: suspend execution before return and pass control to another coroutine -# use await only in async functions - import datetime import json import signal @@ -35,34 +26,12 @@ from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton class Essen: - """Class which represents a Essen-Object""" - def __init__(self, name, preis, kategorie) -> None: - """ - Initialize a Essen-Object with given values - - Parameters: - name (string): name of the meal - preis (float): price of the meal - kategorie (string): category of the meal - - Returns: - None - """ + def __init__(self, name, preis, kategorie): self.name = name self.preis = preis self.kategorie = kategorie - return None - def __str__(self) -> str: - """ - Formats the attribute of the Essen-Object to a string - - Parameters: - None - - Returns: - _ (string): formatted String of attributes - """ + def __str__(self): if self.preis > 0: return str("*%s*: `%s` (%.2f €)" % (self.kategorie, self.name, self.preis)) else: @@ -76,19 +45,6 @@ class Language(Enum): config_filename = "config.json" - -var_corona_msg_de = "Bitte halten Sie sich an die AHA-Regeln in der Mensa und achten sie auf die "\ -"vorgegebenen Markierung und Hinweise. Den aktuellen Corona-Status an der HS Mittweida "\ -"können Sie an der [Corona-Ampel](https://www.hs-mittweida.de/) oder im [Corona-Newsticker]"\ -"(https://www.hs-mittweida.de/index.php?id=247957) der Hochschule einsehen. Nur *ZUSAMMEN* "\ -"bewirken wir etwas und bewätigen die Pandemie. Bleiben Sie gesund." - -var_corona_msg_eng = "Please follow the AHA rules in the cafeteria and pay attention to the given markings "\ -"and instructions. You can see the current Corona status of HS Mittweida at the [Corona"\ -"traffic light](https://www.hs-mittweida.de/) or in the [Corona news ticker]"\ -"(https://www.hs-mittweida.de/index.php?id=247957) of the university. Only *TOGETHER* we "\ -"make a difference and overcome the pandemic. Stay healthy." - info_str = "*Inoffizieller Mensa-Bot der Hochschule Mittweida*\nDieser Bot versendet jeden Tag um 10 Uhr den aktuellen " \ "Mensa-Speiseplan. Er wird über /start für den aktuellen Chat oder die aktuelle Gruppe gestartet, " \ "/stop beendet ihn wieder. Mit /essen, /mensa und /speiseplan (optional gefolgt von _en_ oder _de_) kann " \ @@ -96,7 +52,6 @@ info_str = "*Inoffizieller Mensa-Bot der Hochschule Mittweida*\nDieser Bot verse "verändert werden.\n\n_Haftungsausschluss: Dieser Bot steht in keiner Verbindung mit der Hochschule " \ "Mittweida oder dem Studentenwerk Freiberg. Alle Angaben ohne Gewähr._\n\nGrafik bereitgestellt von [" \ "vecteezy.com](https://de.vecteezy.com) " - status = "" essen = [] essen_eng = [] @@ -268,14 +223,12 @@ async def send_essen(chat_id: int, sprache: Language = Language.GERMAN) -> None: nachricht = "Speiseplan am %s:\n" % format_date(datum, format="full", locale="de_DE") for i in essen: nachricht += "- " + str(i).replace(".", ",") + "\n\n" - nachricht += var_corona_msg_de await send_message(bot_obj=bot, chat_id=chat_id, msg=nachricht, parse_mode="markdown") if sprache == Language.ENGLISH or sprache == Language.GERMAN_ENGLISH: nachricht = "Menu on %s:\n" % format_date(datum, format="full", locale="en") for i in essen_eng: nachricht += "- " + str(i) + "\n\n" - nachricht += var_corona_msg_eng await send_message(bot_obj=bot, chat_id=chat_id, msg=nachricht, parse_mode="markdown") diff --git a/README.md b/README.md index d7c6ef6..a295720 100644 --- a/README.md +++ b/README.md @@ -75,3 +75,23 @@ Konfigurationsdatei könnte so aussehen: ], "logging_enabled": false } + +### Start +### einfach +Der Bot kann über `python3 ` gestartet werden (die aktuelle Version +heißt *HSMensaW_botA.py*). Danach sollte er problemlos laufen. *stdout* und +*stderr* werden jeweils in eine Datei (*out.log* bzw. *err.log*) umgeleitet, +wobei es dort u. U. erst nach dem Beenden des Bots auftaucht. + +### immer +Im Projektordner gibt es ein Shellscript (*telebot_watchdog*), welches beim +Ausführen dafür sorgt, dass der Bot exakt einmal läuft. Wenn dieses Skript +(z. B. per Cron) regelmäßig gestartet wird, wird sichergestellt, dass der +Bot immer läuft (auch nach einem Neustart oder Crash). +Dafür müssen folgende Zeilen in die [Crontab](https://wiki.ubuntuusers.de/Cron/#Cronjobs-manuell-einrichten) eingetragen werden: + + @reboot /home/pi/scripts/telebot_watchdog + * * * * * /home/pi/scripts/telebot_watchdog + +(wobei die erste Zeile eigentlich überflüssig ist). Die Pfade müssen +natürlich angepasst werden, genauso im Skript selbst. \ No newline at end of file diff --git a/telebot_watchdog b/telebot_watchdog new file mode 100644 index 0000000..a279335 --- /dev/null +++ b/telebot_watchdog @@ -0,0 +1,16 @@ +#!/bin/bash +# zaehle die Prozesse, die den Bot ausfuehren +count=$(ps aux | grep HSMensaW_bot.py | grep -c -v grep) +# mehr als einer? +if [ "$count" -gt 1 ]; then + # beende alle + kill `ps axo pid,command | grep HSMensaW | grep -v grep | sed -e 's/^[[:space:]]*//' | cut -d ' ' -f1 | tr '\n' ' '` +fi + +# ungleich 1 (wenn mehr, wurden sie schon beendet, daher keiner) +if [ "$count" -ne 1 ]; then + # starte neu + cd /home/pi/scripts || exit + python3 HSMensaW_bot.py & +fi +