From 0e9cc463c1263a0889f78d7198131c93ca457e94 Mon Sep 17 00:00:00 2001 From: klux2 Date: Tue, 25 Jun 2019 10:53:30 +0200 Subject: [PATCH] enabling/disabling logging with chat commands Signed-off-by: klux2 --- HSMensaW_botA.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/HSMensaW_botA.py b/HSMensaW_botA.py index 67bd57c..faf6884 100644 --- a/HSMensaW_botA.py +++ b/HSMensaW_botA.py @@ -48,7 +48,7 @@ datum = None class HSMensaW(telepot.aio.helper.ChatHandler): @staticmethod async def on_chat_message(msg: dict) -> None: - global config, essen, status, var + global config, essen, status, var, logging_enabled content_type, chat_type, chat_id = telepot.glance(msg) if content_type == "text": @@ -102,6 +102,21 @@ class HSMensaW(telepot.aio.helper.ChatHandler): elif text.startswith("/info"): await send_message(bot_obj=bot, chat_id=chat_id, msg=info_str, parse_mode="markdown") + elif text.startswith("/logging") and chat_id in config_ids: + if text.endswith("on") or text.endswith("1") or text.lower().endswith("true"): + logging_enabled = True + config["logging_enabled"] = True + write_config() + await send_message(bot_obj=bot, chat_id=chat_id, msg="logging enabled", parse_mode="markdown") + elif text.endswith("off") or text.endswith("0") or text.lower().endswith("false"): + logging_enabled = False + config["logging_enabled"] = False + write_config() + await send_message(bot_obj=bot, chat_id=chat_id, msg="logging disabled", parse_mode="markdown") + else: + message = "logging enabled: %s (change with on/off or true/false or 1/0)" % logging_enabled + await send_message(bot_obj=bot, chat_id=chat_id, msg=message, parse_mode="markdown") + elif content_type == "new_chat_member": if msg["new_chat_participant"]["id"] == get_bot_id(): await send_message(bot_obj=bot, chat_id=chat_id, msg=info_str, parse_mode="markdown") @@ -125,8 +140,9 @@ async def send_essen(chat_id: int) -> None: async def send_status(text: str) -> None: global config_ids - for chat_id in config_ids: - await send_message(bot_obj=bot, chat_id=chat_id, msg=text) + if logging_enabled: + for chat_id in config_ids: + await send_message(bot_obj=bot, chat_id=chat_id, msg=text) async def send_message(bot_obj: DelegatorBot, chat_id: int, msg: str, parse_mode: Any = None, @@ -275,6 +291,7 @@ telegram_bot_token = config.get("telegram_bot_token") url = config.get("url") ids = config.get("ids") config_ids = config.get("config_ids") +logging_enabled = config.get("logging_enabled") bot = telepot.aio.DelegatorBot(telegram_bot_token, [ pave_event_space()(