refactor: añade rutas Evolution para obtener nombre de contacto

Co-authored-by: aider (openrouter/openai/gpt-5) <aider@aider.chat>
pull/1/head
borja 2 months ago
parent 8fdac8b49f
commit bfb9f44b69

@ -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,27 +104,47 @@ 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) { if (name) {
this.cache.set(digitsId, { name, expiresAt: this.now() + this.TTL_MS }); this.cache.set(digitsId, { name, expiresAt: this.now() + this.TTL_MS });
return name; return name;
} }
return null;
} catch (e) {
if (process.env.NODE_ENV !== 'test') {
console.warn('ContactsService.findContacts error:', e);
} }
return null; return null;
} catch {
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> {

Loading…
Cancel
Save