Limit task lists and clamp message length
All checks were successful
publish-images / build (push) Successful in 6s
All checks were successful
publish-images / build (push) Successful in 6s
This commit is contained in:
27
bot/main.py
27
bot/main.py
@@ -41,14 +41,33 @@ def build_keyboard() -> InlineKeyboardMarkup:
|
||||
|
||||
|
||||
async def render_view(view: View, client: BoincClient) -> str:
|
||||
MAX_ITEMS = 20
|
||||
snapshot = await client.fetch_snapshot()
|
||||
if view == "dashboard":
|
||||
return format_dashboard(snapshot)
|
||||
if view == "active":
|
||||
return format_tasks_section("Активные задачи", snapshot.active)
|
||||
extra = len(snapshot.active) - MAX_ITEMS
|
||||
text = format_tasks_section("Активные задачи", snapshot.active[:MAX_ITEMS])
|
||||
if extra > 0:
|
||||
text += f"\n... и еще {extra} задач(и)"
|
||||
return text
|
||||
if view == "queued":
|
||||
return format_tasks_section("Ожидают запуска", snapshot.queued)
|
||||
return format_tasks_section("Готовы к отправке", snapshot.completed)
|
||||
extra = len(snapshot.queued) - MAX_ITEMS
|
||||
text = format_tasks_section("Ожидают запуска", snapshot.queued[:MAX_ITEMS])
|
||||
if extra > 0:
|
||||
text += f"\n... и еще {extra} задач(и)"
|
||||
return text
|
||||
extra = len(snapshot.completed) - MAX_ITEMS
|
||||
text = format_tasks_section("Готовы к отправке", snapshot.completed[:MAX_ITEMS])
|
||||
if extra > 0:
|
||||
text += f"\n... и еще {extra} задач(и)"
|
||||
return text
|
||||
|
||||
|
||||
def clamp_message(text: str, limit: int = 3900) -> str:
|
||||
if len(text) <= limit:
|
||||
return text
|
||||
return text[: limit - 80] + "\n...сообщение укорочено, отфильтруйте список"
|
||||
|
||||
|
||||
async def start_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
@@ -63,7 +82,7 @@ async def callback_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -
|
||||
client: BoincClient = context.bot_data["boinc_client"]
|
||||
view: View = query.data if query.data in {"dashboard", "active", "queued", "completed"} else "dashboard"
|
||||
try:
|
||||
text = await render_view(view, client)
|
||||
text = clamp_message(await render_view(view, client))
|
||||
except Exception as exc: # broad catch to show meaningful message in chat
|
||||
logger.exception("Error rendering view %s", view)
|
||||
text = f"Не удалось получить данные: {exc}"
|
||||
|
||||
Reference in New Issue
Block a user