@@ -43,6 +43,12 @@ export class ChatwootService {
4343 this . logger . verbose ( 'message cache saved' ) ;
4444 }
4545
46+ private clearMessageCache ( ) {
47+ this . logger . verbose ( 'clear message cache' ) ;
48+ this . messageCache . clear ( ) ;
49+ this . saveMessageCache ( ) ;
50+ }
51+
4652 private async getProvider ( instance : InstanceDto ) {
4753 this . logger . verbose ( 'get provider to instance: ' + instance . instanceName ) ;
4854 try {
@@ -258,6 +264,7 @@ export class ChatwootService {
258264 inboxId : number ,
259265 isGroup : boolean ,
260266 name ?: string ,
267+ avatar_url ?: string ,
261268 ) {
262269 this . logger . verbose ( 'create contact to instance: ' + instance . instanceName ) ;
263270
@@ -275,13 +282,15 @@ export class ChatwootService {
275282 inbox_id : inboxId ,
276283 name : name || phoneNumber ,
277284 phone_number : `+${ phoneNumber } ` ,
285+ avatar_url : avatar_url ,
278286 } ;
279287 } else {
280288 this . logger . verbose ( 'create contact group in chatwoot' ) ;
281289 data = {
282290 inbox_id : inboxId ,
283291 name : name || phoneNumber ,
284292 identifier : phoneNumber ,
293+ avatar_url : avatar_url ,
285294 } ;
286295 }
287296
@@ -404,34 +413,67 @@ export class ChatwootService {
404413 nameContact = `${ group . subject } (GROUP)` ;
405414
406415 this . logger . verbose ( 'find or create participant in chatwoot' ) ;
407- const participant =
408- ( await this . findContact ( instance , body . key . participant . split ( '@' ) [ 0 ] ) ) ||
409- ( ( await this . createContact (
416+
417+ const picture_url = await this . waMonitor . waInstances [
418+ instance . instanceName
419+ ] . profilePicture ( body . key . participant . split ( '@' ) [ 0 ] ) ;
420+
421+ const findParticipant = await this . findContact (
422+ instance ,
423+ body . key . participant . split ( '@' ) [ 0 ] ,
424+ ) ;
425+
426+ if ( findParticipant ) {
427+ await this . updateContact ( instance , findParticipant . id , {
428+ name : nameContact ,
429+ avatar_url : picture_url . profilePictureUrl || null ,
430+ } ) ;
431+ } else {
432+ await this . createContact (
410433 instance ,
411434 body . key . participant . split ( '@' ) [ 0 ] ,
412435 filterInbox . id ,
413- false ,
414- body . pushName || body . key . participant . split ( '@' ) [ 0 ] ,
415- ) ) as any ) ;
436+ isGroup ,
437+ nameContact ,
438+ picture_url . profilePictureUrl || null ,
439+ ) ;
440+ }
416441 }
417442
418443 this . logger . verbose ( 'find or create contact in chatwoot' ) ;
419- const contact =
420- ( await this . findContact ( instance , chatId ) ) ||
421- ( ( await this . createContact (
444+
445+ const picture_url = await this . waMonitor . waInstances [
446+ instance . instanceName
447+ ] . profilePicture ( chatId ) ;
448+
449+ const findContact = await this . findContact ( instance , chatId ) ;
450+
451+ let contact : any ;
452+
453+ if ( findContact ) {
454+ contact = await this . updateContact ( instance , findContact . id , {
455+ name : nameContact ,
456+ avatar_url : picture_url . profilePictureUrl || null ,
457+ } ) ;
458+ } else {
459+ contact = await this . createContact (
422460 instance ,
423461 chatId ,
424462 filterInbox . id ,
425463 isGroup ,
426464 nameContact ,
427- ) ) as any ) ;
465+ picture_url . profilePictureUrl || null ,
466+ ) ;
467+ }
428468
429469 if ( ! contact ) {
430470 this . logger . warn ( 'contact not found' ) ;
431471 return null ;
432472 }
433473
434- const contactId = contact . id || contact . payload . contact . id ;
474+ console . log ( contact ) ;
475+
476+ const contactId = contact . payload . id || contact . payload . contact . id ;
435477
436478 if ( ! body . key . fromMe && contact . name === chatId && nameContact !== chatId ) {
437479 this . logger . verbose ( 'update contact name in chatwoot' ) ;
@@ -968,6 +1010,9 @@ export class ChatwootService {
9681010 return { message : 'bot' } ;
9691011 }
9701012
1013+ this . logger . verbose ( 'clear cache' ) ;
1014+ this . clearMessageCache ( ) ;
1015+
9711016 this . logger . verbose ( 'Format message to send' ) ;
9721017 let formatText : string ;
9731018 if ( senderName === null || senderName === undefined ) {
@@ -1124,6 +1169,7 @@ export class ChatwootService {
11241169 }
11251170
11261171 if ( event === 'messages.upsert' ) {
1172+ console . log ( body ) ;
11271173 this . logger . verbose ( 'event messages.upsert' ) ;
11281174
11291175 if ( body . key . remoteJid === 'status@broadcast' ) {
0 commit comments