@@ -28,6 +28,8 @@ import {
28
28
ChatStore ,
29
29
ClockSkew ,
30
30
Constants ,
31
+ ContactModel ,
32
+ ContactStore ,
31
33
Features ,
32
34
MediaPrep ,
33
35
MsgKey ,
@@ -36,6 +38,8 @@ import {
36
38
OpaqueData ,
37
39
ReplyButtonModel ,
38
40
UserPrefs ,
41
+ VCard ,
42
+ VCardData ,
39
43
Wid ,
40
44
} from '../whatsapp' ;
41
45
import { SendMsgResult } from '../whatsapp/enums' ;
@@ -54,6 +58,7 @@ import {
54
58
DocumentMessageOptions ,
55
59
ImageMessageOptions ,
56
60
SendMessageReturn ,
61
+ VCardContact ,
57
62
VideoMessageOptions ,
58
63
} from '.' ;
59
64
import {
@@ -987,4 +992,64 @@ export class Chat extends Emittery<ChatEventTypes> {
987
992
sendMsgResult,
988
993
} ;
989
994
}
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
+ }
990
1055
}
0 commit comments