tambahannya
This commit is contained in:
@@ -1,96 +0,0 @@
|
|||||||
import { WhatsAppAPI } from "whatsapp-api-js";
|
|
||||||
import { Document, Image, Text } from "whatsapp-api-js/messages";
|
|
||||||
import type { IncomingHttpHeaders } from "http";
|
|
||||||
import { logger } from "../logger";
|
|
||||||
|
|
||||||
|
|
||||||
// Jangan hardcode — ini hanya contoh
|
|
||||||
const TOKEN: string = process.env.WA_TOKEN!;
|
|
||||||
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)
|
|
||||||
export const Whatsapp = new WhatsAppAPI<number>({
|
|
||||||
token: TOKEN,
|
|
||||||
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)
|
|
||||||
interface PostRequest {
|
|
||||||
data: string | Buffer;
|
|
||||||
headers: IncomingHttpHeaders & {
|
|
||||||
"x-hub-signature-256"?: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fungsi handler webhook POST
|
|
||||||
export async function post(req: PostRequest) {
|
|
||||||
logger.info("WA API received");
|
|
||||||
const signature = req.headers["x-hub-signature-256"] ?? "";
|
|
||||||
return await Whatsapp.post(
|
|
||||||
JSON.parse(req.data.toString()),
|
|
||||||
req.data.toString(),
|
|
||||||
signature,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
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)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
let response;
|
|
||||||
|
|
||||||
switch (message.type) {
|
|
||||||
case "text":
|
|
||||||
logger.info("Text received");
|
|
||||||
response = await reply(
|
|
||||||
new Text(`*${name}* said:\n\n${message.text.body}`),
|
|
||||||
true
|
|
||||||
);
|
|
||||||
logger.info("Text sent");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "image":
|
|
||||||
logger.info("Image received");
|
|
||||||
response = await reply(
|
|
||||||
new Image(message.image.id, true, `Nice photo, ${name}`)
|
|
||||||
);
|
|
||||||
logger.info("Image sent");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "document":
|
|
||||||
logger.info("Document received");
|
|
||||||
response = await reply(
|
|
||||||
new Document(message.document.id, true, undefined, "Our document")
|
|
||||||
);
|
|
||||||
logger.info("Document sent");
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
logger.info(
|
|
||||||
"Unhandled message type. More types available: contacts, locations, templates, interactive, reactions, audio, video, etc."
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("Response sent");
|
|
||||||
|
|
||||||
// Tandai pesan sudah dibaca
|
|
||||||
Whatsapp.markAsRead(phoneID, message.id);
|
|
||||||
|
|
||||||
return 200;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Handler saat pesan berhasil terkirim
|
|
||||||
Whatsapp.on.sent = ({ phoneID, to, message }) => {
|
|
||||||
logger.info(`Bot ${phoneID} sent to user ${to} ${message}`);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -236,6 +236,8 @@ const WaHookRoute = new Elysia({
|
|||||||
async ({ body }) => {
|
async ({ body }) => {
|
||||||
const webhook = client.parseWebhook(body);
|
const webhook = client.parseWebhook(body);
|
||||||
|
|
||||||
|
logger.info(`[POST] Webhook Type: ${webhook[0]?.type}`);
|
||||||
|
|
||||||
if (webhook[0]?.type === WhatsAppMessageType.TEXT) {
|
if (webhook[0]?.type === WhatsAppMessageType.TEXT) {
|
||||||
const messageQuestion = webhook[0]?.text;
|
const messageQuestion = webhook[0]?.text;
|
||||||
const from = webhook[0]?.from;
|
const from = webhook[0]?.from;
|
||||||
@@ -266,12 +268,15 @@ const WaHookRoute = new Elysia({
|
|||||||
|
|
||||||
const buffer = await client.downloadMedia(message.media?.id!);
|
const buffer = await client.downloadMedia(message.media?.id!);
|
||||||
const media_data = buffer.toString("base64");
|
const media_data = buffer.toString("base64");
|
||||||
const media_name = message.media?.filename;
|
const media_name = message.media?.filename || "default_filename";
|
||||||
const media_mime = message.media?.mime_type;
|
const media_mime = message.media?.mime_type || "default_mime_type";
|
||||||
|
|
||||||
// gunakan void agar tidak ada warning “unawaited promise"
|
// gunakan void agar tidak ada warning “unawaited promise"
|
||||||
void flowAiText({
|
void flowAiImage({
|
||||||
message: webhook[0],
|
message,
|
||||||
|
media_data,
|
||||||
|
media_name,
|
||||||
|
media_mime,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user