lidformatter 1.0.1 → 1.0.2
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.
- package/README.md +17 -3
- package/index.js +57 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,6 +8,17 @@
|
|
|
8
8
|
<img src="https://img.shields.io/npm/dm/lidformatter?style=flat-square&color=007ec6" alt="NPM Downloads per Month">
|
|
9
9
|
</p>
|
|
10
10
|
|
|
11
|
+
|
|
12
|
+
## What's New
|
|
13
|
+
|
|
14
|
+
<details>
|
|
15
|
+
<summary><b>Watch the next version</b></summary>
|
|
16
|
+
|
|
17
|
+
> adding a mapping mention user
|
|
18
|
+
|
|
19
|
+
</details>
|
|
20
|
+
|
|
21
|
+
|
|
11
22
|
### Installing Modules
|
|
12
23
|
```bash
|
|
13
24
|
npm i lidformatter
|
|
@@ -20,14 +31,17 @@ import { formatMessageWithJid } from "lidformatter";
|
|
|
20
31
|
bot.ev.on("messages.upsert", async (msg) => {
|
|
21
32
|
const msget = msg.messages[0];
|
|
22
33
|
if (!msget.message) return;
|
|
23
|
-
//
|
|
34
|
+
// Process message parsing and convert LID metadata into native standard JIDs
|
|
24
35
|
const m = await formatMessageWithJid(bot, msget);
|
|
25
|
-
console.log(m.jidUser);
|
|
36
|
+
console.log("Sender JID:", m.jidUser); // Output: 628xxx@s.whatsapp.net
|
|
37
|
+
console.log("Mentioned JIDs:", m.mentions); // Output: ['628xxx@s.whatsapp.net']
|
|
26
38
|
});
|
|
27
39
|
|
|
40
|
+
|
|
28
41
|
```
|
|
29
42
|
|
|
30
43
|
|
|
31
44
|
> [!IMPORTANT]
|
|
32
45
|
> WhatsApp may update LID structures at any time. Please refer to the official Baileys documentation for further updates.
|
|
33
|
-
|
|
46
|
+
> WhatsApp may update LID structures at any time. Please refer to the official Baileys documentation for further updates.
|
|
47
|
+
> [Read the docs](https://baileys.wiki/docs/intro/ "read whiskeysockets/baileys docs.")
|
package/index.js
CHANGED
|
@@ -38,30 +38,81 @@ export async function lidToJid(sock, lid) {
|
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
/**
|
|
42
|
+
* Converts LID-based group mentions within the message text into standard phone number JIDs.
|
|
43
|
+
* Now dynamically resolves nested text and converts @lid mentions inside the array.
|
|
44
|
+
*/
|
|
45
|
+
export async function convertGroupMention(sock, m) {
|
|
46
|
+
// 1. Extract text dynamically from multiple potential Baileys structures
|
|
47
|
+
let text = m.body ||
|
|
48
|
+
m.message?.extendedTextMessage?.text ||
|
|
49
|
+
m.message?.conversation || '';
|
|
50
|
+
|
|
51
|
+
// 2. Extract mentionedJids from contextInfo safely
|
|
52
|
+
const rawMentions = m.message?.extendedTextMessage?.contextInfo?.mentionedJid || [];
|
|
53
|
+
const cleanMentions = [];
|
|
54
|
+
|
|
55
|
+
if (rawMentions.length === 0) {
|
|
56
|
+
return { text, mentions: [] };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 3. Loop through mentions, convert any LID to JID, and replace it in the text body
|
|
60
|
+
for (const rawMention of rawMentions) {
|
|
61
|
+
// If the mention is an LID (e.g., '81419946750204@lid'), convert it to standard JID
|
|
62
|
+
const resolvedJid = rawMention.endsWith('@lid')
|
|
63
|
+
? await lidToJid(sock, rawMention)
|
|
64
|
+
: rawMention;
|
|
65
|
+
|
|
66
|
+
cleanMentions.push(resolvedJid);
|
|
67
|
+
|
|
68
|
+
// Get the raw numeric part of both the target LID and target Phone Number
|
|
69
|
+
const lidNumber = rawMention.split('@')[0];
|
|
70
|
+
const phoneNumber = resolvedJid.split('@')[0];
|
|
71
|
+
|
|
72
|
+
// Create a precise regex to replace only this specific LID mention in the text
|
|
73
|
+
const preciseLidRegex = new RegExp(`@${lidNumber}`, 'g');
|
|
74
|
+
text = text.replace(preciseLidRegex, `@${phoneNumber}`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
text: text,
|
|
79
|
+
mentions: cleanMentions
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Formatting structure to resolve real JIDs for both the message sender and target mentions.
|
|
85
|
+
*/
|
|
41
86
|
export async function formatMessageWithJid(bot, msget) {
|
|
42
87
|
try {
|
|
43
88
|
const rawSender = msget.key.participant || msget.key.remoteJid;
|
|
44
|
-
let
|
|
89
|
+
let originalJid = '';
|
|
45
90
|
|
|
46
91
|
if (msget.key.remoteJidAlt && msget.key.remoteJidAlt.endsWith("@s.whatsapp.net")) {
|
|
47
|
-
|
|
92
|
+
originalJid = msget.key.remoteJidAlt;
|
|
48
93
|
} else {
|
|
49
|
-
|
|
94
|
+
originalJid = await lidToJid(bot, rawSender);
|
|
50
95
|
}
|
|
51
96
|
|
|
97
|
+
// Execute group mention mapping conversions (Passing 'bot' instance to resolve LIDs)
|
|
98
|
+
const { text: cleanText, mentions } = await convertGroupMention(bot, msget);
|
|
99
|
+
|
|
52
100
|
return {
|
|
53
101
|
...msget,
|
|
54
102
|
chat: msget.key.remoteJid,
|
|
55
103
|
sender: rawSender,
|
|
56
|
-
jidUser:
|
|
104
|
+
jidUser: originalJid,
|
|
105
|
+
body: cleanText, // Now properly updates even if m.body was originally empty
|
|
106
|
+
mentions: mentions // Holds an array of standard resolved @s.whatsapp.net JIDs
|
|
57
107
|
};
|
|
58
108
|
} catch (error) {
|
|
59
|
-
console.error(error);
|
|
109
|
+
console.error('[formatMessageWithJid Error]:', error);
|
|
60
110
|
return {
|
|
61
111
|
...msget,
|
|
62
112
|
chat: msget.key.remoteJid,
|
|
63
113
|
sender: msget.key.participant || msget.key.remoteJid,
|
|
64
|
-
jidUser: msget.key.participant || msget.key.remoteJid
|
|
114
|
+
jidUser: msget.key.participant || msget.key.remoteJid,
|
|
115
|
+
mentions: []
|
|
65
116
|
};
|
|
66
117
|
}
|
|
67
118
|
}
|