Skip to content

Commit 0c2e3cb

Browse files
committed
feat: add convertNumber function for WhatsApp ID format conversion
- Convert between @lid and @c.us formats
1 parent 058be8c commit 0c2e3cb

File tree

10 files changed

+60
-1
lines changed

10 files changed

+60
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,10 @@ await client.screenshot();
601601
## Retrieving Data
602602

603603
```javascript
604+
605+
// This function converts a number from the @c.us format to @lid, or vice versa.
606+
const convert = await client.convertNumber('[email protected]' || '00000000000000@lid');
607+
604608
// return a chat
605609
const infoChat = await client.getChatById('[email protected]');
606610

src/webpack/api/layes/retriever.layer.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ export class RetrieverLayer extends Profile {
1313
super(page, browser, options, ev);
1414
}
1515

16+
/**
17+
* Convert contact number to WhatsApp format
18+
* @param number - Contact Number
19+
*/
20+
public async convertNumber(number: string) {
21+
return this.handleApiCallParametres(FunctionsLayer.convertNumber, number);
22+
}
23+
1624
/**
1725
* Get all messages in chat by chatId
1826
* @param chatId - Chat id

src/webpack/assets/api.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
refreshAltLinkingCode,
4343
getInterface,
4444
getChatById,
45+
convertNumber,
4546
} from './functions';
4647

4748
import {
@@ -105,7 +106,7 @@ if (typeof window.API === 'undefined') {
105106

106107
// Get Functions
107108
window.API.loadAndGetAllMessagesInChat = loadAndGetAllMessagesInChat;
108-
109+
window.API.convertNumber = convertNumber;
109110
// Send Functions
110111
window.API.sendMessage = sendMessage;
111112

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Function to convert a number from lid to contact or vice versa
3+
* @param {*} number - number to convert
4+
* @returns - converted number or undefined if not possible
5+
*/
6+
export const convertNumber = async (number) => {
7+
const lid = '@lid';
8+
const contact = '@c.us';
9+
10+
const chat = await API.sendExist(number);
11+
if (chat && chat.status != 404) {
12+
const chatWid = new Store.WidFactory.createWid(chat.id._serialized);
13+
if (lid === number.substr(-lid.length, lid.length)) {
14+
return Store.ApiContact.getPhoneNumber(chatWid);
15+
}
16+
if (contact === number.substr(-contact.length, contact.length)) {
17+
return Store.ApiContact.getCurrentLid(chatWid);
18+
}
19+
return undefined;
20+
} else {
21+
return chat;
22+
}
23+
};

src/webpack/assets/functions/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ export * from './code-for-phone-number';
2525
export * from './refresh-alt-linking-code';
2626
export * from './get-interface';
2727
export * from './get-chat-by-id';
28+
export * from './convert-number';

src/webpack/assets/help/filter-object.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,9 @@ export const filterObjects = [
180180
when: (module) =>
181181
module?.ProfilePicThumb && module?.ProfilePicThumbImpl ? module : null,
182182
},
183+
{
184+
id: 'ApiContact',
185+
conditions: (module) =>
186+
module?.getPhoneNumber && module?.getCurrentLid ? module : null,
187+
},
183188
];

src/webpack/model/api-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,6 @@ export interface API {
132132
eventOnReactionMessage(): Promise<void>;
133133
eventOnIntroReactionMessage(): Promise<void>;
134134
eventNewOnAck(): Promise<void>;
135+
136+
convertNumber(number: string): Promise<string>;
135137
}

src/webpack/model/enum/functions-layes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { API } from '../';
2+
23
export enum FunctionsLayer {
34
getGroupParticipant = 'getGroupParticipant',
45
getAllChatsGroups = 'getAllChatsGroups',
@@ -18,6 +19,7 @@ export enum FunctionsLayer {
1819
getInterface = 'getInterface',
1920
screenshot = 'screenshot',
2021
getChatById = 'getChatById',
22+
convertNumber = 'convertNumber',
2123

2224
// Events Listeners
2325
eventInterfaceChange = 'eventInterfaceChange',

test/dev/bot.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,18 @@ import fs from 'fs';
159159
});
160160
}
161161

162+
if (hydraBotTestFunctions.convertNumber) {
163+
// convert number to whatsapp format
164+
await client
165+
.convertNumber('[email protected]' || '00000000000000@lid')
166+
.then((result) => {
167+
console.log('Convert Number: ', result);
168+
})
169+
.catch((error) => {
170+
console.log('Error Convert Number: ', error);
171+
});
172+
}
173+
162174
if (hydraBotTestFunctions.getAllChats) {
163175
/// get all chats
164176
await client

test/help.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const hydraBotTestFunctions = {
2323
getGroupParticipant: false,
2424
loadAndGetAllMessagesInChat: false,
2525
getChatById: false,
26+
convertNumber: true,
2627

2728
// Navigation functions
2829
browserClose: false,

0 commit comments

Comments
 (0)