From 3b4e9d6a1e8dcff6fc015222ba4046602ab02fca Mon Sep 17 00:00:00 2001 From: Nikita Tyukalov Date: Fri, 21 Aug 2026 08:29:23 +0300 Subject: [PATCH] Added !tokens commands basic implementation --- bot_callbacks.py | 39 +++++++++++++++++++++++++++++++++++++++ util.py | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/bot_callbacks.py b/bot_callbacks.py index 823638a..6c05ab6 100644 --- a/bot_callbacks.py +++ b/bot_callbacks.py @@ -1,10 +1,12 @@ """Callbacks for matrix messages""" import html +import time import traceback from nio import MatrixRoom, RoomMessageText +import util from bot import MatrixBot # @@ -69,9 +71,46 @@ async def _on_cmd_info(room: MatrixRoom, args: list[str]) -> None: response += f"channel: {html.escape(room.room_id)}" await _bot.send_text_to_room(room.room_id, response, is_html=True) +async def _on_cmd_tokens(room: MatrixRoom, args: list[str]) -> None: + """!info""" + TEST_LIST = { + "t0K3n": { + "created": time.time() - 60, + "last_use": time.time() - 30, + "total_uses": 534, + "service_name": "Резервное копирование", + }, + "T35t_t0KeN": { + "created": time.time() - 12341, + "last_use": time.time() - 123, + "total_uses": 342, + "service_name": None, + }, + } + + response = "Список токенов
    " + for token in TEST_LIST: + data = TEST_LIST[token] + response += "
  • " + response += f"{html.escape(token)}
      " + + if data["service_name"] is not None: + response += f"
    • Имя службы: {data["service_name"]}
    • " + else: + response += f"
    • Имя службы: указывается в запросе
    • " + response += f"
    • Создан: {util.date_to_text(data["created"])}
    • " + response += f"
    • Последнее использование: {util.date_to_text(data["last_use"])}
    • " + response += f"
    • Всего использований: {util.date_to_text(data["total_uses"])}
    • " + + response += "
    " + response += "
  • " + response += "
" + await _bot.send_text_to_room(room.room_id, response, is_html=True) + _COMMANDS = { ("help", "?"): (_on_cmd_help, "Получить справку"), ("info", "room"): (_on_cmd_info, "Получить сведения о комнате"), + ("tokens",): (_on_cmd_tokens, "Получить список токенов"), } # diff --git a/util.py b/util.py index b26c8a3..d671aff 100644 --- a/util.py +++ b/util.py @@ -8,6 +8,7 @@ import os import json import logging import traceback +import datetime from html.parser import HTMLParser from pathlib import Path from urllib.parse import urlparse @@ -161,4 +162,38 @@ def check_and_remove_html(possible_html: str) -> tuple[bool, str]: except: traceback.print_exc() - return (False, possible_html) \ No newline at end of file + return (False, possible_html) + +def date_to_text(date: datetime.datetime, dow: bool = True, seconds: bool = True) -> str: + ''' Returns date as formatted string. + Day of week can be added. + Seconds can be added. + ''' + if type(date) is float: + date = int(date) + if type(date) is int: + date = datetime.datetime.utcfromtimestamp(date) + + # format string + format_string = '' + if dow: + format_string += '%a, ' + format_string += '%d.%m.%Y, %H:%M' + if seconds: + format_string += ':%S' + # day of week to Russian + translate_map = [ + ('Mon', 'Пн'), + ('Tue', 'Вт'), + ('Wed', 'Ср'), + ('Thu', 'Чт'), + ('Fri', 'Пт'), + ('Sat', 'Сб'), + ('Sun', 'Вс') + ] + result = date.strftime(format_string) + for en, ru in translate_map: + if en in result: + result = result.replace(en, ru) + break + return result \ No newline at end of file