ops bot: task lists go out one card per task with Сделано / Перенести buttons

This commit is contained in:
maksarsanjeev
2026-11-09 00:47:21 +03:00
parent 1889439ed7
commit a4f5a89026
2 changed files with 42 additions and 19 deletions
+32 -19
View File
@@ -89,27 +89,37 @@ async def send_long(update: Update, text: str, **kw):
await update.effective_message.reply_text(chunk, parse_mode=ParseMode.HTML, **(kw if last else {})) await update.effective_message.reply_text(chunk, parse_mode=ParseMode.HTML, **(kw if last else {}))
def task_buttons(tasks: list, limit: int = 12) -> InlineKeyboardMarkup | None: def one_task_buttons(task_id: int) -> InlineKeyboardMarkup:
"""One row per open task: ✓ completes, ⏰ snoozes a day — same callbacks as reminders.""" """Buttons under a single task card — same callbacks as reminders."""
rows = [] return InlineKeyboardMarkup([[
for t in tasks[:limit]: InlineKeyboardButton("✓ Сделано", callback_data=f"done:task:{task_id}"),
title = t["title"] if len(t["title"]) <= 20 else t["title"][:19] + "…" InlineKeyboardButton("⏰ Перенести", callback_data=f"snooze:task:{task_id}")]])
rows.append([InlineKeyboardButton(f"✓ {title}", callback_data=f"done:task:{t['id']}"),
InlineKeyboardButton("⏰", callback_data=f"snooze:task:{t['id']}")])
return InlineKeyboardMarkup(rows) if rows else None
def flat_tasks(tree: list) -> list: def flat_tasks(tree: list) -> list:
"""[(task, depth)] in display order."""
out = [] out = []
def walk(nodes): def walk(nodes, depth):
for n in nodes: for n in nodes:
out.append(n) out.append((n, depth))
walk(n["children"]) walk(n["children"], depth + 1)
walk(tree) walk(tree, 0)
return out return out
async def send_task_cards(update: Update, tree: list, limit: int = 12):
"""Every open task as its own message: task text, then Сделано / Перенести."""
tasks = flat_tasks(tree)
today = ops.today()
for t, depth in tasks[:limit]:
await update.effective_message.reply_text(fmt.task_line(t, depth, today),
parse_mode=ParseMode.HTML,
reply_markup=one_task_buttons(t["id"]))
if len(tasks) > limit:
await update.effective_message.reply_text(f"…и ещё {len(tasks) - limit}. Повтори /tasks.")
@owner_only @owner_only
async def cmd_start(update: Update, ctx): async def cmd_start(update: Update, ctx):
await update.message.reply_text( await update.message.reply_text(
@@ -123,16 +133,20 @@ async def cmd_start(update: Update, ctx):
async def cmd_today(update: Update, ctx): async def cmd_today(update: Update, ctx):
with lock: with lock:
agenda, tree = ops.agenda(), ops.task_tree() agenda, tree = ops.agenda(), ops.task_tree()
await send_long(update, fmt.agenda(agenda), reply_markup=task_buttons(flat_tasks(tree))) await send_long(update, fmt.agenda(agenda))
await send_task_cards(update, tree)
@owner_only @owner_only
async def cmd_tasks(update: Update, ctx): async def cmd_tasks(update: Update, ctx):
with lock: with lock:
tree = ops.task_tree() tree = ops.task_tree()
lines = fmt.tree(tree) if not tree:
await send_long(update, "\n".join(lines) if lines else "Открытых задач нет 👌", await update.effective_message.reply_text("Открытых задач нет 👌")
reply_markup=task_buttons(flat_tasks(tree))) return
await update.effective_message.reply_text(
f"Открытые задачи ({len(flat_tasks(tree))}) — по одной, жми Сделано или Перенести:")
await send_task_cards(update, tree)
@owner_only @owner_only
@@ -176,10 +190,9 @@ async def think(update: Update, text: str):
except Exception as e: # Kimi timeout, CLI missing… except Exception as e: # Kimi timeout, CLI missing…
log.exception("brain") log.exception("brain")
answer = f"Ошибка: {e.__class__.__name__}" answer = f"Ошибка: {e.__class__.__name__}"
markup = None await msg.edit_text(answer[:4000])
if "✓" not in answer and any(h in text.lower() for h in LIST_HINTS): if "✓" not in answer and any(h in text.lower() for h in LIST_HINTS):
markup = task_buttons(flat_tasks(locked(ops.task_tree))) await send_task_cards(update, locked(ops.task_tree))
await msg.edit_text(answer[:4000], reply_markup=markup)
for sid in paid: # paid by text: ask «repeat?» with buttons for sid in paid: # paid by text: ask «repeat?» with buttons
s = locked(ops.sub, sid) s = locked(ops.sub, sid)
if s["paid"] and s["active"]: if s["paid"] and s["active"]:
+10
View File
@@ -55,6 +55,16 @@ def tree(nodes: list, depth: int = 0) -> list[str]:
return out return out
def task_line(t: dict, depth: int, today: date) -> str:
"""One task as a standalone card text (sent as its own message with buttons)."""
mark = "⏱" if t["running"] else ("▶️" if t["status"] == "doing" else "📌")
due = f"\nСрок: {human_day(t['due'], today)}" if t["due"] else ""
mins = f"\nУчтено: {t['minutes']} мин" if t["minutes"] else ""
client = f" [{client_name(t['client'])}]" if t["client"] else ""
indent = "└ " * depth
return f"{indent}{mark} <b>{escape(t['title'])}</b>{client}{due}{mins}"
def subs(rows: list, by_client: dict, today: date) -> str: def subs(rows: list, by_client: dict, today: date) -> str:
"""Grouped by company, each with its monthly total.""" """Grouped by company, each with its monthly total."""
if not rows: if not rows: