def log_action(update, context, *args, **kwargs): result = func(update, context, *args, **kwargs) chat = update.effective_chat # type: Optional[Chat] message = update.effective_message # type: Optional[Message] if result: if chat.type == chat.SUPERGROUP and chat.username: result += ( "\n<b>Link:</b> " '<a href="http://telegram.me/{}/{}">click here</a>'. format(chat.username, message.message_id)) log_chat = sql.get_chat_log_channel(chat.id) if log_chat: try: send_log(context.bot, log_chat, chat.id, result) except Unauthorized: sql.stop_chat_logging(chat.id) elif result == "": pass else: LOGGER.warning( "%s was set as loggable, but had no return statement.", func) return result
def send_log(bot: Bot, log_chat_id: str, orig_chat_id: str, result: str): try: bot.send_message(log_chat_id, result, parse_mode=ParseMode.HTML) except BadRequest as excp: if excp.message == "Chat not found": bot.send_message(orig_chat_id, "This log channel has been deleted - unsetting.") sql.stop_chat_logging(orig_chat_id) else: LOGGER.warning(excp.message) LOGGER.warning(result) LOGGER.exception("Could not parse") bot.send_message(log_chat_id, result + "\n\nFormatting has been disabled due to an unexpected error.")
def unsetlog(update, context): message = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] log_channel = sql.stop_chat_logging(chat.id) if log_channel: context.bot.send_message(log_channel, "Channel has been unlinked from {}".format(chat.title)) message.reply_text("Log channel has been un-set.") else: message.reply_text("No log channel has been set yet!")