clipwright/examples/n8n/README.md
pedro-bento 4e82d60cbc fix: harden Postiz integration and align docs/CLI/n8n after review
- Postiz URL is now required (no built-in host); a key can never be sent to a default instance
- /api/postiz/scheduled normalizes scheduled_date and widens the default window
- Disabled channels are refused for posting; frontend excludes them from connected platforms
- 4xx instead of 500 on missing clip URL / bad schedule date; Postiz error bodies truncated
- Add tests/test_postiz.py (24 tests); fix remaining cloud-removal test drift
- CLI defaults to localhost:8000; n8n workflows fixed against the real routes
- Remove dead media-token auth code, stale copy (download_url/402) and doc drift
2026-10-04 17:25:17 +01:00

102 lines
4.7 KiB
Markdown

# OpenShorts + n8n
Two importable workflows, from a one-shot clipper to a channel watcher.
Both run against a self-hosted OpenShorts instance (point them at your base URL,
default `http://localhost:8000`) and publish through a self-hosted
[Postiz](https://postiz.com/) instance. No OpenShorts API key is required, but
the server needs a Google Gemini key for the AI stages, and Postiz needs the
target channels connected. The publishing, queue and analytics calls need the
instance configured with `POSTIZ_URL` / `POSTIZ_API_KEY` (or an `X-Postiz-Key`
header sent per request); the workflow nodes hold no credential of their own.
| Workflow | What it does | Credentials |
|---|---|---|
| `openshorts-clip-and-notify.json` | A video URL goes in through a form, clips come back on a signed webhook. No polling. | None (self-hosted) |
| `openshorts-content-machine.json` | Watches your YouTube channel: daily clipping, approval buttons in Telegram, drip-scheduled posting through Postiz, weekly analytics digest. | Telegram bot |
## Shared setup
1. In n8n: **Workflows → Import from file** → pick the workflow.
2. Set `OPENSHORTS_URL` / the request base URL to your instance
(`http://localhost:8000`).
3. Open the **Clips ready (webhook)** node, copy its production URL, and paste
it as `webhook_url` inside the **Start OpenShorts job** node body.
4. Optional: change `webhook_secret`. OpenShorts signs the webhook body with
HMAC-SHA256 and sends it as `X-OpenShorts-Signature: sha256=<hex>`; verify
it in a Code node with:
```javascript
const crypto = require('crypto');
const expected = 'sha256=' + crypto
.createHmac('sha256', 'change-me')
.update(JSON.stringify($json.body))
.digest('hex');
```
## The Content Machine
`openshorts-content-machine.json` runs your channel in four acts:
1. **Watch** — once a day it reads your channel's RSS feed
(`https://www.youtube.com/feeds/videos.xml?channel_id=UC...`) and clips
**one** video: the newest upload if there is one, otherwise the next
unprocessed video from your back catalog. One video a day keeps processing
predictable.
2. **Approve from your phone** — every finished clip lands in Telegram with
✅ Publish / ❌ Skip buttons. Clips over Telegram's ~20 MB URL limit fall
back to a link message with the same buttons.
3. **Drip-publish** — each approved clip takes the next free daily slot and is
scheduled through `POST /api/postiz/post` to every channel connected in
Postiz. Approve five clips today, fill five days of content. The slot comes
from the queue the server actually holds (`GET /api/postiz/scheduled`), so
two clips approved seconds apart cannot book the same one.
4. **Sunday digest** — once a week it lists the channels connected in Postiz
(`GET /api/postiz/integrations`) and reports each channel's impressions over
the last 7 days (`GET /api/postiz/analytics?integration=<id>&days=7`).
Machine-specific setup, all inside sticky notes on the canvas:
- Put your channel id in the **Channel RSS feed** node.
- Create a Telegram bot with [@BotFather](https://t.me/BotFather), add the
`Telegram bot` credential, and replace `YOUR_TELEGRAM_CHAT_ID` in the
notification nodes (message your bot, then check
`api.telegram.org/bot<token>/getUpdates` for your chat id).
- Connect your social accounts in your self-hosted Postiz instance — the
posting step uses them directly; no extra social credentials in n8n.
Note: the machine remembers which videos it already processed in n8n workflow
static data, which only persists for **production** executions — test runs in
the editor won't advance it. Scheduling deliberately does *not* use that
mechanism (see act 3).
## Webhook payload
```json
{
"event": "job.completed",
"job_id": "…",
"status": "completed",
"clips": [
{ "index": 0, "title": "…", "video_url": "…" }
]
}
```
Failed jobs fire the same webhook with `"event": "job.failed"` and an `error`
field, so the flow never hangs waiting.
Each `video_url` is relative to your instance unless the server sets
`PUBLIC_API_URL`; point it at a host Telegram can reach (a `localhost` instance
is not reachable from Telegram's servers).
## Publishing and analytics API
Direct posting: `POST /api/postiz/post` (accepts `scheduled_date`, ISO-8601).
Connected channels: `GET /api/postiz/integrations`.
Publishing queue: `GET /api/postiz/scheduled`, and
`DELETE /api/postiz/scheduled/{id}` to cancel one before it goes out.
Analytics: `GET /api/postiz/analytics?integration=<id>&days=7` (one channel) and
`GET /api/postiz/analytics/posts?post_id=<id>&days=7` (one post) — both the
channel and the post id are required.
Full API reference: `/docs` (OpenAPI) on your instance.