mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-21 19:47:21 -06:00
feature(chatwoot): add merge_brazil_contacts function to solve nine digit in brazilian numbers
This commit is contained in:
parent
7a0149ee23
commit
dcc32479ff
@ -66,6 +66,7 @@ export class InstanceController {
|
|||||||
chatwoot_conversation_pending,
|
chatwoot_conversation_pending,
|
||||||
chatwoot_import_contacts,
|
chatwoot_import_contacts,
|
||||||
chatwoot_name_inbox,
|
chatwoot_name_inbox,
|
||||||
|
chatwoot_merge_brazil_contacts,
|
||||||
chatwoot_import_messages,
|
chatwoot_import_messages,
|
||||||
chatwoot_days_limit_import_messages,
|
chatwoot_days_limit_import_messages,
|
||||||
reject_call,
|
reject_call,
|
||||||
@ -519,6 +520,7 @@ export class InstanceController {
|
|||||||
reopen_conversation: chatwoot_reopen_conversation || false,
|
reopen_conversation: chatwoot_reopen_conversation || false,
|
||||||
conversation_pending: chatwoot_conversation_pending || false,
|
conversation_pending: chatwoot_conversation_pending || false,
|
||||||
import_contacts: chatwoot_import_contacts ?? true,
|
import_contacts: chatwoot_import_contacts ?? true,
|
||||||
|
merge_brazil_contacts: chatwoot_merge_brazil_contacts ?? false,
|
||||||
import_messages: chatwoot_import_messages ?? true,
|
import_messages: chatwoot_import_messages ?? true,
|
||||||
days_limit_import_messages: chatwoot_days_limit_import_messages ?? 60,
|
days_limit_import_messages: chatwoot_days_limit_import_messages ?? 60,
|
||||||
auto_create: true,
|
auto_create: true,
|
||||||
|
@ -27,6 +27,7 @@ export class InstanceDto {
|
|||||||
chatwoot_sign_msg?: boolean;
|
chatwoot_sign_msg?: boolean;
|
||||||
chatwoot_reopen_conversation?: boolean;
|
chatwoot_reopen_conversation?: boolean;
|
||||||
chatwoot_conversation_pending?: boolean;
|
chatwoot_conversation_pending?: boolean;
|
||||||
|
chatwoot_merge_brazil_contacts?: boolean;
|
||||||
chatwoot_import_contacts?: boolean;
|
chatwoot_import_contacts?: boolean;
|
||||||
chatwoot_import_messages?: boolean;
|
chatwoot_import_messages?: boolean;
|
||||||
chatwoot_days_limit_import_messages?: number;
|
chatwoot_days_limit_import_messages?: number;
|
||||||
|
@ -53,6 +53,7 @@ export class ChatwootController {
|
|||||||
data.conversation_pending = false;
|
data.conversation_pending = false;
|
||||||
data.import_contacts = false;
|
data.import_contacts = false;
|
||||||
data.import_messages = false;
|
data.import_messages = false;
|
||||||
|
data.merge_brazil_contacts = false;
|
||||||
data.days_limit_import_messages = 0;
|
data.days_limit_import_messages = 0;
|
||||||
data.auto_create = false;
|
data.auto_create = false;
|
||||||
data.name_inbox = '';
|
data.name_inbox = '';
|
||||||
|
@ -9,6 +9,7 @@ export class ChatwootDto {
|
|||||||
number?: string;
|
number?: string;
|
||||||
reopen_conversation?: boolean;
|
reopen_conversation?: boolean;
|
||||||
conversation_pending?: boolean;
|
conversation_pending?: boolean;
|
||||||
|
merge_brazil_contacts?: boolean;
|
||||||
import_contacts?: boolean;
|
import_contacts?: boolean;
|
||||||
import_messages?: boolean;
|
import_messages?: boolean;
|
||||||
days_limit_import_messages?: number;
|
days_limit_import_messages?: number;
|
||||||
|
@ -14,6 +14,7 @@ export class ChatwootRaw {
|
|||||||
number?: string;
|
number?: string;
|
||||||
reopen_conversation?: boolean;
|
reopen_conversation?: boolean;
|
||||||
conversation_pending?: boolean;
|
conversation_pending?: boolean;
|
||||||
|
merge_brazil_contacts?: boolean;
|
||||||
import_contacts?: boolean;
|
import_contacts?: boolean;
|
||||||
import_messages?: boolean;
|
import_messages?: boolean;
|
||||||
days_limit_import_messages?: number;
|
days_limit_import_messages?: number;
|
||||||
@ -31,6 +32,7 @@ const chatwootSchema = new Schema<ChatwootRaw>({
|
|||||||
number: { type: String, required: true },
|
number: { type: String, required: true },
|
||||||
reopen_conversation: { type: Boolean, required: true },
|
reopen_conversation: { type: Boolean, required: true },
|
||||||
conversation_pending: { type: Boolean, required: true },
|
conversation_pending: { type: Boolean, required: true },
|
||||||
|
merge_brazil_contacts: { type: Boolean, required: true },
|
||||||
import_contacts: { type: Boolean, required: true },
|
import_contacts: { type: Boolean, required: true },
|
||||||
import_messages: { type: Boolean, required: true },
|
import_messages: { type: Boolean, required: true },
|
||||||
days_limit_import_messages: { type: Number, required: true },
|
days_limit_import_messages: { type: Number, required: true },
|
||||||
|
@ -86,13 +86,14 @@ export class ChatwootService {
|
|||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
public getClientCwConfig(): ChatwootAPIConfig & { name_inbox: string } {
|
public getClientCwConfig(): ChatwootAPIConfig & { name_inbox: string; merge_brazil_contacts: boolean } {
|
||||||
return {
|
return {
|
||||||
basePath: this.provider.url,
|
basePath: this.provider.url,
|
||||||
with_credentials: true,
|
with_credentials: true,
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
token: this.provider.token,
|
token: this.provider.token,
|
||||||
name_inbox: this.provider.name_inbox,
|
name_inbox: this.provider.name_inbox,
|
||||||
|
merge_brazil_contacts: this.provider.merge_brazil_contacts,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -418,10 +419,49 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async mergeBrazilianContacts(contacts: any[]) {
|
||||||
|
try {
|
||||||
|
//sdk chatwoot não tem função merge
|
||||||
|
this.logger.verbose('merging contacts');
|
||||||
|
const contact = await chatwootRequest(this.getClientCwConfig(), {
|
||||||
|
method: 'POST',
|
||||||
|
url: `/api/v1/accounts/${this.provider.account_id}/actions/contact_merge`,
|
||||||
|
body: {
|
||||||
|
base_contact_id: contacts.find((contact) => contact.phone_number.length === 14)?.id,
|
||||||
|
mergee_contact_id: contacts.find((contact) => contact.phone_number.length === 13)?.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return contact;
|
||||||
|
} catch {
|
||||||
|
this.logger.error('Error merging contacts');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private findContactInContactList(contacts: any[], query: string) {
|
private findContactInContactList(contacts: any[], query: string) {
|
||||||
const phoneNumbers = this.getNumbers(query);
|
const phoneNumbers = this.getNumbers(query);
|
||||||
const searchableFields = this.getSearchableFields();
|
const searchableFields = this.getSearchableFields();
|
||||||
|
|
||||||
|
// eslint-disable-next-line prettier/prettier
|
||||||
|
if(contacts.length === 2 && this.getClientCwConfig().merge_brazil_contacts && query.startsWith('+55')){
|
||||||
|
|
||||||
|
const contact = this.mergeBrazilianContacts(contacts);
|
||||||
|
if (contact) {
|
||||||
|
return contact;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const phone = phoneNumbers.reduce(
|
||||||
|
(savedNumber, number) => (number.length > savedNumber.length ? number : savedNumber),
|
||||||
|
'',
|
||||||
|
);
|
||||||
|
|
||||||
|
const contact_with9 = contacts.find((contact) => contact.phone_number === phone);
|
||||||
|
if (contact_with9) {
|
||||||
|
return contact_with9;
|
||||||
|
}
|
||||||
|
|
||||||
for (const contact of contacts) {
|
for (const contact of contacts) {
|
||||||
for (const field of searchableFields) {
|
for (const field of searchableFields) {
|
||||||
if (contact[field] && phoneNumbers.includes(contact[field])) {
|
if (contact[field] && phoneNumbers.includes(contact[field])) {
|
||||||
@ -449,7 +489,7 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private getSearchableFields() {
|
private getSearchableFields() {
|
||||||
return ['phone_number', 'identifier'];
|
return ['phone_number'];
|
||||||
}
|
}
|
||||||
|
|
||||||
private getFilterPayload(query: string) {
|
private getFilterPayload(query: string) {
|
||||||
@ -463,7 +503,7 @@ export class ChatwootService {
|
|||||||
const queryOperator = fieldsToSearch.length - 1 === index1 && numbers.length - 1 === index2 ? null : 'OR';
|
const queryOperator = fieldsToSearch.length - 1 === index1 && numbers.length - 1 === index2 ? null : 'OR';
|
||||||
filterPayload.push({
|
filterPayload.push({
|
||||||
attribute_key: field,
|
attribute_key: field,
|
||||||
filter_operator: ['phone_number', 'identifier'].includes(field) ? 'equal_to' : 'contains',
|
filter_operator: 'equal_to',
|
||||||
values: [number.replace('+', '')],
|
values: [number.replace('+', '')],
|
||||||
query_operator: queryOperator,
|
query_operator: queryOperator,
|
||||||
});
|
});
|
||||||
|
@ -35,6 +35,7 @@ export const chatwootSchema: JSONSchema7 = {
|
|||||||
conversation_pending: { type: 'boolean', enum: [true, false] },
|
conversation_pending: { type: 'boolean', enum: [true, false] },
|
||||||
auto_create: { type: 'boolean', enum: [true, false] },
|
auto_create: { type: 'boolean', enum: [true, false] },
|
||||||
import_contacts: { type: 'boolean', enum: [true, false] },
|
import_contacts: { type: 'boolean', enum: [true, false] },
|
||||||
|
merge_brazil_contacts: { type: 'boolean', enum: [true, false] },
|
||||||
import_messages: { type: 'boolean', enum: [true, false] },
|
import_messages: { type: 'boolean', enum: [true, false] },
|
||||||
days_limit_import_messages: { type: 'number' },
|
days_limit_import_messages: { type: 'number' },
|
||||||
},
|
},
|
||||||
|
@ -356,6 +356,7 @@ export class ChannelStartupService {
|
|||||||
this.logger.verbose(`Chatwoot sign delimiter: ${data.sign_delimiter}`);
|
this.logger.verbose(`Chatwoot sign delimiter: ${data.sign_delimiter}`);
|
||||||
this.logger.verbose(`Chatwoot reopen conversation: ${data.reopen_conversation}`);
|
this.logger.verbose(`Chatwoot reopen conversation: ${data.reopen_conversation}`);
|
||||||
this.logger.verbose(`Chatwoot conversation pending: ${data.conversation_pending}`);
|
this.logger.verbose(`Chatwoot conversation pending: ${data.conversation_pending}`);
|
||||||
|
this.logger.verbose(`Chatwoot merge brazilian contacts: ${data.import_contacts}`);
|
||||||
this.logger.verbose(`Chatwoot import contacts: ${data.import_contacts}`);
|
this.logger.verbose(`Chatwoot import contacts: ${data.import_contacts}`);
|
||||||
this.logger.verbose(`Chatwoot import messages: ${data.import_messages}`);
|
this.logger.verbose(`Chatwoot import messages: ${data.import_messages}`);
|
||||||
this.logger.verbose(`Chatwoot days limit import messages: ${data.days_limit_import_messages}`);
|
this.logger.verbose(`Chatwoot days limit import messages: ${data.days_limit_import_messages}`);
|
||||||
@ -370,6 +371,7 @@ export class ChannelStartupService {
|
|||||||
sign_delimiter: data.sign_delimiter || null,
|
sign_delimiter: data.sign_delimiter || null,
|
||||||
reopen_conversation: data.reopen_conversation,
|
reopen_conversation: data.reopen_conversation,
|
||||||
conversation_pending: data.conversation_pending,
|
conversation_pending: data.conversation_pending,
|
||||||
|
merge_brazil_contacts: data.merge_brazil_contacts,
|
||||||
import_contacts: data.import_contacts,
|
import_contacts: data.import_contacts,
|
||||||
import_messages: data.import_messages,
|
import_messages: data.import_messages,
|
||||||
days_limit_import_messages: data.days_limit_import_messages,
|
days_limit_import_messages: data.days_limit_import_messages,
|
||||||
|
Loading…
Reference in New Issue
Block a user