Files
Linux_post_install/DOC/howto/communication.md
T
Your Name 873ae84b49 refactor: rename telegram tool to pos-communication-telegram-sender; drop --send legacy alias
The --send flag duplicated the send subcommand — completion suggested both.
Now 'pos communication telegram <TAB>' completes to just 'sender' and
'listener'. send is the single canonical action.

- git mv bin/pos-communication-telegram bin/pos-communication-telegram-sender
- remove --send branch + POS_FLAGS entry; add --markdown to completion
- lib/notify.sh: notify_sender_name() maps platform telegram -> telegram-sender
  (notify_send otherwise looks for bin/pos-communication-telegram)
- pos-system-health --send 'sent:' check + pos-entertainment-send use the new
  name and the send subcommand
- completion: keys with nested tools but no direct tool complete to the group
  suffixes (sender/listener); --type/--parse-mode value completion moved to
  the new word positions
- docs updated (POS.md, AGENT_Context, DEV.md, SCRIPTS.md, notify.env,
  HOWTO.md, postinstall.sh); removed phantom webhook/log/broadcast subcommands
  from howto/communication.md
2026-08-09 03:28:30 -04:00

137 lines
5.5 KiB
Markdown

# How-To: `pos communication`
Telegram messaging and alerts. Tools: `telegram-sender`, `telegram-listener`,
`matrix`.
| Tool | What it does |
|------|--------------|
| `pos communication telegram sender` | Send messages/files/links/stickers, test, config (token + chat id) |
| `pos communication telegram listener` | Bot listener: map `/command` → bash and run it from chat (systemd user daemon) |
| `pos communication matrix` | Matrix/Synapse sender (extensible; not yet implemented) |
`telegram-sender` is the workhorse: it backs the whole **notify system**
health digests, backup alerts, firewall changes — and can be used directly.
---
## `pos communication telegram sender`
### One-time setup
```bash
pos communication telegram sender config set TELEGRAM_BOT_TOKEN=123456:ABC...
pos communication telegram sender config set TELEGRAM_CHAT_ID=987654321
pos communication telegram sender config
# config lives in ~/.config/linux_post_install/telegram.env (chmod 600)
```
The bot token comes from @BotFather, the chat ID from @userinfobot (or by
starting a chat and reading it). `sender config` shows the configured chat id.
### Send
```bash
pos communication telegram sender send "hello from my server" # plain text
pos communication telegram sender send --markdown "**bold** ok" # parse as markdown
pos communication telegram sender send --help # list all flags
pos communication telegram sender send /path/to/report.pdf # auto-detects file
```
**Recipes:**
- **Alert when a backup finishes** — automatic: `pos system backup` calls
`notify_send` (below) on success *and* failure.
- **Warn before a service update:**
```bash
pos communication telegram sender send "Maintenance: docker compose down in 2min"
```
- **On-call file drop:** `pos communication telegram sender send ~/log/nginx-error.log`
**Troubleshooting:**
- "Not configured (no token or chat id)" → run `sender config set` for both values.
- Send succeeds but nothing arrives → the chat must have started the bot
(press `Start` / send `/start` once).
- **Markdown silently empty** → Telegram uses its own MarkdownV2; unmatched
syntax makes the message vanish. Use `--markdown` only when the text is
Telegram-safe (the health digest output is).
- **Webhook vs getUpdates:** the sender uses polling (`getUpdates`), so a
webhook registered on the bot (e.g. via BotFather) blocks sends; remove it
with BotFather's `/deletewebhook`.
---
## `pos communication telegram listener`
Turns your bot into a two-way remote control: map `/command` → bash, then
message the bot from your phone to run it.
```bash
pos communication telegram listener # edit the /command → bash map
pos communication telegram listener --status # service state + mapped commands
pos communication telegram listener --enable # install the systemd user daemon
pos communication telegram listener --disable # remove it
```
- **Map file:** `~/.config/linux_post_install/telegram_commands.env` (chmod 600),
one `/cmd=bash command` per line — re-read on every message, so edits apply
instantly. Example:
```
/status=@quiet pos system health --send
/temp=sensors | grep -i 'Tctl\|package id 0'
/update=cd /path/to/repo && git pull
```
- **Owner-only:** the bot only reacts to `TELEGRAM_CHAT_ID` (your own chat);
others are ignored. `/help` lists mapped commands; unknown → "Unknown command".
- **Runs as you:** mapped commands execute as your user with a 60s timeout,
stdout + stderr are replied to the chat (truncated ~3800 chars; empty → `OK`).
`sudo` inside a command needs a NOPASSWD rule.
- **`@quiet` prefix:** a map value starting with `@quiet ` runs the command but
does NOT reply — for commands that already send their own notification, so
you don't get it twice. `/status=@quiet pos system health --send` delivers
one digest via the notify system and nothing else.
- **Daemon lifecycle:** the service is a systemd **user** unit; it stops at
logout unless you enable linger: `sudo loginctl enable-linger $(whoami)`.
`--enable` prints this warning if linger is off.
**Troubleshooting:**
- Bot doesn't answer → send `/help`; if silent, check the service with
`systemctl --user status pos-telegram-listener.service`.
- A webhook on the bot blocks `getUpdates` → remove it with BotFather's
`/deletewebhook`.
- Needs `jq` (in preinstall PACKAGES).
---
## The notify system (`notify_send`)
Every tool that announces something sends through `lib/notify.sh` instead of
hard-coding Telegram:
```bash
# ~/.config/linux_post_install/notify.env
NOTIFY_PLATFORM=telegram # default; comma-separated to fan out to all
```
- `notify_send "msg"` → delivers to every platform in `NOTIFY_PLATFORM`
(current senders: `telegram`).
- **Silent-fail:** no platform configured → one WARN line, exit 0, never
breaks the calling tool.
- New platform (e.g. Matrix): implement `bin/pos-communication-<p> send
<value> [--markdown]`, then list it in `NOTIFY_PLATFORM`. Details:
[DOC/DEV.md → Alerting](../DEV.md).
---
## `pos communication matrix`
Sender contract exists (`send <value> [--markdown]`) and the dispatcher routes
to it, but no implementation ships yet. When present, add `matrix` to
`NOTIFY_PLATFORM` and configure it via `pos communication matrix config set …`.
---
## Related
- Reference + config file details: [DOC/POS.md → communication](../POS.md)
- Alerting contract: [DOC/DEV.md → Alerting](../DEV.md)
- Health digest (uses `--send --markdown`): [system.md](system.md)