tambahannya
This commit is contained in:
@@ -11,9 +11,11 @@ const APP_SECRET: string = process.env.WA_APP_SECRET!;
|
||||
logger.info("WA API started");
|
||||
|
||||
// Inisialisasi WhatsApp API dengan typing generik jika diperlukan (contoh: number sebagai tipe session)
|
||||
const Whatsapp = new WhatsAppAPI<number>({
|
||||
export const Whatsapp = new WhatsAppAPI<number>({
|
||||
token: TOKEN,
|
||||
appSecret: APP_SECRET
|
||||
appSecret: APP_SECRET,
|
||||
webhookVerifyToken: process.env.WA_WEBHOOK_TOKEN!,
|
||||
v: "v23.0"
|
||||
});
|
||||
|
||||
// Tipe untuk request body dari server (bisa disesuaikan dengan framework seperti Express, Elysia, Hono, dll)
|
||||
@@ -36,8 +38,10 @@ export async function post(req: PostRequest) {
|
||||
}
|
||||
|
||||
export function whatsappApiInit() {
|
||||
logger.info("WA API initialized");
|
||||
// Handler jika ada pesan masuk dari user
|
||||
Whatsapp.on.message = async ({ phoneID, from, message, name, Whatsapp, reply }) => {
|
||||
logger.info("WA API received");
|
||||
logger.info(
|
||||
`User ${name} (${from}) sent to bot ${phoneID} ${JSON.stringify(message)}`
|
||||
);
|
||||
|
||||
@@ -4,23 +4,73 @@ import path from "path";
|
||||
|
||||
const LOGS_PATH = process.env.APP_LOGS_PATH || "./.logs";
|
||||
|
||||
const LogsRoute = new Elysia({
|
||||
prefix: "/logs", // lebih aman pakai "/" di depan
|
||||
tags: ["logs"]
|
||||
})
|
||||
.get("/app", async () => {
|
||||
const filePath = path.join(LOGS_PATH, "app.log");
|
||||
// Pastikan folder log ada saat startup
|
||||
(async () => {
|
||||
try {
|
||||
await fs.access(LOGS_PATH);
|
||||
} catch {
|
||||
await fs.mkdir(LOGS_PATH, { recursive: true });
|
||||
}
|
||||
})();
|
||||
|
||||
try {
|
||||
const logs = await fs.readFile(filePath, "utf-8");
|
||||
// Format: array line-by-line (optional)
|
||||
return logs.split("\n").filter(Boolean);
|
||||
} catch (err) {
|
||||
// Helper baca log file + optional filter
|
||||
async function readLogs(limit?: number, level?: string) {
|
||||
const filePath = path.join(LOGS_PATH, "app.log");
|
||||
|
||||
try {
|
||||
const data = await fs.readFile(filePath, "utf-8");
|
||||
let lines = data.trim().split("\n").filter(Boolean); // tiap baris = JSON log
|
||||
|
||||
if (limit) {
|
||||
lines = lines.slice(-limit);
|
||||
}
|
||||
|
||||
let parsed = lines.map((line) => {
|
||||
try {
|
||||
return JSON.parse(line);
|
||||
} catch {
|
||||
return { raw: line };
|
||||
}
|
||||
});
|
||||
|
||||
// Filter berdasarkan level (error, info, debug, warn)
|
||||
if (level) {
|
||||
parsed = parsed.filter((log) =>
|
||||
log.level ? String(log.level) === level || log.level === level : false
|
||||
);
|
||||
}
|
||||
|
||||
return parsed;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const LogsRoute = new Elysia({
|
||||
prefix: "/logs",
|
||||
tags: ["logs"],
|
||||
})
|
||||
/**
|
||||
* GET /logs/app?lines=100&level=error
|
||||
*/
|
||||
.get("/app", async ({ query }) => {
|
||||
const lines = query.lines ? Number(query.lines) : undefined;
|
||||
const level = query.level || undefined;
|
||||
|
||||
const logs = await readLogs(lines, level);
|
||||
|
||||
if (!logs) {
|
||||
return {
|
||||
error: "Log file not found or cannot be read",
|
||||
details: (err as Error).message,
|
||||
success: false,
|
||||
message: "Log file not found or unreadable",
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
total: logs.length,
|
||||
data: logs,
|
||||
};
|
||||
});
|
||||
|
||||
export default LogsRoute;
|
||||
|
||||
@@ -2,9 +2,9 @@ import Elysia, { t } from "elysia";
|
||||
import { prisma } from "../lib/prisma";
|
||||
import type { WAHookMessage } from "types/wa_messages";
|
||||
import _ from "lodash";
|
||||
import { whatsappApiInit } from "../lib/wa-api/wa-api";
|
||||
|
||||
whatsappApiInit()
|
||||
import { Whatsapp, whatsappApiInit } from "../lib/wa-api/wa-api";
|
||||
import type { GetParams, PostData } from "whatsapp-api-js/types";
|
||||
import { logger } from "../lib/logger";
|
||||
|
||||
async function fetchWithTimeout(input: RequestInfo, init: RequestInit, timeoutMs = 120_000) {
|
||||
const controller = new AbortController()
|
||||
@@ -40,6 +40,7 @@ const WaHookRoute = new Elysia({
|
||||
})
|
||||
// ✅ Handle verifikasi Webhook (GET)
|
||||
.get("/hook", async (ctx) => {
|
||||
Whatsapp.get(ctx.query as GetParams)
|
||||
const { query, set } = ctx;
|
||||
const mode = query["hub.mode"];
|
||||
const challenge = query["hub.challenge"];
|
||||
@@ -79,7 +80,8 @@ const WaHookRoute = new Elysia({
|
||||
|
||||
// ✅ Handle incoming message (POST)
|
||||
.post("/hook", async ({ body }) => {
|
||||
console.log("Incoming WhatsApp Webhook:", body);
|
||||
Whatsapp.post(body as PostData)
|
||||
logger.info("[POST] Incoming WhatsApp Webhook:", body)
|
||||
|
||||
const create = await prisma.waHook.create({
|
||||
data: {
|
||||
@@ -95,7 +97,7 @@ const WaHookRoute = new Elysia({
|
||||
})
|
||||
|
||||
if (!flow) {
|
||||
console.log("no flow found")
|
||||
logger.info("[POST] no flow found")
|
||||
}
|
||||
|
||||
if (flow?.defaultFlow && flow.active) {
|
||||
@@ -205,3 +207,6 @@ const WaHookRoute = new Elysia({
|
||||
});
|
||||
|
||||
export default WaHookRoute;
|
||||
|
||||
// Initialize WhatsApp API
|
||||
whatsappApiInit()
|
||||
Reference in New Issue
Block a user