Skip to content

Commit 8698523

Browse files
committed
feat: added group integration to chatwoot
1 parent e64926a commit 8698523

File tree

1 file changed

+156
-61
lines changed

1 file changed

+156
-61
lines changed

src/whatsapp/services/chatwoot.service.ts

Lines changed: 156 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export class ChatwootService {
120120
instance: InstanceDto,
121121
phoneNumber: string,
122122
inboxId: number,
123+
isGroup: boolean,
123124
name?: string,
124125
) {
125126
const client = await this.clientCw(instance);
@@ -128,13 +129,23 @@ export class ChatwootService {
128129
throw new Error('client not found');
129130
}
130131

131-
const contact = await client.contacts.create({
132-
accountId: this.provider.account_id,
133-
data: {
132+
let data: any = {};
133+
if (!isGroup) {
134+
data = {
134135
inbox_id: inboxId,
135136
name: name || phoneNumber,
136137
phone_number: `+${phoneNumber}`,
137-
},
138+
};
139+
} else {
140+
data = {
141+
inbox_id: inboxId,
142+
name: name || phoneNumber,
143+
identifier: phoneNumber,
144+
};
145+
}
146+
const contact = await client.contacts.create({
147+
accountId: this.provider.account_id,
148+
data,
138149
});
139150

140151
if (!contact) {
@@ -171,62 +182,109 @@ export class ChatwootService {
171182
throw new Error('client not found');
172183
}
173184

174-
const contact = await client.contacts.search({
185+
let query: any;
186+
187+
if (!phoneNumber.includes('@g.us')) {
188+
query = `+${phoneNumber}`;
189+
} else {
190+
query = phoneNumber;
191+
}
192+
193+
const contact: any = await client.contacts.search({
175194
accountId: this.provider.account_id,
176-
q: `+${phoneNumber}`,
195+
q: query,
177196
});
178197

179-
return contact.payload.find((contact) => contact.phone_number === `+${phoneNumber}`);
198+
if (!phoneNumber.includes('@g.us')) {
199+
return contact.payload.find((contact) => contact.phone_number === query);
200+
} else {
201+
return contact.payload.find((contact) => contact.identifier === query);
202+
}
180203
}
181204

182205
public async createConversation(instance: InstanceDto, body: any) {
183-
const client = await this.clientCw(instance);
206+
try {
207+
const client = await this.clientCw(instance);
184208

185-
if (!client) {
186-
throw new Error('client not found');
187-
}
209+
if (!client) {
210+
throw new Error('client not found');
211+
}
188212

189-
const chatId = body.key.remoteJid.split('@')[0];
190-
const nameContact = !body.key.fromMe ? body.pushName : chatId;
213+
const isGroup = body.key.remoteJid.includes('@g.us');
191214

192-
const filterInbox = await this.getInbox(instance);
215+
const chatId = isGroup ? body.key.remoteJid : body.key.remoteJid.split('@')[0];
193216

194-
const contact =
195-
(await this.findContact(instance, chatId)) ||
196-
((await this.createContact(instance, chatId, filterInbox.id, nameContact)) as any);
217+
let nameContact: string;
197218

198-
const contactId = contact.id || contact.payload.contact.id;
219+
nameContact = !body.key.fromMe ? body.pushName : chatId;
199220

200-
if (!body.key.fromMe && contact.name === chatId && nameContact !== chatId) {
201-
await this.updateContact(instance, contactId, {
202-
name: nameContact,
203-
});
204-
}
221+
const filterInbox = await this.getInbox(instance);
205222

206-
const contactConversations = (await client.contacts.listConversations({
207-
accountId: this.provider.account_id,
208-
id: contactId,
209-
})) as any;
223+
if (isGroup) {
224+
const group = await this.waMonitor.waInstances[
225+
instance.instanceName
226+
].client.groupMetadata(chatId);
210227

211-
if (contactConversations) {
212-
const conversation = contactConversations.payload.find(
213-
(conversation) =>
214-
conversation.status !== 'resolved' && conversation.inbox_id == filterInbox.id,
215-
);
216-
if (conversation) {
217-
return conversation.id;
228+
nameContact = `${group.subject} (GROUP)`;
229+
230+
const participant =
231+
(await this.findContact(instance, body.key.participant.split('@')[0])) ||
232+
((await this.createContact(
233+
instance,
234+
body.key.participant.split('@')[0],
235+
filterInbox.id,
236+
false,
237+
body.pushName || body.key.participant.split('@')[0],
238+
)) as any);
239+
240+
console.log('participant', participant);
218241
}
219-
}
220242

221-
const conversation = await client.conversations.create({
222-
accountId: this.provider.account_id,
223-
data: {
224-
contact_id: `${contactId}`,
225-
inbox_id: `${filterInbox.id}`,
226-
},
227-
});
243+
const contact =
244+
(await this.findContact(instance, chatId)) ||
245+
((await this.createContact(
246+
instance,
247+
chatId,
248+
filterInbox.id,
249+
isGroup,
250+
nameContact,
251+
)) as any);
252+
253+
const contactId = contact.id || contact.payload.contact.id;
254+
255+
if (!body.key.fromMe && contact.name === chatId && nameContact !== chatId) {
256+
await this.updateContact(instance, contactId, {
257+
name: nameContact,
258+
});
259+
}
228260

229-
return conversation.id;
261+
const contactConversations = (await client.contacts.listConversations({
262+
accountId: this.provider.account_id,
263+
id: contactId,
264+
})) as any;
265+
266+
if (contactConversations) {
267+
const conversation = contactConversations.payload.find(
268+
(conversation) =>
269+
conversation.status !== 'resolved' && conversation.inbox_id == filterInbox.id,
270+
);
271+
if (conversation) {
272+
return conversation.id;
273+
}
274+
}
275+
276+
const conversation = await client.conversations.create({
277+
accountId: this.provider.account_id,
278+
data: {
279+
contact_id: `${contactId}`,
280+
inbox_id: `${filterInbox.id}`,
281+
},
282+
});
283+
284+
return conversation.id;
285+
} catch (error) {
286+
console.log(error);
287+
}
230288
}
231289

232290
public async getInbox(instance: InstanceDto) {
@@ -477,7 +535,9 @@ export class ChatwootService {
477535

478536
if (!body?.conversation || body.private) return { message: 'bot' };
479537

480-
const chatId = body.conversation.meta.sender.phone_number.replace('+', '');
538+
const chatId =
539+
body.conversation.meta.sender?.phone_number?.replace('+', '') ||
540+
body.conversation.meta.sender?.identifier;
481541
const messageReceived = body.content;
482542
const senderName = body?.sender?.name;
483543
const waInstance = this.waMonitor.waInstances[instance.instanceName];
@@ -699,30 +759,65 @@ export class ChatwootService {
699759

700760
writeFileSync(fileName, fileData, 'utf8');
701761

702-
return await this.sendData(getConversion, fileName, messageType, bodyMessage);
762+
if (body.key.remoteJid.includes('@g.us')) {
763+
const participantName = body.pushName;
764+
765+
const content = `**${participantName}**\n\n${bodyMessage}`;
766+
return await this.sendData(getConversion, fileName, messageType, content);
767+
} else {
768+
return await this.sendData(getConversion, fileName, messageType, bodyMessage);
769+
}
703770
}
704771

705-
const send = await this.createMessage(
706-
instance,
707-
getConversion,
708-
bodyMessage,
709-
messageType,
710-
);
772+
if (body.key.remoteJid.includes('@g.us')) {
773+
const participantName = body.pushName;
711774

712-
this.messageCacheFile = path.join(
713-
ROOT_DIR,
714-
'store',
715-
'chatwoot',
716-
`${instance.instanceName}_cache.txt`,
717-
);
775+
const content = `**${participantName}**\n\n${bodyMessage}`;
718776

719-
this.messageCache = this.loadMessageCache();
777+
const send = await this.createMessage(
778+
instance,
779+
getConversion,
780+
content,
781+
messageType,
782+
);
783+
784+
this.messageCacheFile = path.join(
785+
ROOT_DIR,
786+
'store',
787+
'chatwoot',
788+
`${instance.instanceName}_cache.txt`,
789+
);
790+
791+
this.messageCache = this.loadMessageCache();
792+
793+
this.messageCache.add(send.id.toString());
720794

721-
this.messageCache.add(send.id.toString());
795+
this.saveMessageCache();
722796

723-
this.saveMessageCache();
797+
return send;
798+
} else {
799+
const send = await this.createMessage(
800+
instance,
801+
getConversion,
802+
bodyMessage,
803+
messageType,
804+
);
805+
806+
this.messageCacheFile = path.join(
807+
ROOT_DIR,
808+
'store',
809+
'chatwoot',
810+
`${instance.instanceName}_cache.txt`,
811+
);
812+
813+
this.messageCache = this.loadMessageCache();
814+
815+
this.messageCache.add(send.id.toString());
724816

725-
return send;
817+
this.saveMessageCache();
818+
819+
return send;
820+
}
726821
}
727822

728823
if (event === 'status.instance') {

0 commit comments

Comments
 (0)