From 2204bd5d9eaa98f545a6d65024ef2798b93db4bf Mon Sep 17 00:00:00 2001 From: fspitzba Date: Tue, 13 Oct 2020 20:09:33 +0200 Subject: [PATCH 1/7] [DELETE] deleted watchdog, it is not longer needed --- telebot_watchdog | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 telebot_watchdog diff --git a/telebot_watchdog b/telebot_watchdog deleted file mode 100644 index a279335..0000000 --- a/telebot_watchdog +++ /dev/null @@ -1,16 +0,0 @@ -#!/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 - From 3f33a13f771463b10f058cdc997a5d2fcdb1f908 Mon Sep 17 00:00:00 2001 From: fspitzba Date: Tue, 13 Oct 2020 20:17:02 +0200 Subject: [PATCH 2/7] cleared watchdog out of README.md --- README.md | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/README.md b/README.md index a295720..d7c6ef6 100644 --- a/README.md +++ b/README.md @@ -75,23 +75,3 @@ 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 From 08a4c4d3c1ca65feac05efae9c3e55acafd57af3 Mon Sep 17 00:00:00 2001 From: fspitzba Date: Tue, 13 Oct 2020 22:35:14 +0200 Subject: [PATCH 3/7] changed gitignore, corrected tabs, added some comments --- .gitignore | 3 ++- HSMensaW_botA.py | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index bcea61f..ab96697 100644 --- a/.gitignore +++ b/.gitignore @@ -81,6 +81,7 @@ modules.xml # End of https://www.gitignore.io/api/pycharm+iml *.log +__pycache__/ config.json config-devel.json -bot_test.py \ No newline at end of file +bot_test.py diff --git a/HSMensaW_botA.py b/HSMensaW_botA.py index 5343508..1222bff 100644 --- a/HSMensaW_botA.py +++ b/HSMensaW_botA.py @@ -1,6 +1,14 @@ #!/usr/bin/python3 # -*- coding: utf-8 -*- -import asyncio + +# 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 @@ -26,6 +34,7 @@ from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton class Essen: + """ Test Docstring """ def __init__(self, name, preis, kategorie): self.name = name self.preis = preis From 7755536da1a7be8684bb4cfc2b563e8ae80362f4 Mon Sep 17 00:00:00 2001 From: fspitzba Date: Thu, 15 Oct 2020 20:39:02 +0200 Subject: [PATCH 4/7] [COMMENT] Added Code Comments --- HSMensaW_botA.py | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/HSMensaW_botA.py b/HSMensaW_botA.py index 1222bff..ab01746 100644 --- a/HSMensaW_botA.py +++ b/HSMensaW_botA.py @@ -4,7 +4,8 @@ # since 3.5: type hints # def ( : -import asyncio # write concurrent code (not threading. not multiprocessing) +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 @@ -34,13 +35,34 @@ from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton class Essen: - """ Test Docstring """ - def __init__(self, name, preis, kategorie): + """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 + """ self.name = name self.preis = preis self.kategorie = kategorie + return None - def __str__(self): + def __str__(self) -> str: + """ + Formats the attribute of the Essen-Object to a string + + Parameters: + None + + Returns: + _ (string): formatted String of attributes + """ if self.preis > 0: return str("*%s*: `%s` (%.2f €)" % (self.kategorie, self.name, self.preis)) else: @@ -54,13 +76,15 @@ class Language(Enum): config_filename = "config.json" -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 " \ - "der aktuelle Speiseplan manuell abgerufen werden. Mit /settings kann (von Gruppenadmins) die Sprache " \ - "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) " +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 + der aktuelle Speiseplan manuell abgerufen werden. Mit /settings kann (von Gruppenadmins) die Sprache + 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 = [] From 052ad4414c08b2c3fb21b9ac42f00b39d93fd412 Mon Sep 17 00:00:00 2001 From: fspitzba Date: Sun, 25 Oct 2020 12:55:20 +0100 Subject: [PATCH 5/7] [UPDATE] Added Corona warning msg in german and english --- HSMensaW_botA.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/HSMensaW_botA.py b/HSMensaW_botA.py index 1222bff..4a8fa83 100644 --- a/HSMensaW_botA.py +++ b/HSMensaW_botA.py @@ -61,6 +61,18 @@ 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) " + +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.""" status = "" essen = [] essen_eng = [] @@ -232,12 +244,14 @@ 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") @@ -425,6 +439,8 @@ def process_json(d): return d +print(var_corona_msg_de) +print(var_corona_msg_eng) sys.stdout = open("out.log", "a") sys.stderr = open("err.log", "a") message_log = open("msg.log", "a") From 56a66f3c03fcc9439f9850b00f2cd047523ffedd Mon Sep 17 00:00:00 2001 From: fspitzba Date: Sun, 25 Oct 2020 23:32:14 +0100 Subject: [PATCH 6/7] corrected weirdo strings --- HSMensaW_botA.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/HSMensaW_botA.py b/HSMensaW_botA.py index 0da423c..b299c9d 100644 --- a/HSMensaW_botA.py +++ b/HSMensaW_botA.py @@ -77,19 +77,17 @@ 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_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.""" +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 From 4dd0e0b1d9931e508d9d4f759a13509a4a255420 Mon Sep 17 00:00:00 2001 From: fspitzba Date: Sun, 25 Oct 2020 23:32:14 +0100 Subject: [PATCH 7/7] corrected weirdo strings --- HSMensaW_botA.py | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/HSMensaW_botA.py b/HSMensaW_botA.py index 0da423c..b911540 100644 --- a/HSMensaW_botA.py +++ b/HSMensaW_botA.py @@ -77,29 +77,25 @@ 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_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.""" +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 -der aktuelle Speiseplan manuell abgerufen werden. Mit /settings kann (von Gruppenadmins) die Sprache -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) -""" +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 " \ + "der aktuelle Speiseplan manuell abgerufen werden. Mit /settings kann (von Gruppenadmins) die Sprache " \ + "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 = []