Ad Engine — Facebook Ads Deployment
Assemble ads from database components and deploy to Facebook. Reads from Supabase ad_components + messages tables, assembles full ad copy, uploads images, and creates Campaign → Ad Set → Ad in Facebook Ads Manager.
First-Time Setup
1. Facebook prerequisites (one-time, manual):
- - Facebook Business Manager account
- Ad Account created
- Facebook Page connected
- Facebook Developer App with
ads_management permission - Long-lived access token (generate at developers.facebook.com)
2. Store credentials:
python3 scripts/fb_deploy.py --setup
Prompts for access token, ad account ID, and page ID. Validates against FB API and saves to
~/.config/ad-engine/fb_config.json.
Usage
Preview assembled ads (no deployment):
CODEBLOCK1
Deploy a single ad:
CODEBLOCK2
Deploy all draft ads for a campaign:
CODEBLOCK3
Dry run (preview what would be created):
CODEBLOCK4
Check deployed ad status + live metrics:
CODEBLOCK5
Parameters
| Parameter | Required | Description |
|---|
| INLINECODE4 | One of | Preview assembled ads without deploying |
| INLINECODE5 |
these | Deploy ads to Facebook |
|
--status | three | Check status of deployed ads |
|
--setup | | Configure Facebook credentials |
|
--message-id | For single | Specific message ID to preview/deploy |
|
--campaign-id | For batch | All draft ads in a campaign |
|
--image | Deploy single | Image file path |
|
--image-dir | Deploy batch | Directory of images (matched by angle name in filename) |
|
--landing-url | Deploy | Booking/landing page URL |
|
--objective | No |
leads (default),
messages, or
link_clicks |
|
--dry-run | No | Preview deployment without creating anything |
|
--json | No | Output as JSON |
Image Naming Convention
When using --image-dir for batch deploy, name images by angle:
CODEBLOCK6
How It Works
- 1. Reads message from Supabase
messages table (contenttype = 'fbad') - Reads component references from message's INLINECODE21
- Resolves component keys → actual text from
ad_components table - Slots components into framework template using INLINECODE23
- Uploads image to Facebook → gets imagehash
- Creates Campaign (if new) → Ad Set (if new) → Ad Creative → Ad
- Updates message status to 'deployed' with FB IDs stored in extradata
Database Schema
See AD_ENGINE_SPEC.md for full schema documentation.
Dependencies
- -
psycopg2-binary — Supabase Postgres connection - INLINECODE26 — Facebook Marketing API SDK
- INLINECODE27 — HTTP client
- All auto-installed on first run.
Ad Engine — Facebook 广告部署
从数据库组件组装广告并部署到 Facebook。读取 Supabase 的 ad_components 和 messages 表,组装完整的广告文案,上传图片,并在 Facebook 广告管理器中创建广告系列 → 广告组 → 广告。
首次设置
1. Facebook 前置条件(一次性手动操作):
- - Facebook 商务管理平台账户
- 已创建的广告账户
- 已关联的 Facebook 主页
- 拥有 ads_management 权限的 Facebook 开发者应用
- 长期有效的访问令牌(在 developers.facebook.com 生成)
2. 存储凭证:
bash
python3 scripts/fb_deploy.py --setup
提示输入访问令牌、广告账户 ID 和主页 ID。通过 Facebook API 验证后保存至 ~/.config/ad-engine/fb_config.json。
使用方法
预览已组装的广告(不部署):
bash
预览单个广告
python3 scripts/fb_deploy.py --preview --message-id 8555
预览某个广告系列的所有草稿广告
python3 scripts/fb_deploy.py --preview --campaign-id 43
以 JSON 格式预览
python3 scripts/fb_deploy.py --preview --campaign-id 43 --json
部署单个广告:
bash
python3 scripts/fb_deploy.py --deploy \
--message-id 8556 \
--image /path/to/security-audit-ad.png \
--landing-url https://calendly.com/your-link \
--objective messages
部署某个广告系列的所有草稿广告:
bash
python3 scripts/fb_deploy.py --deploy \
--campaign-id 43 \
--image-dir /path/to/ad-images/ \
--landing-url https://calendly.com/your-link \
--objective messages
试运行(预览将要创建的内容):
bash
python3 scripts/fb_deploy.py --deploy \
--campaign-id 43 \
--image-dir /path/to/images/ \
--landing-url https://calendly.com/link \
--dry-run
检查已部署广告的状态和实时指标:
bash
python3 scripts/fb_deploy.py --status --campaign-id 43
参数
| 参数 | 必填 | 描述 |
|---|
| --preview | 三选一 | 预览已组装的广告但不部署 |
| --deploy |
这些 | 将广告部署到 Facebook |
| --status | 参数 | 检查已部署广告的状态 |
| --setup | | 配置 Facebook 凭证 |
| --message-id | 单个操作 | 要预览/部署的特定消息 ID |
| --campaign-id | 批量操作 | 广告系列中的所有草稿广告 |
| --image | 部署单个 | 图片文件路径 |
| --image-dir | 部署批量 | 图片目录(通过文件名中的角度名称匹配) |
| --landing-url | 部署 | 预约/落地页 URL |
| --objective | 否 | leads(默认)、messages 或 link_clicks |
| --dry-run | 否 | 预览部署但不创建任何内容 |
| --json | 否 | 以 JSON 格式输出 |
图片命名规范
使用 --image-dir 进行批量部署时,按角度命名图片:
security-audit-ad.png → 匹配角度 security_audit
setup-is-hell-ad.png → 匹配角度 setupishell
dm-trigger-checklist.png → 匹配角度 dm_trigger
anti-wrapper-graveyard.png → 匹配角度 anti_wrapper
工作原理
- 1. 从 Supabase 的 messages 表读取消息(contenttype = fbad)
- 从消息的 extradata.components 读取组件引用
- 解析组件键 → 从 adcomponents 表获取实际文本
- 使用 {{mergetags}} 将组件填入框架模板
- 上传图片到 Facebook → 获取 imagehash
- 创建广告系列(如为新)→ 广告组(如为新)→ 广告创意 → 广告
- 将消息状态更新为 deployed,并将 Facebook ID 存储在 extra_data 中
数据库架构
完整架构文档请参见 ADENGINESPEC.md。
依赖项
- - psycopg2-binary — Supabase Postgres 连接
- facebook-business — Facebook 营销 API SDK
- requests — HTTP 客户端
- 首次运行时自动安装所有依赖。