How diwenne/openreply works
Purpose and Runtime Shape
OpenReply is a self-hosted Instagram comment-to-DM automation SaaS. It receives official Meta webhooks, matches comments against configured campaigns, queues delivery, and sends private/public replies through Meta APIs. The Next.js application serves dashboard, public pages, OAuth, and HTTP endpoints; a separate BullMQ worker performs durable sending. Entry points: app/api/webhook/route.ts, worker/dm-worker.ts, and app/(dashboard)/layout.tsx.
Web Application and API Boundary
Next.js 16 App Router provides authenticated operational UI plus route handlers. Dashboard pages manage campaigns, automations, inbox, logs, settings, diagnostics, and overview; public pages include shared reports, templates, tracking redirects, and legal/SEO content. Key paths: app/(dashboard)/campaigns/[id]/page.tsx, app/api/dashboard/stats/route.ts, app/r/[slug]/route.ts.
Identity, Workspaces, and Account Connection
Auth.js uses email magic links via Resend. Users operate within role-based workspaces (owner/admin/member), invitations, and one or more connected professional Instagram accounts. Meta OAuth/account lifecycle is isolated behind Instagram API routes and Meta modules. Key paths: app/api/auth/[...nextauth]/route.ts, app/api/instagram/connect/route.ts, lib/auth.ts, lib/workspace-access.ts, lib/meta/oauth.ts.
Campaign Definition and Matching
Campaigns bind Instagram posts/reels to keyword triggers and message behavior: personalized DM text, one or two tracked-link buttons, optional public reply, follow gate, follow-up, and targeting/builder fields. The dashboard builder is the primary authoring interface; shared matching logic determines whole-word or partial keyword hits. Key paths: components/campaign-builder.tsx, app/api/automations/route.ts, lib/utils/keyword-matcher.ts, prisma/schema.prisma.
Event-to-Delivery Flow
Meta calls the webhook endpoint for comment and messaging events. The handler validates/parses events, identifies the Instagram account/workspace and active campaign, filters invalid/self comments, and enqueues matching DM work in Redis. The worker consumes jobs, applies per-account Meta reply limits, performs private reply/public reply/follow-gate interactions, records outcomes, and retries/defers overflow rather than dropping it. Key paths: app/api/webhook/route.ts, lib/meta/webhook.ts, lib/queue/dm-worker.ts, worker/dm-worker.ts.
Persistence, Queueing, and External Services
PostgreSQL is the system of record through Prisma for users, workspaces, accounts, campaigns, event/send logs, tracked links, reports, and follower history. Redis supports BullMQ delivery and likely cache/import coordination. External boundaries are Meta’s Instagram Login/Graph APIs and webhooks, plus Resend authentication email. Deployments separate web hosting from long-running worker hosting; local infrastructure is defined in docker-compose.yml. Key paths: lib/db/client.ts, lib/queue/client.ts, lib/meta/client.ts, docker-compose.yml.
Reporting, Tracking, and Operations
Tracked links route through a server redirect that records clicks; reporting aggregates campaign/link performance and can expose shareable reports. Scheduled endpoints refresh tokens, attach next-reel targets, and snapshot followers; reconciliation recovers comment-processing gaps. Diagnostics and health endpoints expose operational status, including worker health. Key paths: lib/tracking/server.ts, lib/reports/data.ts, app/api/cron/refresh-tokens/route.ts, lib/polling/comment-reconciler.ts, app/api/admin/diagnostics/route.ts.
Main entry points: db/client.ts PostgreSQL system of record · meta/client.ts Meta Instagram APIs · webhook.ts Webhook event processing · dm-worker.ts DM delivery queue
Components 19
Next.js web app
Dashboard, public routes, and HTTP handlers.
- Dashboard & campaign builder · Authenticated workspace operations and campaign authoring.
components/campaign-builder.tsx - Auth and workspace access · Magic-link identity and role-based workspace authorization.
lib/auth.ts - Auth endpoint
app/api/auth/[...nextauth]/route.ts - Instagram connection · Starts and manages connected Instagram accounts.
app/api/instagram/connect/route.ts - Dashboard stats API
app/api/dashboard/stats/route.ts - Public tracking route
app/r/[slug]/route.ts
Automation pipeline
Webhook matching and durable delivery.
- Meta webhook endpoint · Validates events and creates matching delivery work.
app/api/webhook/route.ts - Webhook event processing · Resolves account, campaign, and valid comment events.
lib/meta/webhook.ts - Campaign matching · Matches active post/reel keyword triggers.
lib/utils/keyword-matcher.ts - DM delivery queue · Durable Redis-backed jobs for sends and retries.
lib/queue/dm-worker.ts - DM worker · Applies limits and sends private/public replies and follow gates.
worker/dm-worker.ts
Data and operations
Persistence, tracking, reporting, and maintenance.
- PostgreSQL system of record · Stores workspaces, accounts, campaigns, logs, links, reports, and follower history.
lib/db/client.ts - Redis · Supports BullMQ delivery and coordination.
lib/queue/client.ts - Tracked-link redirect · Records clicks before redirecting visitors.
lib/tracking/server.ts - Reporting data · Aggregates campaign and link performance.
lib/reports/data.ts - Reconciliation and scheduled ops · Refreshes tokens and recovers comment-processing gaps.
lib/polling/comment-reconciler.ts - Local service infrastructure · Defines local PostgreSQL and Redis services.
docker-compose.yml
Other components
- Meta Instagram APIs · Instagram Login, Graph APIs, and webhook source.
lib/meta/client.ts - Resend · Delivers Auth.js magic-link email.
Connections 23
- Dashboard & campaign builder → Auth and workspace access: authorizes workspace access
- Auth endpoint → Auth and workspace access: serves authentication
- Auth and workspace access → Resend: sends magic links
- Dashboard & campaign builder → PostgreSQL system of record: manages campaigns and accounts
- Instagram connection → Meta Instagram APIs: OAuth/account lifecycle
- Instagram connection → PostgreSQL system of record: stores connections
- Dashboard stats API → Reporting data: reads performance
- Meta webhook endpoint → Webhook event processing: parses validated events
- Meta Instagram APIs → Meta webhook endpoint: comment and messaging events
- Webhook event processing → Campaign matching: checks active triggers
- Campaign matching → PostgreSQL system of record: reads campaigns and accounts
- Webhook event processing → DM delivery queue: enqueues matching work
- DM delivery queue → Redis: persists jobs
- DM delivery queue → DM worker: delivers jobs
- DM worker → Meta Instagram APIs: sends replies and follows
- DM worker → PostgreSQL system of record: records outcomes and limits
- Public tracking route → Tracked-link redirect: handles tracked links
- Tracked-link redirect → PostgreSQL system of record: records clicks
- Reporting data → PostgreSQL system of record: aggregates logs and clicks
- Reconciliation and scheduled ops → PostgreSQL system of record: reconciles and snapshots
- Reconciliation and scheduled ops → Meta Instagram APIs: refreshes and recovers
- Local service infrastructure → PostgreSQL system of record: runs locally
- Local service infrastructure → Redis: runs locally