Add AI auto sales replies for inbox

This commit is contained in:
Loma Bot Ops
2026-11-25 21:31:32 +07:00
parent dd42b81639
commit 89d46ab141
10 changed files with 272 additions and 5 deletions
+13 -2
View File
@@ -1,16 +1,18 @@
import { NextResponse } from "next/server";
import { createHmac, timingSafeEqual } from "node:crypto";
import { createHash, createHmac, timingSafeEqual } from "node:crypto";
import { prisma } from "@/lib/db";
import { FacebookApiError } from "@/lib/facebook/client";
import { getPageCredentialsByFbId, type PageCredentials } from "@/lib/facebook/tokens";
import { replyToComment, sendImageMessage, sendPrivateReply, sendSenderAction, sendTextMessage } from "@/lib/facebook/inbox";
import { claimReplySlot, matchRule, releaseReplySlot, typingDelayMs } from "@/lib/inbox/autoReply";
import { runInboxAiAutoReply } from "@/lib/inbox/aiAutoReply";
// Meta calls this with no session: GET proves ownership via the verify token,
// POST proves authenticity via the X-Hub-Signature-256 HMAC. Listed in
// SELF_AUTH_PREFIXES in src/proxy.ts. node:crypto needs the Node runtime.
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
export const maxDuration = 300;
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
@@ -118,7 +120,16 @@ async function handleMessage(page: PageCredentials, platform: "FACEBOOK" | "INST
if (!text || !convo.botEnabled) return;
const rule = await matchRule(page.id, "DM", text);
if (!rule) return;
if (!rule) {
await runInboxAiAutoReply({
page,
platform,
recipientId: psid,
sourceId: event.message?.mid ?? createHash("sha256").update(`${platform}:${page.id}:${psid}:${text}`).digest("hex"),
text,
});
return;
}
if (!(await claimReplySlot(rule.id, psid, event.message?.mid ?? `dm-${Date.now()}`))) return;
try {