tambahannya
This commit is contained in:
93
xx.ts
93
xx.ts
@@ -1,24 +1,79 @@
|
||||
import fetch from "node-fetch";
|
||||
import fs from "fs";
|
||||
import { WhatsAppAPI } from "whatsapp-api-js";
|
||||
import { Document, Image, Text } from "whatsapp-api-js/messages";
|
||||
import type { IncomingHttpHeaders } from "http";
|
||||
|
||||
const token = "EAALP22EWyC4BPrjshjjYBbPVKWp4Gp2ljkb7hCmgpZArLigB8XNmRoXBomDJm6aWnjpKpqehdVatbfFAHeGaQftGkNBp4Oyds9apr4lOQjG2YWYEzZC05ZAo7MARnfXn7FVua0iaeNMh2gunMZBd6pO58wjAUP3gqLiUrwASeOnJu5pW3tKg6fHubALBlQZDZD"; // dari Meta Developer > App > Access Token
|
||||
const mediaId = "838467435201133"; // dari webhook
|
||||
// Jangan hardcode — ini hanya contoh
|
||||
const TOKEN: string = "YOUR_TOKEN";
|
||||
const APP_SECRET: string = "YOUR_SECRET";
|
||||
|
||||
// 1. Dapatkan URL file asli
|
||||
const mediaInfo = await fetch(
|
||||
`https://graph.facebook.com/v19.0/${mediaId}?access_token=${token}`
|
||||
).then(res => res.json()) as any;
|
||||
|
||||
// mediaInfo.url berisi link unduhan sementara
|
||||
const fileUrl = mediaInfo.url;
|
||||
|
||||
const fileResponse = await fetch(fileUrl, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`, // wajib!
|
||||
},
|
||||
// Inisialisasi WhatsApp API dengan typing generik jika diperlukan (contoh: number sebagai tipe session)
|
||||
const Whatsapp = new WhatsAppAPI<number>({
|
||||
token: TOKEN,
|
||||
appSecret: APP_SECRET
|
||||
});
|
||||
|
||||
const buffer = await fileResponse.arrayBuffer();
|
||||
fs.writeFileSync("sticker.webp", Buffer.from(buffer));
|
||||
// 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;
|
||||
};
|
||||
}
|
||||
|
||||
console.log("Sticker berhasil diunduh!");
|
||||
// Fungsi handler webhook POST
|
||||
export async function post(req: PostRequest) {
|
||||
const signature = req.headers["x-hub-signature-256"] ?? "";
|
||||
return await Whatsapp.post(
|
||||
JSON.parse(req.data.toString()),
|
||||
req.data.toString(),
|
||||
signature,
|
||||
);
|
||||
}
|
||||
|
||||
// Handler jika ada pesan masuk dari user
|
||||
Whatsapp.on.message = async ({ phoneID, from, message, name, Whatsapp, reply }) => {
|
||||
console.log(
|
||||
`User ${name} (${from}) sent to bot ${phoneID} ${JSON.stringify(message)}`
|
||||
);
|
||||
|
||||
let response;
|
||||
|
||||
switch (message.type) {
|
||||
case "text":
|
||||
response = await reply(
|
||||
new Text(`*${name}* said:\n\n${message.text.body}`),
|
||||
true
|
||||
);
|
||||
break;
|
||||
|
||||
case "image":
|
||||
response = await reply(
|
||||
new Image(message.image.id, true, `Nice photo, ${name}`)
|
||||
);
|
||||
break;
|
||||
|
||||
case "document":
|
||||
response = await reply(
|
||||
new Document(message.document.id, true, undefined, "Our document")
|
||||
);
|
||||
break;
|
||||
|
||||
default:
|
||||
console.log(
|
||||
"Unhandled message type. More types available: contacts, locations, templates, interactive, reactions, audio, video, etc."
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
console.log(response);
|
||||
|
||||
// Tandai pesan sudah dibaca
|
||||
Whatsapp.markAsRead(phoneID, message.id);
|
||||
|
||||
return 200;
|
||||
};
|
||||
|
||||
// Handler saat pesan berhasil terkirim
|
||||
Whatsapp.on.sent = ({ phoneID, to, message }) => {
|
||||
console.log(`Bot ${phoneID} sent to user ${to} ${message}`);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user