Support @lid format in WhatsApp routes and relax number validation
This commit is contained in:
@@ -4,6 +4,11 @@ import _ from "lodash";
|
|||||||
import mime from "mime-types";
|
import mime from "mime-types";
|
||||||
import { MessageMedia } from "whatsapp-web.js";
|
import { MessageMedia } from "whatsapp-web.js";
|
||||||
|
|
||||||
|
const formatJid = (num: string) => {
|
||||||
|
if (num.includes("@")) return num;
|
||||||
|
return `${num}@c.us`;
|
||||||
|
};
|
||||||
|
|
||||||
const WaRoute = new Elysia({
|
const WaRoute = new Elysia({
|
||||||
prefix: "/wa",
|
prefix: "/wa",
|
||||||
tags: ["WhatsApp"]
|
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);
|
await chat.sendMessage(body.text);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -96,7 +101,7 @@ const WaRoute = new Elysia({
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const { number, caption, media } = body;
|
const { number, caption, media } = body;
|
||||||
const jid = `${number}@c.us`;
|
const jid = formatJid(number);
|
||||||
|
|
||||||
// Siapkan data media
|
// Siapkan data media
|
||||||
const { data, filename, mimetype } = media;
|
const { data, filename, mimetype } = media;
|
||||||
@@ -138,7 +143,7 @@ const WaRoute = new Elysia({
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
body: t.Object({
|
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"] })),
|
caption: t.Optional(t.String({ maxLength: 255, examples: ["Hello World"] })),
|
||||||
media: t.Object({
|
media: t.Object({
|
||||||
data: t.String({ examples: ["iVBORw0KGgoAAAANSUhEUgAAAAEAAAABC..."], description: "Base64 encoded media data" }),
|
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 {
|
return {
|
||||||
message: "✅ Message sent",
|
message: "✅ Message sent",
|
||||||
info: chat.id,
|
info: chat.id,
|
||||||
};
|
};
|
||||||
}, {
|
}, {
|
||||||
query: t.Object({
|
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"] }),
|
text: t.String({ examples: ["Hello World"] }),
|
||||||
}),
|
}),
|
||||||
detail: {
|
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();
|
// await chat.sendSeen();
|
||||||
return {
|
return {
|
||||||
message: "✅ Seen sent",
|
message: "✅ Seen sent",
|
||||||
@@ -229,7 +234,7 @@ const WaRoute = new Elysia({
|
|||||||
};
|
};
|
||||||
}, {
|
}, {
|
||||||
query: t.Object({
|
query: t.Object({
|
||||||
nom: t.String({ minLength: 10, maxLength: 15, examples: ["6281234567890"] }),
|
nom: t.String({ minLength: 5, maxLength: 50, examples: ["6281234567890", "1234567890@lid"] }),
|
||||||
}),
|
}),
|
||||||
detail: {
|
detail: {
|
||||||
summary: "Send seen to WhatsApp",
|
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();
|
await chat.sendStateTyping();
|
||||||
return {
|
return {
|
||||||
message: "✅ Typing sent",
|
message: "✅ Typing sent",
|
||||||
@@ -271,7 +276,7 @@ const WaRoute = new Elysia({
|
|||||||
};
|
};
|
||||||
}, {
|
}, {
|
||||||
query: t.Object({
|
query: t.Object({
|
||||||
nom: t.String({ minLength: 10, maxLength: 15, examples: ["6281234567890"] }),
|
nom: t.String({ minLength: 5, maxLength: 50, examples: ["6281234567890", "1234567890@lid"] }),
|
||||||
}),
|
}),
|
||||||
detail: {
|
detail: {
|
||||||
summary: "Send typing to WhatsApp",
|
summary: "Send typing to WhatsApp",
|
||||||
|
|||||||
Reference in New Issue
Block a user