tambahannya

This commit is contained in:
bipproduction
2025-10-23 12:10:50 +08:00
parent 367759c4cc
commit 80b6da80c5

View File

@@ -32,15 +32,9 @@ async function fetchWithTimeout(
const FLOW_ID = "1"; const FLOW_ID = "1";
async function flowAi({ async function flowAi({
message, message
question,
name,
number,
}: { }: {
message: ProcessedIncomingMessage; message: ProcessedIncomingMessage;
question: string;
name: string;
number: string;
}) { }) {
const flow = await prisma.chatFlows.findUnique({ const flow = await prisma.chatFlows.findUnique({
where: { id: FLOW_ID }, where: { id: FLOW_ID },
@@ -55,7 +49,7 @@ async function flowAi({
logger.info("[POST] flow found"); logger.info("[POST] flow found");
await client.markMessageAsRead(message.id); await client.markMessageAsRead(message.id);
await client.sendTypingIndicator(message.from); await client.sendTypingIndicatorWithDuration(message.from, 5000);
const { flowUrl, flowToken } = flow; const { flowUrl, flowToken } = flow;
@@ -69,10 +63,10 @@ async function flowAi({
}, },
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
question, question: message.text,
overrideConfig: { overrideConfig: {
sessionId: `${_.kebabCase(name)}_x_${number}`, sessionId: `${_.kebabCase(message.contact?.name)}_x_${message.from}`,
vars: { userName: _.kebabCase(name), userPhone: number }, vars: { userName: _.kebabCase(message.contact?.name), userPhone: message.from },
}, },
}), }),
} }
@@ -85,9 +79,9 @@ async function flowAi({
await prisma.waHook.create({ await prisma.waHook.create({
data: { data: {
data: JSON.stringify({ data: JSON.stringify({
question, question: message.text,
name, name: message.contact?.name,
number, number: message.from,
answer: result.text, answer: result.text,
flowId: flow.defaultFlow, flowId: flow.defaultFlow,
}), }),
@@ -95,7 +89,8 @@ async function flowAi({
}); });
if (flow.waPhoneNumberId && flow.waToken && flow.active) { if (flow.waPhoneNumberId && flow.waToken && flow.active) {
await client.sendText(number, result.text); await new Promise(resolve => setTimeout(resolve, 3000));
await client.sendText(message.from, result.text);
} }
} catch (error) { } catch (error) {
logger.error(`[POST] Error parsing AI response ${error}`); logger.error(`[POST] Error parsing AI response ${error}`);
@@ -168,9 +163,6 @@ const WaHookRoute = new Elysia({
// gunakan void agar tidak ada warning “unawaited promise” // gunakan void agar tidak ada warning “unawaited promise”
void flowAi({ void flowAi({
message: webhook[0], message: webhook[0],
question: messageQuestion,
name: name || "default_name",
number: from,
}); });
} }
} }