small refactor

This commit is contained in:
kiriharu 2021-06-01 21:16:51 +03:00
parent 3ca281627f
commit 00ea38b251
3 changed files with 10 additions and 7 deletions

View File

@ -49,10 +49,13 @@ class CheckerBaseHandler(SimpleCommandHandler):
def __init__(self):
pass
async def check(self, chat_id: int, bot: Bot, data: dict):
async def check(self, msg: Message, data: dict):
# TODO: start check and end check metrics with ident, chat_id and api_endpoint
ts = time()
ident = uuid4().hex
# refactoring goes brr
chat_id = msg.chat.id
bot = msg.bot
logger.info(f"User {chat_id} started check {ident}")
# format header
rsp_msg = await bot.send_message(
@ -71,8 +74,9 @@ class CheckerBaseHandler(SimpleCommandHandler):
iter_keys = iter_keys + 1
if NOTIFY_CHECKS:
notify_text = f"**User {chat_id} issued check: {self.api_endpoint} for {data['target_fq']}**\n\n" \
f"```{rsp_msg.text}```"
notify_text = f"**User {msg.from_user.full_name} (@{msg.from_user.username}) ({chat_id}) issued check: " \
f"{self.api_endpoint} for {data['target_fq']}**\n\n" \
f"```\n{rsp_msg.text}\n```"
await send_message_to_admins(notify_text)
logger.info(f"User {chat_id} ended check {ident}")
@ -108,8 +112,7 @@ class CheckerTargetPortHandler(CheckerBaseHandler):
logger.info(f"User {message.from_user.id} got LocalhostForbidden error")
return await message.answer(self.localhost_forbidden_message, parse_mode="Markdown")
await self.check(
message.chat.id,
message.bot,
message,
dict(target=args[0], port=args[1], target_fq=f"{args[0]}:{args[1]}")
)

View File

@ -30,7 +30,7 @@ class ICMPCheckerHandler(CheckerBaseHandler):
except LocalhostForbidden:
return await message.answer(self.localhost_forbidden_message, parse_mode="Markdown")
else:
await self.check(message.chat.id, message.bot, dict(target=args[0], target_fq=args[0]))
await self.check(message, dict(target=args[0], target_fq=args[0]))
def process_args(self, text: str) -> list:
args = text.split()

View File

@ -27,7 +27,7 @@ start_message = """
🚩 [Этот бот с открытым с исходным кодом](https://github.com/catspace-dev/unicheckbot)
🚩 [Помогите улучшить бота](https://github.com/catspace-dev/unicheckbot/issues) или [расскажите об ошибке](https://github.com/catspace-dev/unicheckbot/issues)
Разработчик: [kiriharu](http://t.me/kiriharu)
Разработчик: [kiriharu](https://t.me/kiriharu)
При поддержке: [SpaceCore.pro](https://spacecore.pro/)
"""