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:
klux2 2019-05-12 15:09:29 +02:00
parent 8e04e905e6
commit 0cc781ac4e

View file

@ -7,11 +7,14 @@ import signal
import sys
import urllib.request
import xml.etree.ElementTree as ET
from typing import Any
from urllib.error import HTTPError
import telepot
from telepot.aio import DelegatorBot
from telepot.aio.delegate import per_chat_id, create_open, pave_event_space
from telepot.aio.loop import MessageLoop
from telepot.exception import BotWasBlockedError
class Essen:
@ -55,29 +58,27 @@ class HSMensaW(telepot.aio.helper.ChatHandler):
if chat_id not in ids:
ids.append(chat_id)
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)
await send_status("Bot aktiviert für Chat %s (ID: %i)" % (chat, chat_id))
with open(config_filename, 'w') as outfile:
json.dump(config, outfile)
write_config()
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"):
if chat_id in ids:
ids.remove(chat_id)
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)
await send_status("Bot deaktiviert für Chat %s (ID: %i)" % (chat, chat_id))
with open(config_filename, 'w') as outfile:
json.dump(config, outfile)
write_config()
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"):
chat = get_chat_name(msg)
@ -86,30 +87,31 @@ class HSMensaW(telepot.aio.helper.ChatHandler):
await get_essen(False)
if len(essen) == 0:
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:
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:
await send_essen(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:
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"):
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":
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":
if msg["left_chat_participant"]["id"] == get_bot_id():
if chat_id in ids:
ids.remove(chat_id)
config['ids'] = ids
with open(config_filename, 'w') as outfile:
json.dump(config, outfile)
write_config()
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])
for i in essen:
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):
global 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):