Compare commits

...

12 commits

Author SHA1 Message Date
fspitzba 4b7b99103f Merge branch 'flo-dev' 2020-10-25 23:58:53 +01:00
fspitzba 4dd0e0b1d9 corrected weirdo strings 2020-10-25 23:54:35 +01:00
fspitzba 56a66f3c03 corrected weirdo strings 2020-10-25 23:32:14 +01:00
fspitzba 9fab537a58 smth 2020-10-25 13:02:41 +01:00
fspitzba 052ad4414c [UPDATE] Added Corona warning msg in german and english 2020-10-25 12:55:20 +01:00
fspitzba 7755536da1 [COMMENT] Added Code Comments 2020-10-15 20:40:58 +02:00
fspitzba 08a4c4d3c1 changed gitignore, corrected tabs, added some comments 2020-10-13 22:35:14 +02:00
fspitzba 3f33a13f77 cleared watchdog out of README.md 2020-10-13 20:17:02 +02:00
fspitzba 2204bd5d9e [DELETE] deleted watchdog, it is not longer needed 2020-10-13 20:09:33 +02:00
fspitzba 44f359ed7f Merge branch 'ansgar-dev' into 'master'
[UPDATE] Minor changes in JSON formating and info text

See merge request fspitzba/hsmensaw!1
2020-10-13 19:48:50 +02:00
Ansgar Dabow 94a57ce845 [UPDATE] try pretty json format - may WIP 2020-09-11 11:31:48 +02:00
Ansgar Dabow ddcba9b8a7 clarify that this bot is inofficial 2020-09-11 11:28:14 +02:00
4 changed files with 53 additions and 41 deletions

3
.gitignore vendored
View file

@ -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
bot_test.py

View file

@ -1,6 +1,15 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# since 3.5: type hints
# def <name>(<args) -> <returntype>:
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,12 +35,34 @@ from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton
class Essen:
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:
@ -45,13 +76,27 @@ class Language(Enum):
config_filename = "config.json"
info_str = "*Mensa-Bot der Hochschule Mittweida*\nDieser Bot versendet jeden Tag um 10 Uhr den aktuellen " \
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 " \
"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 = []
@ -223,12 +268,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")
@ -266,7 +313,7 @@ async def send_message(bot_obj: DelegatorBot, chat_id: int, msg: str, parse_mode
def write_config() -> None:
with open(config_filename, 'w') as outfile:
json.dump(config, outfile, cls=EnumEncode)
json.dump(config, outfile, indent=4, sort_keys=True, cls=EnumEncode)
async def get_essen(only_today: bool) -> None:

View file

@ -75,23 +75,3 @@ Konfigurationsdatei könnte so aussehen:
],
"logging_enabled": false
}
### Start
### einfach
Der Bot kann über `python3 <Bot.py>` 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.

View file

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