lidformatter 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +34 -0
  2. package/index.js +67 -0
  3. package/package.json +21 -0
package/README.md ADDED
@@ -0,0 +1,34 @@
1
+ <p align="center">
2
+ <img src="https://img.shields.io/badge/Node%20js-339933?style=for-the-badge&logo=nodedotjs&logoColor=white" alt="Node.js">
3
+ <img src="https://img.shields.io/badge/npm-CB3837?style=for-the-badge&logo=npm&logoColor=white" alt="NPM">
4
+ <img src="https://img.shields.io/badge/MIT-green?style=for-the-badge" alt="License">
5
+ </p>
6
+
7
+ <p align="center">
8
+ <img src="https://img.shields.io/npm/dependency-version/lidformatter/dev/node?style=flat-square&color=339933" alt="Node Engine">
9
+ <img src="https://img.shields.io/npm/dt/lidformatter?style=flat-square&color=007ec6" alt="NPM Total Downloads">
10
+ </p>
11
+
12
+ ### Installing Modules
13
+ ```bash
14
+ npm i lidformatter
15
+ ```
16
+
17
+ ### How to work
18
+ ```javascript
19
+ import { formatMessageWithJid } from "lidformatter";
20
+
21
+ bot.ev.on("messages.upsert", async (msg) => {
22
+ const msget = msg.messages[0];
23
+ if (!msget.message) return;
24
+ // get jid via message
25
+ const m = await formatMessageWithJid(bot, msget);
26
+ console.log(m.jidUser); // Output: 628xxx@s.whatsapp.net
27
+ });
28
+
29
+ ```
30
+
31
+
32
+ > [!IMPORTANT]
33
+ > WhatsApp may update LID structures at any time. Please refer to the official Baileys documentation for further updates.
34
+
package/index.js ADDED
@@ -0,0 +1,67 @@
1
+ function cleanLidString(lid) {
2
+ if (!lid.includes(':')) return lid;
3
+ const [userWithDevice, domain] = lid.split('@');
4
+ const [user] = userWithDevice.split(':');
5
+ return `${user}@${domain}`;
6
+ }
7
+
8
+ export async function lidToJid(sock, lid) {
9
+ if (!lid || typeof lid !== 'string') return '';
10
+ if (!lid.endsWith('@lid')) return lid;
11
+
12
+ try {
13
+ const hasDevice = lid.includes(':') ? `:${lid.split(':')[1].split('@')[0]}` : '';
14
+ const pureLid = cleanLidString(lid);
15
+ const lidUser = pureLid.split('@')[0];
16
+
17
+ let stored = await sock.authState.keys.get('lid-mapping', [`${lidUser}_reverse`]);
18
+ let pnUser = stored[`${lidUser}_reverse`];
19
+
20
+ if (pnUser) {
21
+ return `${pnUser}${hasDevice}@s.whatsapp.net`;
22
+ }
23
+
24
+ if (sock.onWhatsApp) {
25
+ const result = await sock.onWhatsApp(pureLid).catch(() => []);
26
+ if (result && result.length > 0 && result[0].exists) {
27
+ stored = await sock.authState.keys.get('lid-mapping', [`${lidUser}_reverse`]);
28
+ pnUser = stored[`${lidUser}_reverse`];
29
+ if (pnUser) {
30
+ return `${pnUser}${hasDevice}@s.whatsapp.net`;
31
+ }
32
+ }
33
+ }
34
+ return lid;
35
+ } catch (error) {
36
+ console.error('[lidToJid Error]:', error);
37
+ return lid;
38
+ }
39
+ }
40
+
41
+ export async function formatMessageWithJid(bot, msget) {
42
+ try {
43
+ const rawSender = msget.key.participant || msget.key.remoteJid;
44
+ let jidAsli = '';
45
+
46
+ if (msget.key.remoteJidAlt && msget.key.remoteJidAlt.endsWith("@s.whatsapp.net")) {
47
+ jidAsli = msget.key.remoteJidAlt;
48
+ } else {
49
+ jidAsli = await lidToJid(bot, rawSender);
50
+ }
51
+
52
+ return {
53
+ ...msget,
54
+ chat: msget.key.remoteJid,
55
+ sender: rawSender,
56
+ jidUser: jidAsli
57
+ };
58
+ } catch (error) {
59
+ console.error(error);
60
+ return {
61
+ ...msget,
62
+ chat: msget.key.remoteJid,
63
+ sender: msget.key.participant || msget.key.remoteJid,
64
+ jidUser: msget.key.participant || msget.key.remoteJid
65
+ };
66
+ }
67
+ }
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "lidformatter",
3
+ "version": "1.0.0",
4
+ "description": "Helper to format and convert WhatsApp LID to JID for Baileys library",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "engines": {
8
+ "node": ">=20.0.0"
9
+ },
10
+ "keywords": [
11
+ "baileys",
12
+ "helper",
13
+ "lid",
14
+ "whatsapp",
15
+ "jid",
16
+ "bot",
17
+ "automation"
18
+ ],
19
+ "author": "zoisgood",
20
+ "license": "MIT"
21
+ }