Add news research assistant to post editor

This commit is contained in:
Loma Bot Ops
2026-11-23 21:59:02 +07:00
parent 5f8f0c9159
commit 6ef01cb4b6
9 changed files with 491 additions and 22 deletions
+10
View File
@@ -63,6 +63,15 @@ export async function graphPostJson(path: string, accessToken: string, body: Rec
return parse(res);
}
/** Uploads binary media to Graph without requiring Meta to fetch a public URL. */
export async function graphPostForm(path: string, accessToken: string, body: FormData): Promise<GraphBody> {
const res = await fetch(buildUrl(path, accessToken), {
method: "POST",
body,
});
return parse(res);
}
export async function graphDelete(path: string, accessToken: string): Promise<GraphBody> {
const res = await fetch(buildUrl(path, accessToken), { method: "DELETE" });
return parse(res);
@@ -91,6 +100,7 @@ const GRAPH_ERROR_HINT: Record<number, string> = {
32: "เพจนี้เรียก Facebook ถี่เกินไป รอสักครู่แล้วลองใหม่",
190: "สิทธิ์เข้าถึงเพจหมดอายุหรือถูกยกเลิก — ไปที่เพจที่เชื่อม แล้วเชื่อมบัญชี Facebook ใหม่",
200: "บัญชีที่เชื่อมไม่มีสิทธิ์ทำสิ่งนี้ — เชื่อมบัญชี Facebook ใหม่และอนุญาตสิทธิ์ให้ครบ",
324: "Facebook ไม่พบหรืออ่านไฟล์รูปไม่ได้ — ลองอัปโหลดรูป JPG/PNG ใหม่อีกครั้ง",
10: "บัญชีที่เชื่อมไม่มีสิทธิ์ทำสิ่งนี้ — เชื่อมบัญชี Facebook ใหม่และอนุญาตสิทธิ์ให้ครบ",
368: "Facebook บล็อกการกระทำนี้ชั่วคราว (อาจถูกมองว่าเป็นสแปม) ลองใหม่ภายหลัง",
506: "โพสต์ซ้ำกับโพสต์ล่าสุด — แก้ข้อความก่อนเผยแพร่",
+61 -6
View File
@@ -1,4 +1,4 @@
import { bareId, graphDelete, graphGet, graphGetAll, graphPostJson } from "./client";
import { bareId, graphDelete, graphGet, graphGetAll, graphPostForm, graphPostJson, UserFacingError } from "./client";
import type { PageCredentials } from "./tokens";
/**
@@ -22,6 +22,60 @@ export interface FbPublishResult {
permalink: string | null;
}
const MAX_FACEBOOK_IMAGE_BYTES = 8 * 1024 * 1024;
const FACEBOOK_IMAGE_TYPES = new Map([
["image/jpeg", "image.jpg"],
["image/png", "image.png"],
]);
function isAllowedImageUrl(imageUrl: URL): boolean {
if (imageUrl.protocol !== "https:" && imageUrl.protocol !== "http:") return false;
const appUrl = process.env.NEXT_PUBLIC_APP_URL;
if (appUrl) {
const app = new URL(appUrl);
const uploadPrefix = `${app.pathname.replace(/\/$/, "")}/api/uploads/`;
if (imageUrl.origin === app.origin && imageUrl.pathname.startsWith(uploadPrefix)) return true;
}
return imageUrl.protocol === "https:" && imageUrl.hostname.endsWith(".public.blob.vercel-storage.com");
}
async function facebookImageSource(imageUrl: string): Promise<{ blob: Blob; filename: string }> {
let parsed: URL;
try {
parsed = new URL(imageUrl);
} catch {
throw new UserFacingError("URL รูปไม่ถูกต้อง กรุณาอัปโหลดรูปใหม่");
}
if (!isAllowedImageUrl(parsed)) {
throw new UserFacingError("URL รูปไม่ได้มาจากพื้นที่จัดเก็บของระบบ กรุณาอัปโหลดรูปใหม่");
}
const response = await fetch(parsed, { cache: "no-store", redirect: "follow" });
if (!response.ok) {
throw new UserFacingError("ระบบอ่านไฟล์รูปไม่ได้ กรุณาอัปโหลดรูปใหม่");
}
const contentType = response.headers.get("content-type")?.split(";", 1)[0].trim().toLowerCase() ?? "";
const filename = FACEBOOK_IMAGE_TYPES.get(contentType);
if (!filename) {
throw new UserFacingError("ไฟล์รูปต้องเป็น JPG หรือ PNG กรุณาอัปโหลดรูปใหม่");
}
const contentLength = Number(response.headers.get("content-length"));
if (Number.isFinite(contentLength) && contentLength > MAX_FACEBOOK_IMAGE_BYTES) {
throw new UserFacingError("ไฟล์รูปใหญ่เกิน 8MB กรุณาอัปโหลดรูปที่เล็กลง");
}
const bytes = await response.arrayBuffer();
if (bytes.byteLength === 0) throw new UserFacingError("ไฟล์รูปว่างเปล่า กรุณาอัปโหลดรูปใหม่");
if (bytes.byteLength > MAX_FACEBOOK_IMAGE_BYTES) {
throw new UserFacingError("ไฟล์รูปใหญ่เกิน 8MB กรุณาอัปโหลดรูปที่เล็กลง");
}
return { blob: new Blob([bytes], { type: contentType }), filename };
}
/**
* Publishes now or hands Facebook a scheduled_publish_time — Facebook holds
* and fires scheduled posts itself, no cron needed.
@@ -36,11 +90,12 @@ export async function publishToFacebook(
let fbPostId: string;
if (params.imageUrl) {
const res = await graphPostJson(`/${page.fbPageId}/photos`, page.token, {
url: params.imageUrl,
caption: params.caption,
...scheduling,
});
const image = await facebookImageSource(params.imageUrl);
const body = new FormData();
body.set("source", image.blob, image.filename);
body.set("caption", params.caption);
for (const [key, value] of Object.entries(scheduling)) body.set(key, String(value));
const res = await graphPostForm(`/${page.fbPageId}/photos`, page.token, body);
// Photos return `id` (photo id) and `post_id` (feed post id) — the post id is what the feed edges use.
fbPostId = res.post_id ?? res.id;
} else {