diff --git a/CHANGELOG.md b/CHANGELOG.md index d6c7853c..ae69826a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ * Fixed bug that saved contacts from groups came without number in chatwoot * Fixed problem to receive csat in chatwoot * Fixed require fileName for document only in base64 for send media message +* Bug fix when sending mobile message change contact name to number in chatwoot # 1.2.2 (2023-07-15 09:36) diff --git a/package.json b/package.json index 1e577870..5c182838 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "@ffmpeg-installer/ffmpeg": "^1.1.0", "@figuro/chatwoot-sdk": "^1.1.14", "@hapi/boom": "^10.0.1", + "@sentry/node": "^7.59.2", "@whiskeysockets/baileys": "^6.4.0", "axios": "^1.3.5", "class-validator": "^0.13.2", diff --git a/src/main.ts b/src/main.ts index 7d16b5c6..ac66e7b5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -10,6 +10,7 @@ import { waMonitor } from './whatsapp/whatsapp.module'; import { HttpStatus, router } from './whatsapp/routers/index.router'; import 'express-async-errors'; import { ServerUP } from './utils/server-up'; +import * as Sentry from '@sentry/node'; function initWA() { waMonitor.loadInstance(); @@ -19,6 +20,27 @@ function bootstrap() { const logger = new Logger('SERVER'); const app = express(); + // Sentry.init({ + // dsn: '', + // integrations: [ + // // enable HTTP calls tracing + // new Sentry.Integrations.Http({ tracing: true }), + // // enable Express.js middleware tracing + // new Sentry.Integrations.Express({ app }), + // // Automatically instrument Node.js libraries and frameworks + // ...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(), + // ], + + // // Set tracesSampleRate to 1.0 to capture 100% + // // of transactions for performance monitoring. + // // We recommend adjusting this value in production + // tracesSampleRate: 1.0, + // }); + + // app.use(Sentry.Handlers.requestHandler()); + + // app.use(Sentry.Handlers.tracingHandler()); + app.use( cors({ origin(requestOrigin, callback) { @@ -43,6 +65,13 @@ function bootstrap() { app.use('/', router); + // app.use(Sentry.Handlers.errorHandler()); + + // app.use(function onError(err, req, res, next) { + // res.statusCode = 500; + // res.end(res.sentry + '\n'); + // }); + app.use( (err: Error, req: Request, res: Response, next: NextFunction) => { if (err) { diff --git a/src/whatsapp/services/chatwoot.service.ts b/src/whatsapp/services/chatwoot.service.ts index 17ccf19b..d93ab3be 100644 --- a/src/whatsapp/services/chatwoot.service.ts +++ b/src/whatsapp/services/chatwoot.service.ts @@ -453,21 +453,24 @@ export class ChatwootService { const findContact = await this.findContact(instance, chatId); let contact: any; - - if (findContact) { - contact = await this.updateContact(instance, findContact.id, { - name: nameContact, - avatar_url: picture_url.profilePictureUrl || null, - }); + if (body.key.fromMe) { + contact = findContact; } else { - contact = await this.createContact( - instance, - chatId, - filterInbox.id, - isGroup, - nameContact, - picture_url.profilePictureUrl || null, - ); + if (findContact) { + contact = await this.updateContact(instance, findContact.id, { + name: nameContact, + avatar_url: picture_url.profilePictureUrl || null, + }); + } else { + contact = await this.createContact( + instance, + chatId, + filterInbox.id, + isGroup, + nameContact, + picture_url.profilePictureUrl || null, + ); + } } if (!contact) { @@ -475,7 +478,8 @@ export class ChatwootService { return null; } - const contactId = contact.payload.id || contact.payload.contact.id; + const contactId = + contact?.payload?.id || contact?.payload?.contact?.id || contact?.id; if (!body.key.fromMe && contact.name === chatId && nameContact !== chatId) { this.logger.verbose('update contact name in chatwoot'); @@ -987,13 +991,9 @@ export class ChatwootService { } if (command.includes('#inbox_whatsapp')) { - console.log('command include #inbox_whatsapp'); - const urlServer = this.configService.get('SERVER').URL; const apiKey = this.configService.get('AUTHENTICATION').API_KEY.KEY; - console.log('url server: ' + urlServer); - console.log('api key: ' + apiKey); const data = { instanceName: command.split(':')[1], qrcode: true, @@ -1014,9 +1014,7 @@ export class ChatwootService { data: data, }; - const { data: response } = await axios.request(config); - - console.log(response); + await axios.request(config); } } @@ -1101,7 +1099,6 @@ export class ChatwootService { body.content_type === 'input_csat' && body.event === 'message_created' ) { - console.log(body); this.logger.verbose('check if is csat'); const data: SendTextDto = { @@ -1161,6 +1158,7 @@ export class ChatwootService { documentWithCaptionMessage: msg.documentWithCaptionMessage?.message?.documentMessage?.caption, audioMessage: msg.audioMessage?.caption, + contactMessage: msg.contactMessage?.vcard, }; this.logger.verbose('type message: ' + types); @@ -1174,6 +1172,25 @@ export class ChatwootService { const result = typeKey ? types[typeKey] : undefined; + if (typeKey === 'contactMessage') { + const vCardData = result.split('\n'); + const contactInfo = {}; + + vCardData.forEach((line) => { + const [key, value] = line.split(':'); + if (key && value) { + contactInfo[key] = value; + } + }); + + const formattedContact = `**Contact:** + **name:** ${contactInfo['FN']} + **number:** ${contactInfo['item1.TEL;waid=5511952801378']}`; + + this.logger.verbose('message content: ' + formattedContact); + return formattedContact; + } + this.logger.verbose('message content: ' + result); return result; @@ -1184,8 +1201,12 @@ export class ChatwootService { const types = this.getTypeMessage(msg); + console.log('types', types); + const messageContent = this.getMessageContent(types); + console.log('messageContent', messageContent); + this.logger.verbose('conversation message: ' + messageContent); return messageContent;