send schedule upload result as file
This commit is contained in:
parent
7dbfb29a21
commit
1003afd6e8
1 changed files with 11 additions and 8 deletions
|
|
@ -5,11 +5,12 @@ import json
|
|||
import secrets
|
||||
import tempfile
|
||||
from dataclasses import dataclass
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import requests
|
||||
from telebot.types import CallbackQuery, Message
|
||||
from telebot.types import CallbackQuery, InputFile, Message
|
||||
from telebot.util import quick_markup
|
||||
|
||||
import config
|
||||
|
|
@ -69,12 +70,14 @@ def _clear_database() -> dict[str, Any]:
|
|||
return response.json()
|
||||
|
||||
|
||||
def _format_upload_result(payload: dict[str, Any]) -> str:
|
||||
serialized = json.dumps(payload, ensure_ascii=False, indent=2)
|
||||
if len(serialized) > 3400:
|
||||
serialized = serialized[:3400] + "\n..."
|
||||
heading = "База применена" if payload.get("ok") else "Архив обработан с ошибками"
|
||||
return f"<b>{heading}, результат:</b>\n<pre>{html.escape(serialized)}</pre>"
|
||||
def _send_upload_result(admin_id: int, payload: dict[str, Any]):
|
||||
content = json.dumps(payload, ensure_ascii=False, indent=2).encode("utf-8")
|
||||
caption = "База применена" if payload.get("ok") else "Архив обработан с ошибками"
|
||||
return bot.send_document(
|
||||
admin_id,
|
||||
InputFile(BytesIO(content), "schedule-upload-result.json"),
|
||||
caption=caption,
|
||||
)
|
||||
|
||||
|
||||
def _format_request_error(error: requests.RequestException) -> str:
|
||||
|
|
@ -154,7 +157,7 @@ def upload_schedule_archive(call: CallbackQuery):
|
|||
PENDING_ARCHIVES.pop(token, None)
|
||||
archive.path.unlink(missing_ok=True)
|
||||
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))
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue