feat: added group integration to chatwoot

This commit is contained in:
Davidson Gomes 2023-07-13 12:32:10 -03:00
parent e64926a429
commit 86985231ff

View File

@ -120,6 +120,7 @@ export class ChatwootService {
instance: InstanceDto, instance: InstanceDto,
phoneNumber: string, phoneNumber: string,
inboxId: number, inboxId: number,
isGroup: boolean,
name?: string, name?: string,
) { ) {
const client = await this.clientCw(instance); const client = await this.clientCw(instance);
@ -128,13 +129,23 @@ export class ChatwootService {
throw new Error('client not found'); throw new Error('client not found');
} }
const contact = await client.contacts.create({ let data: any = {};
accountId: this.provider.account_id, if (!isGroup) {
data: { data = {
inbox_id: inboxId, inbox_id: inboxId,
name: name || phoneNumber, name: name || phoneNumber,
phone_number: `+${phoneNumber}`, phone_number: `+${phoneNumber}`,
}, };
} else {
data = {
inbox_id: inboxId,
name: name || phoneNumber,
identifier: phoneNumber,
};
}
const contact = await client.contacts.create({
accountId: this.provider.account_id,
data,
}); });
if (!contact) { if (!contact) {
@ -171,29 +182,73 @@ export class ChatwootService {
throw new Error('client not found'); throw new Error('client not found');
} }
const contact = await client.contacts.search({ let query: any;
if (!phoneNumber.includes('@g.us')) {
query = `+${phoneNumber}`;
} else {
query = phoneNumber;
}
const contact: any = await client.contacts.search({
accountId: this.provider.account_id, accountId: this.provider.account_id,
q: `+${phoneNumber}`, q: query,
}); });
return contact.payload.find((contact) => contact.phone_number === `+${phoneNumber}`); if (!phoneNumber.includes('@g.us')) {
return contact.payload.find((contact) => contact.phone_number === query);
} else {
return contact.payload.find((contact) => contact.identifier === query);
}
} }
public async createConversation(instance: InstanceDto, body: any) { public async createConversation(instance: InstanceDto, body: any) {
try {
const client = await this.clientCw(instance); const client = await this.clientCw(instance);
if (!client) { if (!client) {
throw new Error('client not found'); throw new Error('client not found');
} }
const chatId = body.key.remoteJid.split('@')[0]; const isGroup = body.key.remoteJid.includes('@g.us');
const nameContact = !body.key.fromMe ? body.pushName : chatId;
const chatId = isGroup ? body.key.remoteJid : body.key.remoteJid.split('@')[0];
let nameContact: string;
nameContact = !body.key.fromMe ? body.pushName : chatId;
const filterInbox = await this.getInbox(instance); const filterInbox = await this.getInbox(instance);
if (isGroup) {
const group = await this.waMonitor.waInstances[
instance.instanceName
].client.groupMetadata(chatId);
nameContact = `${group.subject} (GROUP)`;
const participant =
(await this.findContact(instance, body.key.participant.split('@')[0])) ||
((await this.createContact(
instance,
body.key.participant.split('@')[0],
filterInbox.id,
false,
body.pushName || body.key.participant.split('@')[0],
)) as any);
console.log('participant', participant);
}
const contact = const contact =
(await this.findContact(instance, chatId)) || (await this.findContact(instance, chatId)) ||
((await this.createContact(instance, chatId, filterInbox.id, nameContact)) as any); ((await this.createContact(
instance,
chatId,
filterInbox.id,
isGroup,
nameContact,
)) as any);
const contactId = contact.id || contact.payload.contact.id; const contactId = contact.id || contact.payload.contact.id;
@ -227,6 +282,9 @@ export class ChatwootService {
}); });
return conversation.id; return conversation.id;
} catch (error) {
console.log(error);
}
} }
public async getInbox(instance: InstanceDto) { public async getInbox(instance: InstanceDto) {
@ -477,7 +535,9 @@ export class ChatwootService {
if (!body?.conversation || body.private) return { message: 'bot' }; if (!body?.conversation || body.private) return { message: 'bot' };
const chatId = body.conversation.meta.sender.phone_number.replace('+', ''); const chatId =
body.conversation.meta.sender?.phone_number?.replace('+', '') ||
body.conversation.meta.sender?.identifier;
const messageReceived = body.content; const messageReceived = body.content;
const senderName = body?.sender?.name; const senderName = body?.sender?.name;
const waInstance = this.waMonitor.waInstances[instance.instanceName]; const waInstance = this.waMonitor.waInstances[instance.instanceName];
@ -699,9 +759,43 @@ export class ChatwootService {
writeFileSync(fileName, fileData, 'utf8'); writeFileSync(fileName, fileData, 'utf8');
if (body.key.remoteJid.includes('@g.us')) {
const participantName = body.pushName;
const content = `**${participantName}**\n\n${bodyMessage}`;
return await this.sendData(getConversion, fileName, messageType, content);
} else {
return await this.sendData(getConversion, fileName, messageType, bodyMessage); return await this.sendData(getConversion, fileName, messageType, bodyMessage);
} }
}
if (body.key.remoteJid.includes('@g.us')) {
const participantName = body.pushName;
const content = `**${participantName}**\n\n${bodyMessage}`;
const send = await this.createMessage(
instance,
getConversion,
content,
messageType,
);
this.messageCacheFile = path.join(
ROOT_DIR,
'store',
'chatwoot',
`${instance.instanceName}_cache.txt`,
);
this.messageCache = this.loadMessageCache();
this.messageCache.add(send.id.toString());
this.saveMessageCache();
return send;
} else {
const send = await this.createMessage( const send = await this.createMessage(
instance, instance,
getConversion, getConversion,
@ -724,6 +818,7 @@ export class ChatwootService {
return send; return send;
} }
}
if (event === 'status.instance') { if (event === 'status.instance') {
const data = body; const data = body;