mirror of
https://github.com/complexcaresolutions/whatsapp-bot.git
synced 2026-03-17 18:34:07 +00:00
fix: use nested objects for Payload group fields in InteractionWriter
Payload REST API expects group fields as nested objects (e.g.
`author: { name: "...", handle: "..." }`), not dot-notation or flat
field names. Also adds platform/socialAccount relationship IDs and
proper flags/analysis/response group fields.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
f098cca1bd
commit
c471e42592
1 changed files with 39 additions and 28 deletions
|
|
@ -5,11 +5,10 @@ import type { PayloadClient } from './PayloadClient.js'
|
||||||
|
|
||||||
const log = getLogger('interaction-writer')
|
const log = getLogger('interaction-writer')
|
||||||
|
|
||||||
/**
|
// WhatsApp platform + account IDs in CMS
|
||||||
* Writes messages to community_interactions via Payload REST API.
|
const WHATSAPP_PLATFORM_ID = 4
|
||||||
* When direct DB access becomes available, this can be swapped to a
|
const WHATSAPP_ACCOUNT_ID = 1
|
||||||
* pg Pool-based implementation for higher throughput.
|
|
||||||
*/
|
|
||||||
export class InteractionWriter {
|
export class InteractionWriter {
|
||||||
constructor(private payloadClient: PayloadClient) {}
|
constructor(private payloadClient: PayloadClient) {}
|
||||||
|
|
||||||
|
|
@ -22,22 +21,28 @@ export class InteractionWriter {
|
||||||
analysis: LLMResponse | null,
|
analysis: LLMResponse | null,
|
||||||
): Promise<number> {
|
): Promise<number> {
|
||||||
try {
|
try {
|
||||||
const doc = await this.payloadClient.create<{ id: number }>(
|
const data: Record<string, unknown> = {
|
||||||
'community-interactions',
|
platform: WHATSAPP_PLATFORM_ID,
|
||||||
{
|
socialAccount: WHATSAPP_ACCOUNT_ID,
|
||||||
type: 'dm',
|
type: 'dm',
|
||||||
externalId: message.messageId,
|
externalId: message.messageId,
|
||||||
authorName: message.senderName,
|
author: {
|
||||||
authorHandle: message.from,
|
name: message.senderName || message.from,
|
||||||
message: message.text ?? `[${message.type}]`,
|
handle: message.from,
|
||||||
messageType: message.type,
|
|
||||||
...(analysis && {
|
|
||||||
analysisSentiment: 'neutral',
|
|
||||||
analysisIsMedicalQuestion: analysis.isMedicalQuestion,
|
|
||||||
}),
|
|
||||||
status: 'new',
|
|
||||||
tenant: 10,
|
|
||||||
},
|
},
|
||||||
|
message: message.text ?? `[${message.type}]`,
|
||||||
|
publishedAt: new Date().toISOString(),
|
||||||
|
status: 'new',
|
||||||
|
}
|
||||||
|
|
||||||
|
if (analysis?.isMedicalQuestion) {
|
||||||
|
data.flags = { isMedicalQuestion: true }
|
||||||
|
data.analysis = { sentiment: 'question' }
|
||||||
|
}
|
||||||
|
|
||||||
|
const doc = await this.payloadClient.create<{ id: number }>(
|
||||||
|
'community-interactions',
|
||||||
|
data,
|
||||||
)
|
)
|
||||||
|
|
||||||
log.debug({ id: doc.id, messageId: message.messageId }, 'Incoming message stored')
|
log.debug({ id: doc.id, messageId: message.messageId }, 'Incoming message stored')
|
||||||
|
|
@ -51,25 +56,31 @@ export class InteractionWriter {
|
||||||
async writeOutgoing(
|
async writeOutgoing(
|
||||||
to: string,
|
to: string,
|
||||||
text: string,
|
text: string,
|
||||||
replyToMessageId: string,
|
_replyToMessageId: string,
|
||||||
): Promise<number> {
|
): Promise<number> {
|
||||||
try {
|
try {
|
||||||
const doc = await this.payloadClient.create<{ id: number }>(
|
const doc = await this.payloadClient.create<{ id: number }>(
|
||||||
'community-interactions',
|
'community-interactions',
|
||||||
{
|
{
|
||||||
|
platform: WHATSAPP_PLATFORM_ID,
|
||||||
|
socialAccount: WHATSAPP_ACCOUNT_ID,
|
||||||
type: 'dm',
|
type: 'dm',
|
||||||
authorName: 'CCS Bot',
|
externalId: `bot-reply-${Date.now()}`,
|
||||||
authorHandle: 'bot',
|
author: {
|
||||||
|
name: 'CCS Bot',
|
||||||
|
handle: 'bot',
|
||||||
|
},
|
||||||
message: text,
|
message: text,
|
||||||
messageType: 'text',
|
publishedAt: new Date().toISOString(),
|
||||||
status: 'responded',
|
status: 'replied',
|
||||||
responseText: text,
|
response: {
|
||||||
responseType: 'bot',
|
text,
|
||||||
tenant: 10,
|
sentAt: new Date().toISOString(),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
log.debug({ id: doc.id, to, replyTo: replyToMessageId }, 'Outgoing message stored')
|
log.debug({ id: doc.id, to }, 'Outgoing message stored')
|
||||||
return doc.id
|
return doc.id
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.error({ error: (err as Error).message }, 'Failed to store outgoing message')
|
log.error({ error: (err as Error).message }, 'Failed to store outgoing message')
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue