@@ -2,6 +2,7 @@ import ChatwootClient from '@figuro/chatwoot-sdk';
22import axios from 'axios' ;
33import FormData from 'form-data' ;
44import { createReadStream , readFileSync , unlinkSync , writeFileSync } from 'fs' ;
5+ import Jimp from 'jimp' ;
56import mimeTypes from 'mime-types' ;
67import path from 'path' ;
78
@@ -998,7 +999,7 @@ export class ChatwootService {
998999
9991000 public async receiveWebhook ( instance : InstanceDto , body : any ) {
10001001 try {
1001- // espera 500ms para evitar duplicidade de mensagens
1002+ // espera 500ms para evitar duplicidade de mensagens
10021003 await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
10031004
10041005 this . logger . verbose ( 'receive webhook to chatwoot instance: ' + instance . instanceName ) ;
@@ -1179,6 +1180,20 @@ export class ChatwootService {
11791180 return result ;
11801181 }
11811182
1183+ private getAdsMessage ( msg : any ) {
1184+ interface AdsMessage {
1185+ title : string ;
1186+ body : string ;
1187+ thumbnailUrl : string ;
1188+ sourceUrl : string ;
1189+ }
1190+ let adsMessage : AdsMessage | undefined = msg . extendedTextMessage ?. contextInfo . externalAdReply ;
1191+
1192+ this . logger . verbose ( 'Get ads message if it exist' ) ;
1193+ adsMessage && this . logger . verbose ( 'Ads message: ' + adsMessage ) ;
1194+ return adsMessage ;
1195+ }
1196+
11821197 private getTypeMessage ( msg : any ) {
11831198 this . logger . verbose ( 'get type message' ) ;
11841199
@@ -1378,15 +1393,17 @@ export class ChatwootService {
13781393
13791394 const isMedia = this . isMediaMessage ( body . message ) ;
13801395
1396+ const adsMessage = this . getAdsMessage ( body . message ) ;
1397+
13811398 if ( ! bodyMessage && ! isMedia ) {
13821399 this . logger . warn ( 'no body message found' ) ;
13831400 return ;
13841401 }
13851402
13861403 this . logger . verbose ( 'get conversation in chatwoot' ) ;
1387- const getConversion = await this . createConversation ( instance , body ) ;
1404+ const getConversation = await this . createConversation ( instance , body ) ;
13881405
1389- if ( ! getConversion ) {
1406+ if ( ! getConversation ) {
13901407 this . logger . warn ( 'conversation not found' ) ;
13911408 return ;
13921409 }
@@ -1439,7 +1456,7 @@ export class ChatwootService {
14391456 }
14401457
14411458 this . logger . verbose ( 'send data to chatwoot' ) ;
1442- const send = await this . sendData ( getConversion , fileName , messageType , content , source_id , source_reply_id ) ;
1459+ const send = await this . sendData ( getConversation , fileName , messageType , content , source_id , source_reply_id ) ;
14431460
14441461 if ( ! send ) {
14451462 this . logger . warn ( 'message not sent' ) ;
@@ -1461,7 +1478,7 @@ export class ChatwootService {
14611478
14621479 this . logger . verbose ( 'send data to chatwoot' ) ;
14631480 const send = await this . sendData (
1464- getConversion ,
1481+ getConversation ,
14651482 fileName ,
14661483 messageType ,
14671484 bodyMessage ,
@@ -1487,6 +1504,67 @@ export class ChatwootService {
14871504 }
14881505 }
14891506
1507+ this . logger . verbose ( 'check if has Ads Message' ) ;
1508+ if ( adsMessage ) {
1509+ this . logger . verbose ( 'message is from Ads' ) ;
1510+
1511+ this . logger . verbose ( 'get base64 from media ads message' ) ;
1512+ const imgBuffer = await axios . get ( adsMessage . thumbnailUrl , { responseType : 'arraybuffer' } ) ;
1513+
1514+ const extension = mimeTypes . extension ( imgBuffer . headers [ 'content-type' ] ) ;
1515+ const mimeType = extension && mimeTypes . lookup ( extension ) ;
1516+
1517+ if ( ! mimeType ) {
1518+ this . logger . warn ( 'mimetype of Ads message not found' ) ;
1519+ return ;
1520+ }
1521+
1522+ const random = Math . random ( ) . toString ( 36 ) . substring ( 7 ) ;
1523+ const nameFile = `${ random } .${ mimeTypes . extension ( mimeType ) } ` ;
1524+ const fileData = Buffer . from ( imgBuffer . data , 'binary' ) ;
1525+ const fileName = `${ path . join ( waInstance ?. storePath , 'temp' , `${ nameFile } ` ) } ` ;
1526+
1527+ this . logger . verbose ( 'temp file name: ' + nameFile ) ;
1528+ this . logger . verbose ( 'create temp file' ) ;
1529+ await Jimp . read ( fileData )
1530+ . then ( async ( img ) => {
1531+ await img . cover ( 320 , 180 ) . writeAsync ( fileName ) ;
1532+ } )
1533+ . catch ( ( err ) => {
1534+ this . logger . error ( `image is not write: ${ err } ` ) ;
1535+ } ) ;
1536+ const truncStr = ( str : string , len : number ) => {
1537+ return str . length > len ? str . substring ( 0 , len ) + '...' : str ;
1538+ } ;
1539+
1540+ const title = truncStr ( adsMessage . title , 40 ) ;
1541+ const description = truncStr ( adsMessage . body , 75 ) ;
1542+
1543+ this . logger . verbose ( 'send data to chatwoot' ) ;
1544+ const send = await this . sendData (
1545+ getConversation ,
1546+ fileName ,
1547+ messageType ,
1548+ `${ bodyMessage } \n\n\n**${ title } **\n${ description } \n${ adsMessage . sourceUrl } ` ,
1549+ ) ;
1550+
1551+ if ( ! send ) {
1552+ this . logger . warn ( 'message not sent' ) ;
1553+ return ;
1554+ }
1555+
1556+ this . messageCacheFile = path . join ( ROOT_DIR , 'store' , 'chatwoot' , `${ instance . instanceName } _cache.txt` ) ;
1557+
1558+ this . messageCache = this . loadMessageCache ( ) ;
1559+
1560+ this . messageCache . add ( send . id . toString ( ) ) ;
1561+
1562+ this . logger . verbose ( 'save message cache' ) ;
1563+ this . saveMessageCache ( ) ;
1564+
1565+ return send ;
1566+ }
1567+
14901568 this . logger . verbose ( 'check if is group' ) ;
14911569 if ( body . key . remoteJid . includes ( '@g.us' ) ) {
14921570 this . logger . verbose ( 'message is group' ) ;
@@ -1505,7 +1583,7 @@ export class ChatwootService {
15051583 this . logger . verbose ( 'send data to chatwoot' ) ;
15061584 const send = await this . createMessage (
15071585 instance ,
1508- getConversion ,
1586+ getConversation ,
15091587 content ,
15101588 messageType ,
15111589 source_id ,
@@ -1533,7 +1611,7 @@ export class ChatwootService {
15331611 this . logger . verbose ( 'send data to chatwoot' ) ;
15341612 const send = await this . createMessage (
15351613 instance ,
1536- getConversion ,
1614+ getConversation ,
15371615 bodyMessage ,
15381616 messageType ,
15391617 source_id ,
@@ -1575,14 +1653,14 @@ export class ChatwootService {
15751653 }
15761654
15771655 // if (event === 'connection.update') {
1578- // this.logger.verbose('event connection.update');
1656+ // this.logger.verbose('event connection.update');
15791657
1580- // if (body.status === 'open') {
1581- // const msgConnection = `🚀 Connection successfully established!`;
1658+ // if (body.status === 'open') {
1659+ // const msgConnection = `🚀 Connection successfully established!`;
15821660
1583- // this.logger.verbose('send message to chatwoot');
1584- // await this.createBotMessage(instance, msgConnection, 'incoming');
1585- // }
1661+ // this.logger.verbose('send message to chatwoot');
1662+ // await this.createBotMessage(instance, msgConnection, 'incoming');
1663+ // }
15861664 // }
15871665
15881666 if ( event === 'qrcode.updated' ) {
0 commit comments