added sendMessage function checking if the bot was blocked and removing the id from the chat list if it was
Signed-off-by: klux2 <k.lux.gm@gmail.com>
This commit is contained in:
parent
8e04e905e6
commit
0cc781ac4e
1 changed files with 43 additions and 17 deletions
|
@ -7,11 +7,14 @@ import signal
|
||||||
import sys
|
import sys
|
||||||
import urllib.request
|
import urllib.request
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
|
from typing import Any
|
||||||
from urllib.error import HTTPError
|
from urllib.error import HTTPError
|
||||||
|
|
||||||
import telepot
|
import telepot
|
||||||
|
from telepot.aio import DelegatorBot
|
||||||
from telepot.aio.delegate import per_chat_id, create_open, pave_event_space
|
from telepot.aio.delegate import per_chat_id, create_open, pave_event_space
|
||||||
from telepot.aio.loop import MessageLoop
|
from telepot.aio.loop import MessageLoop
|
||||||
|
from telepot.exception import BotWasBlockedError
|
||||||
|
|
||||||
|
|
||||||
class Essen:
|
class Essen:
|
||||||
|
@ -55,29 +58,27 @@ class HSMensaW(telepot.aio.helper.ChatHandler):
|
||||||
if chat_id not in ids:
|
if chat_id not in ids:
|
||||||
ids.append(chat_id)
|
ids.append(chat_id)
|
||||||
config['ids'] = ids
|
config['ids'] = ids
|
||||||
await bot.sendMessage(chat_id, "Bot wurde aktiviert")
|
await send_message(bot_obj=bot, chat_id=chat_id, msg="Bot wurde aktiviert")
|
||||||
|
|
||||||
chat = get_chat_name(msg)
|
chat = get_chat_name(msg)
|
||||||
await send_status("Bot aktiviert für Chat %s (ID: %i)" % (chat, chat_id))
|
await send_status("Bot aktiviert für Chat %s (ID: %i)" % (chat, chat_id))
|
||||||
|
|
||||||
with open(config_filename, 'w') as outfile:
|
write_config()
|
||||||
json.dump(config, outfile)
|
|
||||||
else:
|
else:
|
||||||
await bot.sendMessage(chat_id, "Bot war bereits aktiviert")
|
await send_message(bot_obj=bot, chat_id=chat_id, msg="Bot war bereits aktiviert")
|
||||||
|
|
||||||
elif text.startswith("/stop"):
|
elif text.startswith("/stop"):
|
||||||
if chat_id in ids:
|
if chat_id in ids:
|
||||||
ids.remove(chat_id)
|
ids.remove(chat_id)
|
||||||
config['ids'] = ids
|
config['ids'] = ids
|
||||||
await bot.sendMessage(chat_id, "Bot wurde deaktiviert")
|
await send_message(bot_obj=bot, chat_id=chat_id, msg="Bot wurde deaktiviert")
|
||||||
|
|
||||||
chat = get_chat_name(msg)
|
chat = get_chat_name(msg)
|
||||||
await send_status("Bot deaktiviert für Chat %s (ID: %i)" % (chat, chat_id))
|
await send_status("Bot deaktiviert für Chat %s (ID: %i)" % (chat, chat_id))
|
||||||
|
|
||||||
with open(config_filename, 'w') as outfile:
|
write_config()
|
||||||
json.dump(config, outfile)
|
|
||||||
else:
|
else:
|
||||||
await bot.sendMessage(chat_id, "Bot war nicht aktiv")
|
await send_message(bot_obj=bot, chat_id=chat_id, msg="Bot war nicht aktiv")
|
||||||
|
|
||||||
elif text.startswith("/essen") or text.startswith("/mensa") or text.startswith("/speiseplan"):
|
elif text.startswith("/essen") or text.startswith("/mensa") or text.startswith("/speiseplan"):
|
||||||
chat = get_chat_name(msg)
|
chat = get_chat_name(msg)
|
||||||
|
@ -86,30 +87,31 @@ class HSMensaW(telepot.aio.helper.ChatHandler):
|
||||||
await get_essen(False)
|
await get_essen(False)
|
||||||
if len(essen) == 0:
|
if len(essen) == 0:
|
||||||
if var:
|
if var:
|
||||||
await bot.sendMessage(chat_id, "Es ist ein Fehler aufgetreten. Bitte später erneut versuchen.")
|
await send_message(bot_obj=bot, chat_id=chat_id,
|
||||||
|
msg="Es ist ein Fehler aufgetreten. Bitte später erneut versuchen.")
|
||||||
else:
|
else:
|
||||||
await bot.sendMessage(chat_id, "Für heute ist leider kein Speiseplan verfügbar.")
|
await send_message(bot_obj=bot, chat_id=chat_id,
|
||||||
|
msg="Für heute ist leider kein Speiseplan verfügbar.")
|
||||||
else:
|
else:
|
||||||
await send_essen(chat_id)
|
await send_essen(chat_id)
|
||||||
await send_status("Essen versendet für Chat %s (ID: %i)" % (chat, chat_id))
|
await send_status("Essen versendet für Chat %s (ID: %i)" % (chat, chat_id))
|
||||||
|
|
||||||
elif text.startswith("/status") and chat_id in config_ids:
|
elif text.startswith("/status") and chat_id in config_ids:
|
||||||
await bot.sendMessage(chat_id, status)
|
await send_message(bot_obj=bot, chat_id=chat_id, msg=status, parse_mode="markdown")
|
||||||
|
|
||||||
elif text.startswith("/info"):
|
elif text.startswith("/info"):
|
||||||
await bot.sendMessage(chat_id, info_str, "markdown")
|
await send_message(bot_obj=bot, chat_id=chat_id, msg=info_str, parse_mode="markdown")
|
||||||
|
|
||||||
elif content_type == "new_chat_member":
|
elif content_type == "new_chat_member":
|
||||||
if msg["new_chat_participant"]["id"] == get_bot_id():
|
if msg["new_chat_participant"]["id"] == get_bot_id():
|
||||||
await bot.sendMessage(chat_id, info_str, "markdown")
|
await send_message(bot_obj=bot, chat_id=chat_id, msg=info_str, parse_mode="markdown")
|
||||||
|
|
||||||
elif content_type == "left_chat_member":
|
elif content_type == "left_chat_member":
|
||||||
if msg["left_chat_participant"]["id"] == get_bot_id():
|
if msg["left_chat_participant"]["id"] == get_bot_id():
|
||||||
if chat_id in ids:
|
if chat_id in ids:
|
||||||
ids.remove(chat_id)
|
ids.remove(chat_id)
|
||||||
config['ids'] = ids
|
config['ids'] = ids
|
||||||
with open(config_filename, 'w') as outfile:
|
write_config()
|
||||||
json.dump(config, outfile)
|
|
||||||
|
|
||||||
|
|
||||||
async def send_essen(chat_id):
|
async def send_essen(chat_id):
|
||||||
|
@ -118,13 +120,37 @@ async def send_essen(chat_id):
|
||||||
nachricht = nachricht.replace("MONAT", monate[(datum.month - 1) % 12])
|
nachricht = nachricht.replace("MONAT", monate[(datum.month - 1) % 12])
|
||||||
for i in essen:
|
for i in essen:
|
||||||
nachricht += "- " + str(i).replace(".", ",") + "\n\n"
|
nachricht += "- " + str(i).replace(".", ",") + "\n\n"
|
||||||
await bot.sendMessage(chat_id, nachricht, "markdown")
|
send_message(bot_obj=bot, chat_id=chat_id, msg=nachricht, parse_mode="markdown")
|
||||||
|
|
||||||
|
|
||||||
async def send_status(text):
|
async def send_status(text):
|
||||||
global config_ids
|
global config_ids
|
||||||
for chat_id in config_ids:
|
for chat_id in config_ids:
|
||||||
await bot.sendMessage(chat_id, text)
|
send_message(bot_obj=bot, chat_id=chat_id, msg=text)
|
||||||
|
|
||||||
|
|
||||||
|
def send_message(bot_obj: DelegatorBot, chat_id: int, msg: str, parse_mode: Any = None,
|
||||||
|
disable_web_page_preview: Any = None, disable_notification: Any = None,
|
||||||
|
reply_to_message_id: Any = None, reply_markup: Any = None) -> None:
|
||||||
|
try:
|
||||||
|
bot_obj.sendMessage(chat_id=chat_id, text=msg, parse_mode=parse_mode,
|
||||||
|
disable_web_page_preview=disable_web_page_preview,
|
||||||
|
disable_notification=disable_notification, reply_to_message_id=reply_to_message_id,
|
||||||
|
reply_markup=reply_markup)
|
||||||
|
except BotWasBlockedError:
|
||||||
|
if chat_id in ids:
|
||||||
|
ids.remove(chat_id)
|
||||||
|
config['ids'] = ids
|
||||||
|
|
||||||
|
chat = get_chat_name(msg=msg)
|
||||||
|
await send_status("Bot wurde blockiert für Chat %s (ID: %i)" % (chat, chat_id))
|
||||||
|
|
||||||
|
write_config()
|
||||||
|
|
||||||
|
|
||||||
|
def write_config() -> None:
|
||||||
|
with open(config_filename, 'w') as outfile:
|
||||||
|
json.dump(config, outfile)
|
||||||
|
|
||||||
|
|
||||||
async def get_essen(only_today):
|
async def get_essen(only_today):
|
||||||
|
|
Loading…
Reference in a new issue