Skip to content

Commit

Permalink
feat: Added WPP.chat.sendVCardContact function
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Nov 15, 2021
1 parent 09ce7e3 commit 01c9fbe
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/chat/Chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
ChatStore,
ClockSkew,
Constants,
ContactModel,
ContactStore,
Features,
MediaPrep,
MsgKey,
Expand All @@ -36,6 +38,8 @@ import {
OpaqueData,
ReplyButtonModel,
UserPrefs,
VCard,
VCardData,
Wid,
} from '../whatsapp';
import { SendMsgResult } from '../whatsapp/enums';
Expand All @@ -54,6 +58,7 @@ import {
DocumentMessageOptions,
ImageMessageOptions,
SendMessageReturn,
VCardContact,
VideoMessageOptions,
} from '.';
import {
Expand Down Expand Up @@ -987,4 +992,64 @@ export class Chat extends Emittery<ChatEventTypes> {
sendMsgResult,
};
}

async sendVCardContact(
chatId: any,
contacts: string | Wid | VCardContact | (string | Wid | VCardContact)[],
options: SendMessageOptions = {}
): Promise<SendMessageReturn> {
options = {
...this.defaultSendMessageOptions,
...options,
};

if (!Array.isArray(contacts)) {
contacts = [contacts];
}

const vcards: VCardData[] = [];

for (const contact of contacts) {
let id = '';
let name = '';

if (typeof contact === 'object' && 'name' in contact) {
id = contact.id.toString();
name = contact.name;
} else {
id = contact.toString();
}

let contactModel = ContactStore.get(id);
if (!contactModel) {
contactModel = new ContactModel({
id: assertWid(id),
name,
});
}

if (name) {
// Create a clone
contactModel = new ContactModel(contactModel.attributes);
contactModel.name = name;
Object.defineProperty(contactModel, 'formattedName', { value: name });
Object.defineProperty(contactModel, 'displayName', { value: name });
}

vcards.push(VCard.vcardFromContactModel(contactModel));
}

const message: RawMessage = {};

if (vcards.length === 1) {
message.type = 'vcard';
message.body = vcards[0].vcard;
message.vcardFormattedName = vcards[0].displayName;
} else {
message.type = 'multi_vcard';
message.vcardList = vcards;
}

return this.sendRawMessage(chatId, message, options);
}
}
5 changes: 5 additions & 0 deletions src/chat/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ export interface VideoMessageOptions
isGif?: boolean;
}

export interface VCardContact {
id: string | Wid;
name: string;
}

export type AllMessageOptions = SendMessageOptions &
MessageButtonsOptions &
Partial<ListMessageOptions>;
Expand Down
32 changes: 32 additions & 0 deletions src/whatsapp/misc/VCard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*!
* Copyright 2021 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { ContactModel } from '..';
import { exportModule } from '../exportModule';

export interface VCardData {
displayName: string;
vcard: string;
isMultiVcard: false;
}

/** @whatsapp 2.2144.10:36117 */
export declare namespace VCard {
function vcardFromContactModel(contact: ContactModel): VCardData;
function mergeVcards(vcards: VCardData[]): VCardData;
}

exportModule(exports, 'VCard', (m) => m.vcardFromContactModel);
1 change: 1 addition & 0 deletions src/whatsapp/misc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ export * from './MsgLoadState';
export * from './OpaqueData';
export * from './State';
export * from './UserPrefs';
export * from './VCard';
export * from './Wid';
export * from './WidFactory';
1 change: 1 addition & 0 deletions src/whatsapp/models/ContactModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {

interface Props {
id: Wid;
name?: any;
shortName?: any;
pushname?: any;
type?: any;
Expand Down

0 comments on commit 01c9fbe

Please sign in to comment.