mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-23 17:08:44 -06:00
feat: added group integration to chatwoot
This commit is contained in:
parent
e64926a429
commit
86985231ff
@ -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,62 +182,109 @@ 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) {
|
||||||
const client = await this.clientCw(instance);
|
try {
|
||||||
|
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 nameContact = !body.key.fromMe ? body.pushName : chatId;
|
|
||||||
|
|
||||||
const filterInbox = await this.getInbox(instance);
|
|
||||||
|
|
||||||
const contact =
|
|
||||||
(await this.findContact(instance, chatId)) ||
|
|
||||||
((await this.createContact(instance, chatId, filterInbox.id, nameContact)) as any);
|
|
||||||
|
|
||||||
const contactId = contact.id || contact.payload.contact.id;
|
|
||||||
|
|
||||||
if (!body.key.fromMe && contact.name === chatId && nameContact !== chatId) {
|
|
||||||
await this.updateContact(instance, contactId, {
|
|
||||||
name: nameContact,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const contactConversations = (await client.contacts.listConversations({
|
|
||||||
accountId: this.provider.account_id,
|
|
||||||
id: contactId,
|
|
||||||
})) as any;
|
|
||||||
|
|
||||||
if (contactConversations) {
|
|
||||||
const conversation = contactConversations.payload.find(
|
|
||||||
(conversation) =>
|
|
||||||
conversation.status !== 'resolved' && conversation.inbox_id == filterInbox.id,
|
|
||||||
);
|
|
||||||
if (conversation) {
|
|
||||||
return conversation.id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const isGroup = body.key.remoteJid.includes('@g.us');
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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 =
|
||||||
|
(await this.findContact(instance, chatId)) ||
|
||||||
|
((await this.createContact(
|
||||||
|
instance,
|
||||||
|
chatId,
|
||||||
|
filterInbox.id,
|
||||||
|
isGroup,
|
||||||
|
nameContact,
|
||||||
|
)) as any);
|
||||||
|
|
||||||
|
const contactId = contact.id || contact.payload.contact.id;
|
||||||
|
|
||||||
|
if (!body.key.fromMe && contact.name === chatId && nameContact !== chatId) {
|
||||||
|
await this.updateContact(instance, contactId, {
|
||||||
|
name: nameContact,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const contactConversations = (await client.contacts.listConversations({
|
||||||
|
accountId: this.provider.account_id,
|
||||||
|
id: contactId,
|
||||||
|
})) as any;
|
||||||
|
|
||||||
|
if (contactConversations) {
|
||||||
|
const conversation = contactConversations.payload.find(
|
||||||
|
(conversation) =>
|
||||||
|
conversation.status !== 'resolved' && conversation.inbox_id == filterInbox.id,
|
||||||
|
);
|
||||||
|
if (conversation) {
|
||||||
|
return conversation.id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const conversation = await client.conversations.create({
|
||||||
|
accountId: this.provider.account_id,
|
||||||
|
data: {
|
||||||
|
contact_id: `${contactId}`,
|
||||||
|
inbox_id: `${filterInbox.id}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return conversation.id;
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
const conversation = await client.conversations.create({
|
|
||||||
accountId: this.provider.account_id,
|
|
||||||
data: {
|
|
||||||
contact_id: `${contactId}`,
|
|
||||||
inbox_id: `${filterInbox.id}`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return conversation.id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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,30 +759,65 @@ export class ChatwootService {
|
|||||||
|
|
||||||
writeFileSync(fileName, fileData, 'utf8');
|
writeFileSync(fileName, fileData, 'utf8');
|
||||||
|
|
||||||
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}`;
|
||||||
|
return await this.sendData(getConversion, fileName, messageType, content);
|
||||||
|
} else {
|
||||||
|
return await this.sendData(getConversion, fileName, messageType, bodyMessage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const send = await this.createMessage(
|
if (body.key.remoteJid.includes('@g.us')) {
|
||||||
instance,
|
const participantName = body.pushName;
|
||||||
getConversion,
|
|
||||||
bodyMessage,
|
|
||||||
messageType,
|
|
||||||
);
|
|
||||||
|
|
||||||
this.messageCacheFile = path.join(
|
const content = `**${participantName}**\n\n${bodyMessage}`;
|
||||||
ROOT_DIR,
|
|
||||||
'store',
|
|
||||||
'chatwoot',
|
|
||||||
`${instance.instanceName}_cache.txt`,
|
|
||||||
);
|
|
||||||
|
|
||||||
this.messageCache = this.loadMessageCache();
|
const send = await this.createMessage(
|
||||||
|
instance,
|
||||||
|
getConversion,
|
||||||
|
content,
|
||||||
|
messageType,
|
||||||
|
);
|
||||||
|
|
||||||
this.messageCache.add(send.id.toString());
|
this.messageCacheFile = path.join(
|
||||||
|
ROOT_DIR,
|
||||||
|
'store',
|
||||||
|
'chatwoot',
|
||||||
|
`${instance.instanceName}_cache.txt`,
|
||||||
|
);
|
||||||
|
|
||||||
this.saveMessageCache();
|
this.messageCache = this.loadMessageCache();
|
||||||
|
|
||||||
return send;
|
this.messageCache.add(send.id.toString());
|
||||||
|
|
||||||
|
this.saveMessageCache();
|
||||||
|
|
||||||
|
return send;
|
||||||
|
} else {
|
||||||
|
const send = await this.createMessage(
|
||||||
|
instance,
|
||||||
|
getConversion,
|
||||||
|
bodyMessage,
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event === 'status.instance') {
|
if (event === 'status.instance') {
|
||||||
|
Loading…
Reference in New Issue
Block a user