Added !tokens commands basic implementation
This commit is contained in:
@@ -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"<b>channel: <b><code>{html.escape(room.room_id)}</code>"
|
||||
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 = "<b><i>Список токенов</i></b><ul>"
|
||||
for token in TEST_LIST:
|
||||
data = TEST_LIST[token]
|
||||
response += "<li>"
|
||||
response += f"<code>{html.escape(token)}</code><ul>"
|
||||
|
||||
if data["service_name"] is not None:
|
||||
response += f"<li><b>Имя службы:</b> <i>{data["service_name"]}</i></li>"
|
||||
else:
|
||||
response += f"<li><b>Имя службы:</b> <i>указывается в запросе</i></li>"
|
||||
response += f"<li><b>Создан:</b> <i>{util.date_to_text(data["created"])}</i></li>"
|
||||
response += f"<li><b>Последнее использование:</b> <i>{util.date_to_text(data["last_use"])}</i></li>"
|
||||
response += f"<li><b>Всего использований:</b> <i>{util.date_to_text(data["total_uses"])}</i></li>"
|
||||
|
||||
response += "</ul>"
|
||||
response += "</li>"
|
||||
response += "</ul>"
|
||||
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, "Получить список токенов"),
|
||||
}
|
||||
|
||||
#
|
||||
|
||||
37
util.py
37
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)
|
||||
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
|
||||
Reference in New Issue
Block a user