send schedule upload result as file

This commit is contained in:
Server 2026-08-30 14:13:17 +00:00
parent 7dbfb29a21
commit 1003afd6e8

View file

@ -5,11 +5,12 @@ import json
import secrets import secrets
import tempfile import tempfile
from dataclasses import dataclass from dataclasses import dataclass
from io import BytesIO
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import requests import requests
from telebot.types import CallbackQuery, Message from telebot.types import CallbackQuery, InputFile, Message
from telebot.util import quick_markup from telebot.util import quick_markup
import config import config
@ -69,12 +70,14 @@ def _clear_database() -> dict[str, Any]:
return response.json() return response.json()
def _format_upload_result(payload: dict[str, Any]) -> str: def _send_upload_result(admin_id: int, payload: dict[str, Any]):
serialized = json.dumps(payload, ensure_ascii=False, indent=2) content = json.dumps(payload, ensure_ascii=False, indent=2).encode("utf-8")
if len(serialized) > 3400: caption = "База применена" if payload.get("ok") else "Архив обработан с ошибками"
serialized = serialized[:3400] + "\n..." return bot.send_document(
heading = "База применена" if payload.get("ok") else "Архив обработан с ошибками" admin_id,
return f"<b>{heading}, результат:</b>\n<pre>{html.escape(serialized)}</pre>" InputFile(BytesIO(content), "schedule-upload-result.json"),
caption=caption,
)
def _format_request_error(error: requests.RequestException) -> str: def _format_request_error(error: requests.RequestException) -> str:
@ -154,7 +157,7 @@ def upload_schedule_archive(call: CallbackQuery):
PENDING_ARCHIVES.pop(token, None) PENDING_ARCHIVES.pop(token, None)
archive.path.unlink(missing_ok=True) archive.path.unlink(missing_ok=True)
clear_schedule_cache() clear_schedule_cache()
return send(call.from_user.id, _format_upload_result(payload)) return _send_upload_result(call.from_user.id, payload)
@bot.message_handler(commands=["reset_schedule_db"], func=lambda message: _is_admin(message.from_user.id)) @bot.message_handler(commands=["reset_schedule_db"], func=lambda message: _is_admin(message.from_user.id))