fix(chatwoot): findContactByIdentifier runtime crash 'TypeError: client.get is not a function' & LID fallback issue

Open Beginner friendly
#2,795 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 4 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
api, backend

Research direction

Start with src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts and trace findContactByIdentifier plus the createConversation fallback described in the issue. Check how the Chatwoot client exposes contact search and how the remote JID is selected when remoteJidAlt is missing. Update both behaviors and run the relevant tests for Chatwoot contact lookup and conversation creation; done when the 422 recovery and missing alternate JID no longer cause runtime errors.

Written by the indexing model from the issue text.

Description

Description

When using the native Chatwoot integration with Evolution API, a critical runtime error occurs that causes incoming WhatsApp messages to fail and get dropped silently in ChatwootService:

TypeError: t.get is not a function in findContactByIdentifier:
When an incoming message arrives and Chatwoot returns 422 Unprocessable Entity on contact creation (e.g., when the identifier or phone number is already associated with an existing contact in Chatwoot), Evolution API attempts to recover by calling findContactByIdentifier(instance, identifier).

Inside findContactByIdentifier, it calls:

const contact = (await (client as any).get('contacts/search', { params: { q: identifier, sort: 'name' } }));

However, at runtime, the ChatwootClient instance returned by clientCw does not expose a root .get() method. It crashes with:

ERROR [ChatwootService] Error in createConversation: TypeError: t.get is not a function
WARN  [ChatwootService] conversation not found

Additionally, in createConversation:

const i = s && !a ? t.key.remoteJidAlt : t.key.remoteJid;

When a user sends a message from a Linked Device / WhatsApp privacy mode where remoteJidAlt is undefined in that packet, i becomes undefined, causing downstream calls like i.split('@')[0] to throw TypeError: Cannot read properties of undefined (reading 'split').


Steps to Reproduce
  1. Connect WhatsApp with Chatwoot native integration enabled (/chatwoot/set/:instance).
  2. In Chatwoot, have an existing contact where identifier matches the incoming sender's WhatsApp JID but the stored phone number differs or has conflict.
  3. Send a message to the instance from that WhatsApp number.
  4. Chatwoot returns 422 on contact creation because the identifier is already registered.
  5. Evolution API enters the 422 catch block and executes this.findContactByIdentifier(...).
  6. Watch Evolution API logs throw TypeError: t.get is not a function and fail to create the conversation.

Source Code References

File: src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts

  1. findContactByIdentifier runtime failure:

    async findContactByIdentifier(instance: InstanceDto, identifier: string) {
      const client = await this.clientCw(instance);
      if (!client) {
        this.logger.warn('client not found');
        return null;
      }
    
      // BUG: client is a ChatwootClient instance, not an Axios instance. Calling .get() fails at runtime.
      const contact = (await (client as any).get('contacts/search', {
        params: { q: identifier, sort: 'name' },
      })) as any;
    
  2. LID remoteJidAlt undefined issue:

    // BUG: if remoteJidAlt is undefined, i becomes undefined
    const i = s && !a ? t.key.remoteJidAlt : t.key.remoteJid;
    

Suggested Fix
  1. In findContactByIdentifier, query contacts via client.contacts.search:

    async findContactByIdentifier(instance: InstanceDto, identifier: string) {
      const client = await this.clientCw(instance);
      if (!client) {
        this.logger.warn('client not found');
        return null;
      }
    
      try {
        const res = await client.contacts.search({
          accountId: this.provider.accountId,
          q: identifier,
        });
        if (res?.payload?.length > 0) {
          return res.payload[0];
        }
      } catch (err) {
        this.logger.error(`Error searching contact by identifier: ${err}`);
      }
      return null;
    }
    
  2. In createConversation, fallback to t.key.remoteJid if remoteJidAlt is missing:

    const i = s && !a ? (t.key.remoteJidAlt || t.key.remoteJid) : t.key.remoteJid;
    

Environment
  • Evolution API Version: v2.3.7
  • Chatwoot Version: v3.x / v4.x
  • Deployment: Docker / Linux
Dominant language
TypeScript
Stars
10.2k
Forks
7.5k
Avg merge
6d 19h
Merged PRs (30d)
4

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from evolution-foundation/evolution-api

All issues in evolution-foundation/evolution-api

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.