fix(send): helpful error when --file gets a binary; document MEDIA: attachments (#45116)

A user passing an image to `hermes send --file` got a raw
UnicodeDecodeError ('utf-8 codec can't decode byte 0x89...') with no
hint that media delivery goes through the MEDIA:<path> directive.

- send_cmd: catch UnicodeDecodeError separately and print a usage error
  explaining --file is for text bodies, with copy-pasteable MEDIA: and
  [[as_document]] examples using the user's own path
- --file help text + epilog now mention MEDIA:
- docs: new 'Sending images and other media' section on the hermes send
  reference page
This commit is contained in:
Teknium 2026-06-12 11:48:06 -07:00 committed by GitHub
parent 652dd9c9f2
commit fa5e98facb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 41 additions and 5 deletions

View file

@ -59,7 +59,20 @@ def _read_message_body(
return sys.stdin.read()
try:
return Path(file_path).read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError) as exc:
except UnicodeDecodeError:
print(
f"hermes send: {file_path} is not a text file. --file reads the "
"message *body* (logs, reports, markdown).\n"
"To send an image/document/audio file as a native attachment, "
"reference it with MEDIA: in the message text instead:\n"
f' hermes send --to telegram "MEDIA:{file_path}"\n'
f' hermes send --to telegram "optional caption MEDIA:{file_path}"\n'
"Add [[as_document]] to deliver an image as an uncompressed file:\n"
f' hermes send --to telegram "[[as_document]] MEDIA:{file_path}"',
file=sys.stderr,
)
sys.exit(_USAGE_EXIT)
except OSError as exc:
print(f"hermes send: cannot read {file_path}: {exc}", file=sys.stderr)
sys.exit(_USAGE_EXIT)
@ -367,6 +380,7 @@ def register_send_subparser(subparsers) -> argparse.ArgumentParser:
" echo \"RAM 92%\" | hermes send --to telegram:-1001234567890\n"
" hermes send --to discord:#ops --file /tmp/report.md\n"
" hermes send --to slack:#eng --subject \"[CI]\" --file build.log\n"
" hermes send --to telegram \"MEDIA:/tmp/chart.png\" # send a media attachment\n"
" hermes send --list # all platforms\n"
" hermes send --list telegram # filter by platform\n"
"\n"
@ -403,7 +417,11 @@ def register_send_subparser(subparsers) -> argparse.ArgumentParser:
"--file",
metavar="PATH",
default=None,
help="Read message body from PATH. Use '-' to force stdin.",
help=(
"Read message body from PATH (text only). Use '-' to force stdin. "
"To send an image/document as an attachment, use MEDIA:<path> in "
"the message text instead."
),
)
parser.add_argument(