|
|
|
@ -79,18 +79,7 @@ export class ContactsService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static async maybeFetchFromApi(digitsId: string): Promise<string | null> {
|
|
|
|
private static async fetchNameFrom(url: string, body: any, digitsId: string): Promise<string | null> {
|
|
|
|
const baseUrl = process.env.EVOLUTION_API_URL;
|
|
|
|
|
|
|
|
const instance = process.env.EVOLUTION_API_INSTANCE;
|
|
|
|
|
|
|
|
const apikey = process.env.EVOLUTION_API_KEY;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!baseUrl || !instance || !apikey) return null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const url = `${baseUrl}/chat/findContacts/${instance}`;
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
|
|
|
|
where: { id: `${digitsId}@s.whatsapp.net` }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const res = await fetch(url, {
|
|
|
|
const res = await fetch(url, {
|
|
|
|
method: 'POST',
|
|
|
|
method: 'POST',
|
|
|
|
@ -99,10 +88,6 @@ export class ContactsService {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
if (!res.ok) {
|
|
|
|
if (process.env.NODE_ENV !== 'test') {
|
|
|
|
|
|
|
|
const txt = await res.text().catch(() => '');
|
|
|
|
|
|
|
|
console.warn('ContactsService.findContacts non-OK:', res.status, txt?.slice(0, 200));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -119,29 +104,49 @@ export class ContactsService {
|
|
|
|
? [data]
|
|
|
|
? [data]
|
|
|
|
: [];
|
|
|
|
: [];
|
|
|
|
|
|
|
|
|
|
|
|
let name: string | null = null;
|
|
|
|
|
|
|
|
for (const rec of arrayCandidates) {
|
|
|
|
for (const rec of arrayCandidates) {
|
|
|
|
const recId = rec?.id ?? rec?.jid ?? rec?.user ?? rec?.remoteJid ?? rec?.wid ?? rec?.wid?._serialized;
|
|
|
|
const recId = rec?.id ?? rec?.jid ?? rec?.user ?? rec?.remoteJid ?? rec?.wid ?? rec?.wid?._serialized;
|
|
|
|
if (!recId) continue;
|
|
|
|
if (!recId) continue;
|
|
|
|
const norm = normalizeWhatsAppId(String(recId));
|
|
|
|
const norm = normalizeWhatsAppId(String(recId));
|
|
|
|
if (!norm || norm !== digitsId) continue;
|
|
|
|
if (!norm || norm !== digitsId) continue;
|
|
|
|
name = this.extractName(rec);
|
|
|
|
const name = this.extractName(rec);
|
|
|
|
if (name) break;
|
|
|
|
if (name) {
|
|
|
|
}
|
|
|
|
this.cache.set(digitsId, { name, expiresAt: this.now() + this.TTL_MS });
|
|
|
|
|
|
|
|
return name;
|
|
|
|
if (name) {
|
|
|
|
}
|
|
|
|
this.cache.set(digitsId, { name, expiresAt: this.now() + this.TTL_MS });
|
|
|
|
|
|
|
|
return name;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
} catch (e) {
|
|
|
|
} catch {
|
|
|
|
if (process.env.NODE_ENV !== 'test') {
|
|
|
|
|
|
|
|
console.warn('ContactsService.findContacts error:', e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static async maybeFetchFromApi(digitsId: string): Promise<string | null> {
|
|
|
|
|
|
|
|
const baseUrl = process.env.EVOLUTION_API_URL;
|
|
|
|
|
|
|
|
const instance = process.env.EVOLUTION_API_INSTANCE;
|
|
|
|
|
|
|
|
const apikey = process.env.EVOLUTION_API_KEY;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!baseUrl || !instance || !apikey) return null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const jid = `${digitsId}@s.whatsapp.net`;
|
|
|
|
|
|
|
|
const body = { where: { id: jid } };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Probar múltiples rutas conocidas según versión de Evolution
|
|
|
|
|
|
|
|
const candidates = [
|
|
|
|
|
|
|
|
`${baseUrl}/chat/findContacts/${instance}`,
|
|
|
|
|
|
|
|
`${baseUrl}/contact/findContacts/${instance}`,
|
|
|
|
|
|
|
|
`${baseUrl}/contact/find/${instance}`,
|
|
|
|
|
|
|
|
`${baseUrl}/chat/find/${instance}`,
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const url of candidates) {
|
|
|
|
|
|
|
|
const name = await this.fetchNameFrom(url, body, digitsId);
|
|
|
|
|
|
|
|
if (name) return name;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static async getDisplayName(idOrJid: string | null | undefined): Promise<string | null> {
|
|
|
|
static async getDisplayName(idOrJid: string | null | undefined): Promise<string | null> {
|
|
|
|
const normalized = normalizeWhatsAppId(idOrJid || '');
|
|
|
|
const normalized = normalizeWhatsAppId(idOrJid || '');
|
|
|
|
if (!normalized) return null;
|
|
|
|
if (!normalized) return null;
|
|
|
|
|