Support @lid format in WhatsApp routes and relax number validation

This commit is contained in:
bipproduction
2026-02-05 16:35:28 +08:00
parent 98b134e72a
commit 32610ebfd1

View File

@@ -4,6 +4,11 @@ import _ from "lodash";
import mime from "mime-types";
import { MessageMedia } from "whatsapp-web.js";
const formatJid = (num: string) => {
if (num.includes("@")) return num;
return `${num}@c.us`;
};
const WaRoute = new Elysia({
prefix: "/wa",
tags: ["WhatsApp"]
@@ -66,7 +71,7 @@ const WaRoute = new Elysia({
}
const chat = await client.getChatById(`${body.number}@c.us`);
const chat = await client.getChatById(formatJid(body.number));
await chat.sendMessage(body.text);
return {
@@ -96,7 +101,7 @@ const WaRoute = new Elysia({
try {
const { number, caption, media } = body;
const jid = `${number}@c.us`;
const jid = formatJid(number);
// Siapkan data media
const { data, filename, mimetype } = media;
@@ -138,7 +143,7 @@ const WaRoute = new Elysia({
},
{
body: t.Object({
number: t.String({ minLength: 10, maxLength: 15, examples: ["6281234567890"] }),
number: t.String({ minLength: 5, maxLength: 50, examples: ["6281234567890", "1234567890@lid"] }),
caption: t.Optional(t.String({ maxLength: 255, examples: ["Hello World"] })),
media: t.Object({
data: t.String({ examples: ["iVBORw0KGgoAAAANSUhEUgAAAAEAAAABC..."], description: "Base64 encoded media data" }),
@@ -179,14 +184,14 @@ const WaRoute = new Elysia({
};
}
const chat = await state.client.sendMessage(`${nom}@c.us`, text);
const chat = await state.client.sendMessage(formatJid(nom as string), text as string);
return {
message: "✅ Message sent",
info: chat.id,
};
}, {
query: t.Object({
nom: t.String({ minLength: 10, maxLength: 15, examples: ["6281234567890"] }),
nom: t.String({ minLength: 5, maxLength: 50, examples: ["6281234567890", "1234567890@lid"] }),
text: t.String({ examples: ["Hello World"] }),
}),
detail: {
@@ -221,7 +226,7 @@ const WaRoute = new Elysia({
};
}
const chat = await state.client.getChatById(`${nom}@c.us`);
const chat = await state.client.getChatById(formatJid(nom as string));
// await chat.sendSeen();
return {
message: "✅ Seen sent",
@@ -229,7 +234,7 @@ const WaRoute = new Elysia({
};
}, {
query: t.Object({
nom: t.String({ minLength: 10, maxLength: 15, examples: ["6281234567890"] }),
nom: t.String({ minLength: 5, maxLength: 50, examples: ["6281234567890", "1234567890@lid"] }),
}),
detail: {
summary: "Send seen to WhatsApp",
@@ -263,7 +268,7 @@ const WaRoute = new Elysia({
};
}
const chat = await state.client.getChatById(`${nom}@c.us`);
const chat = await state.client.getChatById(formatJid(nom as string));
await chat.sendStateTyping();
return {
message: "✅ Typing sent",
@@ -271,7 +276,7 @@ const WaRoute = new Elysia({
};
}, {
query: t.Object({
nom: t.String({ minLength: 10, maxLength: 15, examples: ["6281234567890"] }),
nom: t.String({ minLength: 5, maxLength: 50, examples: ["6281234567890", "1234567890@lid"] }),
}),
detail: {
summary: "Send typing to WhatsApp",