fix(brother-printer): support --thread-id for forum topics and prevent double ask_user prompts

This commit is contained in:
Moritz Graf
2026-09-17 09:05:01 +02:00
parent b104b2f6a2
commit 0162f9dcd3
2 changed files with 26 additions and 14 deletions
+20 -12
View File
@@ -17,8 +17,10 @@ Direct network printing to the **Brother HL-L8240CDW** duplex color laser printe
## 🕹️ Interactive Telegram Options Protocol (CRITICAL)
When the user requests a print (e.g. *"Bitte drucke die Rechnung"* or sends a PDF), **DO NOT print blindly immediately**.
Provide an interactive decision prompt on Telegram using the `ask_user` tool with pre-configured defaults:
When the user requests a print without specifying options (e.g. *"Bitte drucke die Rechnung"* or sends a PDF):
- **Single Invocation**: Call `ask_user` exactly ONCE. Do NOT print duplicate text option lists in the chat; `ask_user` renders the interactive buttons natively.
- **Skip if Already Specified**: If the user already gave preferences in their prompt (e.g. *"Druck das in Farbe"*, *"Drucke das bitte schwarz-weiß beidseitig auf 2 Seiten"*), **DO NOT call `ask_user`** — execute the print job immediately with their specified options!
- **Never Duplicate or Retry**: Once `ask_user` is sent, wait for the user to tap an option. Never re-trigger `ask_user` for the same document.
### Fast-Path Preset Menu via `ask_user`
@@ -40,25 +42,30 @@ Call `ask_user` with:
`description`: `"Schwarz-Weiß, 1 Seite pro Blatt, nur Vorderseite"`
*(Maps to: `--color monochrome --nup 1 --duplex one-sided`)*
> [!TIP]
> If the user specifically asks for custom parameters (e.g. *"Druck das in Farbe auf 2 Seiten"*), parse the options directly and run without extra prompt.
---
## 📍 Active Conversation & Thread Routing (CRITICAL)
**Never hardcode a chat ID!** To ensure status updates appear in the exact conversation and topic where requested (whether requested by Moritz, Amelie, or in a group topic like the Mila topic):
1. **`--chat-id`**: Set to the CURRENT session's chat ID (e.g. `-1004326921510` for group chat, or user ID if in private DM).
2. **`--thread-id`**: If the incoming message came from a forum topic / thread (e.g. `threadId: 22` / `message_thread_id: 22`), **YOU MUST PASS `--thread-id <threadId>`**. Without `--thread-id`, Telegram routes messages to the General topic instead of the active conversation thread!
---
## 🚀 Execution & Live Telegram Status Updates
Execute the bundled driver script with `uv run`.
Execute the bundled driver script with `uv run`:
Always pass `--chat-id <chat_id>` so the script automatically creates and live-updates a single Telegram message (`⏳ Bereite vor...` ➔ `📡 Sende an Drucker...` ➔ `🖨️ Druckt...` ➔ `✅ Erfolgreich gedruckt!`):
### 1. Print a PDF file with selected options
### 1. Print a PDF file with selected options (in a topic or group)
```bash
uv run /home/node/mop-skills/mop-skills/skills/brother-printer/scripts/brother_printer.py print \
--file "/home/node/.openclaw/workspace/document.pdf" \
--color monochrome \
--nup 2 \
--duplex two-sided-long-edge \
--chat-id "306373425"
--chat-id "<current_chat_id>" \
--thread-id "<current_thread_id>"
```
### 2. Print a document from a URL
@@ -69,7 +76,7 @@ uv run /home/node/mop-skills/mop-skills/skills/brother-printer/scripts/brother_p
--color color \
--nup 1 \
--duplex two-sided-long-edge \
--chat-id "306373425"
--chat-id "<current_chat_id>"
```
### 3. Print plain text or notes
@@ -80,7 +87,7 @@ uv run /home/node/mop-skills/mop-skills/skills/brother-printer/scripts/brother_p
--color monochrome \
--nup 1 \
--duplex one-sided \
--chat-id "306373425"
--chat-id "<current_chat_id>"
```
### 4. Check status & toner levels
@@ -102,6 +109,7 @@ uv run /home/node/mop-skills/mop-skills/skills/brother-printer/scripts/brother_p
| `--nup` | `1`, `2`, `4` | `1` | Hardware pages per sheet (1 = normal, 2 = 2 pages/sheet) |
| `--duplex` | `two-sided-long-edge`, `one-sided`, `two-sided-short-edge` | `two-sided-long-edge` | Duplex binding mode |
| `--copies` | Integer | `1` | Number of printed copies |
| `--chat-id` | Telegram Chat ID | None | Target chat ID for live status updates |
| `--chat-id` | Telegram Chat ID | None | Target chat ID for live status updates (use current chat) |
| `--thread-id` | Integer | None | Target forum topic / message thread ID (mandatory for topics) |
| `--max-pages`| Integer | `30` | Safety limit to prevent runaway printing |
| `--force` | Flag | `false` | Bypass page limit |
@@ -50,9 +50,10 @@ TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN")
# ---------------------------------------------------------------------------
class TelegramNotifier:
def __init__(self, token: Optional[str] = None, chat_id: Optional[str] = None, message_id: Optional[int] = None):
def __init__(self, token: Optional[str] = None, chat_id: Optional[str] = None, thread_id: Optional[int] = None, message_id: Optional[int] = None):
self.token = token or TELEGRAM_BOT_TOKEN
self.chat_id = chat_id
self.thread_id = int(thread_id) if thread_id else None
self.message_id = int(message_id) if message_id else None
def can_notify(self) -> bool:
@@ -67,6 +68,8 @@ class TelegramNotifier:
"text": text,
"parse_mode": "Markdown",
}
if self.thread_id:
payload["message_thread_id"] = self.thread_id
try:
resp = requests.post(url, json=payload, timeout=8)
if resp.ok:
@@ -281,7 +284,7 @@ def handle_validate(args):
def handle_print(args):
notifier = TelegramNotifier(chat_id=args.chat_id, message_id=args.message_id)
notifier = TelegramNotifier(chat_id=args.chat_id, thread_id=args.thread_id, message_id=args.message_id)
# 1. Resolve source document
doc_title = args.title or "Dokument"
@@ -464,6 +467,7 @@ def main():
print_parser.add_argument("--max-pages", type=int, default=30, help="Safety page limit (default 30)")
print_parser.add_argument("--force", action="store_true", help="Bypass max pages guardrail")
print_parser.add_argument("--chat-id", help="Telegram Chat ID for live updates")
print_parser.add_argument("--thread-id", type=int, help="Telegram Thread / Topic ID (for forum supergroups)")
print_parser.add_argument("--message-id", help="Telegram Message ID to edit (optional, created if omitted)")
print_parser.set_defaults(func=handle_print)