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,92 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { requirePageAction } from "@/lib/auth/user";
|
||||
import { getPageCredentials } from "@/lib/facebook/tokens";
|
||||
import { lastInboundAt, listMessages, resolveRecipient, sendTextMessage } from "@/lib/facebook/inbox";
|
||||
import { isWithinMessagingWindow, MAX_MESSAGE_LENGTH } from "@/lib/facebook/messagingWindow";
|
||||
import { errorResponse, readJson, str } from "@/lib/http";
|
||||
import type { Platform } from "@prisma/client";
|
||||
|
||||
type Ctx = RouteContext<"/api/inbox/[pageId]/conversations/[conversationId]">;
|
||||
|
||||
function platformOf(req: Request): Platform {
|
||||
return new URL(req.url).searchParams.get("platform") === "instagram" ? "INSTAGRAM" : "FACEBOOK";
|
||||
}
|
||||
|
||||
/** Messages + bot state for one thread. */
|
||||
export async function GET(req: Request, ctx: Ctx) {
|
||||
const { pageId, conversationId } = await ctx.params;
|
||||
const auth = await requirePageAction(pageId, "view");
|
||||
if (!auth.ok) return auth.response;
|
||||
|
||||
try {
|
||||
const page = await getPageCredentials(pageId);
|
||||
const [messages, recipient] = await Promise.all([
|
||||
listMessages(page, conversationId),
|
||||
resolveRecipient(page, conversationId),
|
||||
]);
|
||||
const convo = recipient
|
||||
? await prisma.conversation.findUnique({
|
||||
where: { pageId_platform_psid: { pageId, platform: platformOf(req), psid: recipient.id } },
|
||||
select: { botEnabled: true },
|
||||
})
|
||||
: null;
|
||||
return NextResponse.json({
|
||||
messages,
|
||||
recipient,
|
||||
lastInboundAt: lastInboundAt(messages),
|
||||
botEnabled: convo?.botEnabled ?? true,
|
||||
});
|
||||
} catch (e) {
|
||||
return errorResponse(e, "inbox:messages");
|
||||
}
|
||||
}
|
||||
|
||||
/** Send a reply `{ text }`. Recipient is resolved server-side; the 24h window is enforced here too. */
|
||||
export async function POST(req: Request, ctx: Ctx) {
|
||||
const { pageId, conversationId } = await ctx.params;
|
||||
const auth = await requirePageAction(pageId, "replyInbox");
|
||||
if (!auth.ok) return auth.response;
|
||||
|
||||
const text = str((await readJson(req)).text).trim();
|
||||
if (!text) return NextResponse.json({ error: "พิมพ์ข้อความก่อนส่ง" }, { status: 400 });
|
||||
if (text.length > MAX_MESSAGE_LENGTH) return NextResponse.json({ error: `ข้อความยาวเกิน ${MAX_MESSAGE_LENGTH} ตัวอักษร` }, { status: 400 });
|
||||
|
||||
try {
|
||||
const page = await getPageCredentials(pageId);
|
||||
const messages = await listMessages(page, conversationId, 25);
|
||||
if (!isWithinMessagingWindow(lastInboundAt(messages))) {
|
||||
return NextResponse.json({ error: "เลย 24 ชั่วโมงหลังข้อความล่าสุดของลูกค้าแล้ว ตอบผ่านระบบไม่ได้" }, { status: 400 });
|
||||
}
|
||||
const recipient = await resolveRecipient(page, conversationId);
|
||||
if (!recipient) return NextResponse.json({ error: "ไม่พบผู้รับในแชทนี้" }, { status: 400 });
|
||||
const messageId = await sendTextMessage(page, recipient.id, text);
|
||||
console.info(`[inbox:send] page=${page.fbPageId} by=${auth.user.id} conversation=${conversationId}`);
|
||||
return NextResponse.json({ ok: true, messageId });
|
||||
} catch (e) {
|
||||
return errorResponse(e, "inbox:send");
|
||||
}
|
||||
}
|
||||
|
||||
/** Toggle bot vs human `{ botEnabled }`. */
|
||||
export async function PATCH(req: Request, ctx: Ctx) {
|
||||
const { pageId, conversationId } = await ctx.params;
|
||||
const auth = await requirePageAction(pageId, "replyInbox");
|
||||
if (!auth.ok) return auth.response;
|
||||
|
||||
const botEnabled = (await readJson(req)).botEnabled === true;
|
||||
try {
|
||||
const page = await getPageCredentials(pageId);
|
||||
const recipient = await resolveRecipient(page, conversationId);
|
||||
if (!recipient) return NextResponse.json({ error: "ไม่พบผู้รับในแชทนี้" }, { status: 400 });
|
||||
const platform = platformOf(req);
|
||||
await prisma.conversation.upsert({
|
||||
where: { pageId_platform_psid: { pageId, platform, psid: recipient.id } },
|
||||
create: { pageId, platform, psid: recipient.id, botEnabled, lastInboundAt: new Date(0) },
|
||||
update: { botEnabled },
|
||||
});
|
||||
return NextResponse.json({ ok: true, botEnabled });
|
||||
} catch (e) {
|
||||
return errorResponse(e, "inbox:bot");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { requirePageAction } from "@/lib/auth/user";
|
||||
import { getPageCredentials } from "@/lib/facebook/tokens";
|
||||
import { listConversations, type InboxPlatform } from "@/lib/facebook/inbox";
|
||||
import { errorResponse } from "@/lib/http";
|
||||
|
||||
/** `?platform=messenger|instagram&after=` — read live from Graph. */
|
||||
export async function GET(req: Request, ctx: RouteContext<"/api/inbox/[pageId]/conversations">) {
|
||||
const { pageId } = await ctx.params;
|
||||
const auth = await requirePageAction(pageId, "view");
|
||||
if (!auth.ok) return auth.response;
|
||||
|
||||
const q = new URL(req.url).searchParams;
|
||||
const platform: InboxPlatform = q.get("platform") === "instagram" ? "instagram" : "messenger";
|
||||
try {
|
||||
const page = await getPageCredentials(pageId);
|
||||
if (platform === "instagram" && !page.igUserId) return NextResponse.json({ conversations: [], nextCursor: null });
|
||||
const result = await listConversations(page, platform, { after: q.get("after") ?? undefined });
|
||||
return NextResponse.json(result);
|
||||
} catch (e) {
|
||||
return errorResponse(e, "inbox:list");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user