Skip to content

Commit 0db8f72

Browse files
committed
feat: Send contact in chatwoot
1 parent c122606 commit 0db8f72

File tree

4 files changed

+75
-23
lines changed

4 files changed

+75
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* Fixed bug that saved contacts from groups came without number in chatwoot
2222
* Fixed problem to receive csat in chatwoot
2323
* Fixed require fileName for document only in base64 for send media message
24+
* Bug fix when sending mobile message change contact name to number in chatwoot
2425

2526
# 1.2.2 (2023-07-15 09:36)
2627

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"@ffmpeg-installer/ffmpeg": "^1.1.0",
4545
"@figuro/chatwoot-sdk": "^1.1.14",
4646
"@hapi/boom": "^10.0.1",
47+
"@sentry/node": "^7.59.2",
4748
"@whiskeysockets/baileys": "^6.4.0",
4849
"axios": "^1.3.5",
4950
"class-validator": "^0.13.2",

src/main.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { waMonitor } from './whatsapp/whatsapp.module';
1010
import { HttpStatus, router } from './whatsapp/routers/index.router';
1111
import 'express-async-errors';
1212
import { ServerUP } from './utils/server-up';
13+
import * as Sentry from '@sentry/node';
1314

1415
function initWA() {
1516
waMonitor.loadInstance();
@@ -19,6 +20,27 @@ function bootstrap() {
1920
const logger = new Logger('SERVER');
2021
const app = express();
2122

23+
// Sentry.init({
24+
// dsn: '',
25+
// integrations: [
26+
// // enable HTTP calls tracing
27+
// new Sentry.Integrations.Http({ tracing: true }),
28+
// // enable Express.js middleware tracing
29+
// new Sentry.Integrations.Express({ app }),
30+
// // Automatically instrument Node.js libraries and frameworks
31+
// ...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(),
32+
// ],
33+
34+
// // Set tracesSampleRate to 1.0 to capture 100%
35+
// // of transactions for performance monitoring.
36+
// // We recommend adjusting this value in production
37+
// tracesSampleRate: 1.0,
38+
// });
39+
40+
// app.use(Sentry.Handlers.requestHandler());
41+
42+
// app.use(Sentry.Handlers.tracingHandler());
43+
2244
app.use(
2345
cors({
2446
origin(requestOrigin, callback) {
@@ -43,6 +65,13 @@ function bootstrap() {
4365

4466
app.use('/', router);
4567

68+
// app.use(Sentry.Handlers.errorHandler());
69+
70+
// app.use(function onError(err, req, res, next) {
71+
// res.statusCode = 500;
72+
// res.end(res.sentry + '\n');
73+
// });
74+
4675
app.use(
4776
(err: Error, req: Request, res: Response, next: NextFunction) => {
4877
if (err) {

src/whatsapp/services/chatwoot.service.ts

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -453,29 +453,33 @@ export class ChatwootService {
453453
const findContact = await this.findContact(instance, chatId);
454454

455455
let contact: any;
456-
457-
if (findContact) {
458-
contact = await this.updateContact(instance, findContact.id, {
459-
name: nameContact,
460-
avatar_url: picture_url.profilePictureUrl || null,
461-
});
456+
if (body.key.fromMe) {
457+
contact = findContact;
462458
} else {
463-
contact = await this.createContact(
464-
instance,
465-
chatId,
466-
filterInbox.id,
467-
isGroup,
468-
nameContact,
469-
picture_url.profilePictureUrl || null,
470-
);
459+
if (findContact) {
460+
contact = await this.updateContact(instance, findContact.id, {
461+
name: nameContact,
462+
avatar_url: picture_url.profilePictureUrl || null,
463+
});
464+
} else {
465+
contact = await this.createContact(
466+
instance,
467+
chatId,
468+
filterInbox.id,
469+
isGroup,
470+
nameContact,
471+
picture_url.profilePictureUrl || null,
472+
);
473+
}
471474
}
472475

473476
if (!contact) {
474477
this.logger.warn('contact not found');
475478
return null;
476479
}
477480

478-
const contactId = contact.payload.id || contact.payload.contact.id;
481+
const contactId =
482+
contact?.payload?.id || contact?.payload?.contact?.id || contact?.id;
479483

480484
if (!body.key.fromMe && contact.name === chatId && nameContact !== chatId) {
481485
this.logger.verbose('update contact name in chatwoot');
@@ -987,13 +991,9 @@ export class ChatwootService {
987991
}
988992

989993
if (command.includes('#inbox_whatsapp')) {
990-
console.log('command include #inbox_whatsapp');
991-
992994
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
993995
const apiKey = this.configService.get('AUTHENTICATION').API_KEY.KEY;
994996

995-
console.log('url server: ' + urlServer);
996-
console.log('api key: ' + apiKey);
997997
const data = {
998998
instanceName: command.split(':')[1],
999999
qrcode: true,
@@ -1014,9 +1014,7 @@ export class ChatwootService {
10141014
data: data,
10151015
};
10161016

1017-
const { data: response } = await axios.request(config);
1018-
1019-
console.log(response);
1017+
await axios.request(config);
10201018
}
10211019
}
10221020

@@ -1101,7 +1099,6 @@ export class ChatwootService {
11011099
body.content_type === 'input_csat' &&
11021100
body.event === 'message_created'
11031101
) {
1104-
console.log(body);
11051102
this.logger.verbose('check if is csat');
11061103

11071104
const data: SendTextDto = {
@@ -1161,6 +1158,7 @@ export class ChatwootService {
11611158
documentWithCaptionMessage:
11621159
msg.documentWithCaptionMessage?.message?.documentMessage?.caption,
11631160
audioMessage: msg.audioMessage?.caption,
1161+
contactMessage: msg.contactMessage?.vcard,
11641162
};
11651163

11661164
this.logger.verbose('type message: ' + types);
@@ -1174,6 +1172,25 @@ export class ChatwootService {
11741172

11751173
const result = typeKey ? types[typeKey] : undefined;
11761174

1175+
if (typeKey === 'contactMessage') {
1176+
const vCardData = result.split('\n');
1177+
const contactInfo = {};
1178+
1179+
vCardData.forEach((line) => {
1180+
const [key, value] = line.split(':');
1181+
if (key && value) {
1182+
contactInfo[key] = value;
1183+
}
1184+
});
1185+
1186+
const formattedContact = `**Contact:**
1187+
**name:** ${contactInfo['FN']}
1188+
**number:** ${contactInfo['item1.TEL;waid=5511952801378']}`;
1189+
1190+
this.logger.verbose('message content: ' + formattedContact);
1191+
return formattedContact;
1192+
}
1193+
11771194
this.logger.verbose('message content: ' + result);
11781195

11791196
return result;
@@ -1184,8 +1201,12 @@ export class ChatwootService {
11841201

11851202
const types = this.getTypeMessage(msg);
11861203

1204+
console.log('types', types);
1205+
11871206
const messageContent = this.getMessageContent(types);
11881207

1208+
console.log('messageContent', messageContent);
1209+
11891210
this.logger.verbose('conversation message: ' + messageContent);
11901211

11911212
return messageContent;

0 commit comments

Comments
 (0)