# Morning Briefing Telegram Delivery

Pattern for delivering cron-generated morning briefings to Telegram.

## Telegram Token Discovery

The Telegram bot token is NOT in `~/.openclaw/gateway_token.txt` — that file contains the OpenClaw gateway auth token. The actual Telegram bot token is in `~/.hermes/.env` under `TELEGRAM_BOT_TOKEN`.

```bash
# Find the token locally; never paste it into skills, git, or chat.
grep '^TELEGRAM_BOT_TOKEN=' ~/.hermes/.env
```

## Delivery Pattern

Use Python `urllib.request` for Telegram API delivery (no external deps needed):

```python
import json, os, urllib.request

bot_token = os.environ["TELEGRAM_BOT_TOKEN"]
chat_id = os.environ.get("TELEGRAM_ALLOWED_USERS", "").split(",")[0]
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"

# MarkdownV2 requires escaping special chars
def escape_md(text):
    for c in ['_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!']:
        text = text.replace(c, '\\' + c)
    return text

payload = {
    "chat_id": chat_id,
    "text": escape_md(formatted_briefing),
    "parse_mode": "MarkdownV2",
    "disable_web_page_preview": True,
}

body = json.dumps(payload).encode("utf-8")
req = urllib.request.Request(url, data=body, headers={"Content-Type": "application/json"})
with urllib.request.urlopen(req) as response:
    result = json.loads(response.read().decode())
    assert result.get("ok") is True, result
```

## Pitfalls

- **Secrets in skills are forbidden:** use placeholders/env vars only. If a real token appears in a skill reference, redact it immediately.
- **Wrong token:** `gateway_token.txt` is NOT the Telegram bot token — it's the OpenClaw gateway auth token. Use `.env` instead.
- **MarkdownV2 escaping:** All special MarkdownV2 characters must be escaped with backslash. Use the `escape_md()` function above.
- **Chat ID:** Use the Telegram user ID from `.env` (`TELEGRAM_ALLOWED_USERS`), NOT `home_channel` if they differ.
- **Script success ≠ delivery success:** Always verify the Telegram API response `ok` field.

## Related

- `cronjob/SKILL.md` — General cron job management
- `cronjob/references/morning-briefing-context.md` — JSON data-collector pattern for richer morning briefings
- `hermes-agent/references/telegram-gateway-setup.md` — Telegram gateway configuration
