Initial commit: page-manager Next.js app with Coolify deploy support
Replace Vercel-specific infra (Cron, Blob) with self-hosted equivalents for deployment to Coolify: in-container cron scheduler (scripts/cron-runner.mjs) and disk-based upload storage served via /api/uploads/[...path]. Add Dockerfile, .dockerignore, and /api/health for container health checks. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import { graphGet, graphPostJson, UserFacingError } from "@/lib/facebook/client";
|
||||
import type { PageCredentials } from "@/lib/facebook/tokens";
|
||||
|
||||
async function waitForContainer(page: PageCredentials, containerId: string): Promise<void> {
|
||||
for (let attempt = 0; attempt < 10; attempt++) {
|
||||
const res = await graphGet(`/${containerId}`, page.token, { fields: "status_code" });
|
||||
if (res.status_code === "FINISHED") return;
|
||||
if (res.status_code === "ERROR") throw new Error("Instagram media container failed to process");
|
||||
await new Promise((r) => setTimeout(r, 1500));
|
||||
}
|
||||
throw new Error("Timed out waiting for Instagram media container");
|
||||
}
|
||||
|
||||
/**
|
||||
* Single-image feed post. `imageUrl` must be publicly fetchable by Meta's
|
||||
* servers (Vercel Blob public URL). Instagram has no native scheduling via the
|
||||
* API — scheduled IG posts are fired by /api/cron/ig-publisher.
|
||||
*/
|
||||
export async function publishToInstagram(
|
||||
page: PageCredentials,
|
||||
params: { imageUrl: string; caption: string }
|
||||
): Promise<{ mediaId: string; permalink: string | null }> {
|
||||
if (!page.igUserId) throw new UserFacingError("เพจนี้ยังไม่ได้ผูกบัญชี Instagram Business");
|
||||
|
||||
const container = await graphPostJson(`/${page.igUserId}/media`, page.token, {
|
||||
image_url: params.imageUrl,
|
||||
caption: params.caption,
|
||||
});
|
||||
await waitForContainer(page, container.id);
|
||||
|
||||
const publish = await graphPostJson(`/${page.igUserId}/media_publish`, page.token, { creation_id: container.id });
|
||||
const media = await graphGet(`/${publish.id}`, page.token, { fields: "permalink" }).catch(() => ({}));
|
||||
return { mediaId: publish.id, permalink: media.permalink ?? null };
|
||||
}
|
||||
Reference in New Issue
Block a user