tambahannya
This commit is contained in:
@@ -6,6 +6,17 @@ import { Whatsapp, whatsappApiInit } from "../lib/wa-api/wa-api";
|
|||||||
import type { GetParams, PostData } from "whatsapp-api-js/types";
|
import type { GetParams, PostData } from "whatsapp-api-js/types";
|
||||||
import { logger } from "../lib/logger";
|
import { logger } from "../lib/logger";
|
||||||
|
|
||||||
|
|
||||||
|
import { WhatsAppClient, WhatsAppMessageType } from 'whatsapp-client-sdk';
|
||||||
|
|
||||||
|
const client = new WhatsAppClient({
|
||||||
|
accessToken: 'your-access-token', // Required: Get from Meta Developer Console
|
||||||
|
phoneNumberId: 'your-phone-number-id', // Required: Your WhatsApp Business phone number ID
|
||||||
|
webhookVerifyToken: 'your-verify-token' // Required: For receiving messages
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async function fetchWithTimeout(input: RequestInfo, init: RequestInit, timeoutMs = 120_000) {
|
async function fetchWithTimeout(input: RequestInfo, init: RequestInit, timeoutMs = 120_000) {
|
||||||
const controller = new AbortController()
|
const controller = new AbortController()
|
||||||
const id = setTimeout(() => controller.abort(), timeoutMs)
|
const id = setTimeout(() => controller.abort(), timeoutMs)
|
||||||
@@ -83,74 +94,90 @@ const WaHookRoute = new Elysia({
|
|||||||
Whatsapp.post(body as PostData)
|
Whatsapp.post(body as PostData)
|
||||||
logger.info("[POST] Incoming WhatsApp Webhook:", body)
|
logger.info("[POST] Incoming WhatsApp Webhook:", body)
|
||||||
|
|
||||||
const create = await prisma.waHook.create({
|
const webhook = client.parseWebhook(body)
|
||||||
data: {
|
|
||||||
data: body,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const waHook = body as WAHookMessage
|
if (webhook[0]?.type === WhatsAppMessageType.TEXT) {
|
||||||
const flow = await prisma.chatFlows.findUnique({
|
const message = webhook[0]?.text
|
||||||
where: {
|
const from = webhook[0]?.from
|
||||||
id: "1",
|
const name = webhook[0].contact?.name
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!flow) {
|
const dataMessage = {
|
||||||
logger.info("[POST] no flow found")
|
message,
|
||||||
}
|
from,
|
||||||
|
name,
|
||||||
if (flow?.defaultFlow && flow.active) {
|
|
||||||
const { flowUrl, flowToken } = flow
|
|
||||||
const question = waHook?.entry[0]?.changes[0]?.value?.messages[0]?.text?.body
|
|
||||||
const contacts = waHook?.entry[0]?.changes[0]?.value?.contacts[0]
|
|
||||||
const name = contacts?.profile?.name
|
|
||||||
const number = contacts?.wa_id
|
|
||||||
|
|
||||||
const response = await fetchWithTimeout(`${flowUrl}/prediction/${flow.defaultFlow}`, {
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${flowToken}`,
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify({
|
|
||||||
question,
|
|
||||||
overrideConfig: {
|
|
||||||
sessionId: `${_.kebabCase(name)}_x_${number}`,
|
|
||||||
vars: { userName: _.kebabCase(name), userPhone: number },
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
|
|
||||||
const responseText = await response.text()
|
|
||||||
try {
|
|
||||||
const result = JSON.parse(responseText)
|
|
||||||
let createData = create.data as any
|
|
||||||
createData.answer = {
|
|
||||||
text: result.text,
|
|
||||||
type: "text",
|
|
||||||
flowId: flow.defaultFlow
|
|
||||||
}
|
|
||||||
|
|
||||||
await prisma.waHook.update({
|
|
||||||
where: {
|
|
||||||
id: create.id,
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
data: createData,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
if (flow?.waPhoneNumberId && flow?.waToken && number) {
|
|
||||||
// await sendReplyMessage(number, result.text, flow.waPhoneNumberId, flow.waToken)
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.log(error)
|
|
||||||
console.log(responseText)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.info(`[POST] Incoming WhatsApp Webhook: ${JSON.stringify(dataMessage)}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// const create = await prisma.waHook.create({
|
||||||
|
// data: {
|
||||||
|
// data: body,
|
||||||
|
// },
|
||||||
|
// });
|
||||||
|
|
||||||
|
// const waHook = body as WAHookMessage
|
||||||
|
// const flow = await prisma.chatFlows.findUnique({
|
||||||
|
// where: {
|
||||||
|
// id: "1",
|
||||||
|
// },
|
||||||
|
// })
|
||||||
|
|
||||||
|
// if (!flow) {
|
||||||
|
// logger.info("[POST] no flow found")
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (flow?.defaultFlow && flow.active) {
|
||||||
|
// const { flowUrl, flowToken } = flow
|
||||||
|
// const question = waHook?.entry[0]?.changes[0]?.value?.messages[0]?.text?.body
|
||||||
|
// const contacts = waHook?.entry[0]?.changes[0]?.value?.contacts[0]
|
||||||
|
// const name = contacts?.profile?.name
|
||||||
|
// const number = contacts?.wa_id
|
||||||
|
|
||||||
|
// const response = await fetchWithTimeout(`${flowUrl}/prediction/${flow.defaultFlow}`, {
|
||||||
|
// headers: {
|
||||||
|
// Authorization: `Bearer ${flowToken}`,
|
||||||
|
// 'Content-Type': 'application/json',
|
||||||
|
// },
|
||||||
|
// method: 'POST',
|
||||||
|
// body: JSON.stringify({
|
||||||
|
// question,
|
||||||
|
// overrideConfig: {
|
||||||
|
// sessionId: `${_.kebabCase(name)}_x_${number}`,
|
||||||
|
// vars: { userName: _.kebabCase(name), userPhone: number },
|
||||||
|
// },
|
||||||
|
// }),
|
||||||
|
// })
|
||||||
|
|
||||||
|
// const responseText = await response.text()
|
||||||
|
// try {
|
||||||
|
// const result = JSON.parse(responseText)
|
||||||
|
// let createData = create.data as any
|
||||||
|
// createData.answer = {
|
||||||
|
// text: result.text,
|
||||||
|
// type: "text",
|
||||||
|
// flowId: flow.defaultFlow
|
||||||
|
// }
|
||||||
|
|
||||||
|
// await prisma.waHook.update({
|
||||||
|
// where: {
|
||||||
|
// id: create.id,
|
||||||
|
// },
|
||||||
|
// data: {
|
||||||
|
// data: createData,
|
||||||
|
// },
|
||||||
|
// })
|
||||||
|
|
||||||
|
// if (flow?.waPhoneNumberId && flow?.waToken && number) {
|
||||||
|
// // await sendReplyMessage(number, result.text, flow.waPhoneNumberId, flow.waToken)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// } catch (error) {
|
||||||
|
// console.log(error)
|
||||||
|
// console.log(responseText)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
@@ -207,6 +234,3 @@ const WaHookRoute = new Elysia({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export default WaHookRoute;
|
export default WaHookRoute;
|
||||||
|
|
||||||
// Initialize WhatsApp API
|
|
||||||
// whatsappApiInit()
|
|
||||||
Reference in New Issue
Block a user