Add AI auto sales replies for inbox
This commit is contained in:
@@ -104,6 +104,8 @@ model PostApproval { postId, approverId, approverLabel, note? @@unique([postId,
|
||||
model AuditLog { id, pageId?, postId?, actorId?, actorLabel, action, detail Json?, createdAt @@index([pageId, createdAt]) } // append-only
|
||||
|
||||
model Conversation { id, pageId, platform, psid, botEnabled @default(true), lastInboundAt, stage? @@unique([pageId, platform, psid]) }
|
||||
model PageInboxAiAutomation { id, pageId @unique, enabled, personaName, salesGoal, instructions, updatedById? }
|
||||
model InboxAiReply { id, automationId, sourceId @unique, recipientId, outcome, createdAt } // dedupe only; no message body
|
||||
model ReplyRule { id, pageId, kind (DM | COMMENT), keywords String[], reply, imageUrl?, publicAck?, isActive, priority }
|
||||
model ReplyLog { id, ruleId, recipientId, sourceId, createdAt @@unique([ruleId, recipientId]) } // dedupe: คนเดิมได้กฎเดิมครั้งเดียว
|
||||
|
||||
@@ -165,7 +167,7 @@ DRAFT ──ส่ง──▶ PENDING_APPROVAL ──อนุมัติค
|
||||
`/api/webhooks/meta` endpoint เดียวรับทุกเพจ. `entry[].id` = fbPageId → หา `Page` ใน DB (ไม่เจอ = ข้าม)
|
||||
|
||||
1. อ่าน raw body (`await req.text()`), verify `X-Hub-Signature-256` ด้วย `META_APP_SECRET` + `timingSafeEqual` → ไม่ผ่านคืน 403
|
||||
2. `entry[].messaging[]` = DM (ข้าม `is_echo`) → อัปเดต `Conversation.lastInboundAt` → ถ้า `botEnabled` ลอง match `ReplyRule(kind=DM)`
|
||||
2. `entry[].messaging[]` = DM (ข้าม `is_echo`) → อัปเดต `Conversation.lastInboundAt` → ถ้า `botEnabled` ลอง match `ReplyRule(kind=DM)` ก่อน; เมื่อไม่ match และเปิด `PageInboxAiAutomation` ให้ AI ตอบจาก `PageProfile` หรือสลับแชทเป็นคนตอบเมื่อข้อมูลไม่พอ/ต้อง handoff. `InboxAiReply.sourceId` กัน webhook ซ้ำโดยไม่เก็บข้อความ
|
||||
3. `entry[].changes[]` field `feed` (FB, `item=comment, verb=add`) หรือ `comments` (IG, shape ต่างกัน) → ข้ามคอมเมนต์ของเพจเอง → match `ReplyRule(kind=COMMENT)` → จอง `ReplyLog` ก่อนส่ง (unique กันซ้ำ) → `POST /{comment-id}/private_replies` → ส่งไม่ผ่านให้ลบ `ReplyLog` คืน
|
||||
4. คืน 200 เสมอ (ไม่งั้น Meta ยิงซ้ำรัว)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user