Skip to content

Commit 01c9fbe

Browse files
committed
feat: Added WPP.chat.sendVCardContact function
1 parent 09ce7e3 commit 01c9fbe

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

src/chat/Chat.ts

+65
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import {
2828
ChatStore,
2929
ClockSkew,
3030
Constants,
31+
ContactModel,
32+
ContactStore,
3133
Features,
3234
MediaPrep,
3335
MsgKey,
@@ -36,6 +38,8 @@ import {
3638
OpaqueData,
3739
ReplyButtonModel,
3840
UserPrefs,
41+
VCard,
42+
VCardData,
3943
Wid,
4044
} from '../whatsapp';
4145
import { SendMsgResult } from '../whatsapp/enums';
@@ -54,6 +58,7 @@ import {
5458
DocumentMessageOptions,
5559
ImageMessageOptions,
5660
SendMessageReturn,
61+
VCardContact,
5762
VideoMessageOptions,
5863
} from '.';
5964
import {
@@ -987,4 +992,64 @@ export class Chat extends Emittery<ChatEventTypes> {
987992
sendMsgResult,
988993
};
989994
}
995+
996+
async sendVCardContact(
997+
chatId: any,
998+
contacts: string | Wid | VCardContact | (string | Wid | VCardContact)[],
999+
options: SendMessageOptions = {}
1000+
): Promise<SendMessageReturn> {
1001+
options = {
1002+
...this.defaultSendMessageOptions,
1003+
...options,
1004+
};
1005+
1006+
if (!Array.isArray(contacts)) {
1007+
contacts = [contacts];
1008+
}
1009+
1010+
const vcards: VCardData[] = [];
1011+
1012+
for (const contact of contacts) {
1013+
let id = '';
1014+
let name = '';
1015+
1016+
if (typeof contact === 'object' && 'name' in contact) {
1017+
id = contact.id.toString();
1018+
name = contact.name;
1019+
} else {
1020+
id = contact.toString();
1021+
}
1022+
1023+
let contactModel = ContactStore.get(id);
1024+
if (!contactModel) {
1025+
contactModel = new ContactModel({
1026+
id: assertWid(id),
1027+
name,
1028+
});
1029+
}
1030+
1031+
if (name) {
1032+
// Create a clone
1033+
contactModel = new ContactModel(contactModel.attributes);
1034+
contactModel.name = name;
1035+
Object.defineProperty(contactModel, 'formattedName', { value: name });
1036+
Object.defineProperty(contactModel, 'displayName', { value: name });
1037+
}
1038+
1039+
vcards.push(VCard.vcardFromContactModel(contactModel));
1040+
}
1041+
1042+
const message: RawMessage = {};
1043+
1044+
if (vcards.length === 1) {
1045+
message.type = 'vcard';
1046+
message.body = vcards[0].vcard;
1047+
message.vcardFormattedName = vcards[0].displayName;
1048+
} else {
1049+
message.type = 'multi_vcard';
1050+
message.vcardList = vcards;
1051+
}
1052+
1053+
return this.sendRawMessage(chatId, message, options);
1054+
}
9901055
}

src/chat/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ export interface VideoMessageOptions
192192
isGif?: boolean;
193193
}
194194

195+
export interface VCardContact {
196+
id: string | Wid;
197+
name: string;
198+
}
199+
195200
export type AllMessageOptions = SendMessageOptions &
196201
MessageButtonsOptions &
197202
Partial<ListMessageOptions>;

src/whatsapp/misc/VCard.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*!
2+
* Copyright 2021 WPPConnect Team
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { ContactModel } from '..';
18+
import { exportModule } from '../exportModule';
19+
20+
export interface VCardData {
21+
displayName: string;
22+
vcard: string;
23+
isMultiVcard: false;
24+
}
25+
26+
/** @whatsapp 2.2144.10:36117 */
27+
export declare namespace VCard {
28+
function vcardFromContactModel(contact: ContactModel): VCardData;
29+
function mergeVcards(vcards: VCardData[]): VCardData;
30+
}
31+
32+
exportModule(exports, 'VCard', (m) => m.vcardFromContactModel);

src/whatsapp/misc/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ export * from './MsgLoadState';
3333
export * from './OpaqueData';
3434
export * from './State';
3535
export * from './UserPrefs';
36+
export * from './VCard';
3637
export * from './Wid';
3738
export * from './WidFactory';

src/whatsapp/models/ContactModel.ts

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626

2727
interface Props {
2828
id: Wid;
29+
name?: any;
2930
shortName?: any;
3031
pushname?: any;
3132
type?: any;

0 commit comments

Comments
 (0)