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

未关闭 适合新手
#2,795 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 4 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
76/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
typescript
领域
api, backend

调研方向

从src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts开始,跟踪findContactByIdentifier以及 issue 中描述的createConversation回退逻辑。检查 Chatwoot 客户端如何提供联系人搜索功能,以及缺少remoteJidAlt时如何选择远程 JID。更新这两种行为,并运行与 Chatwoot 联系人查找和会话创建相关的测试;当 422 错误恢复处理和缺少备用 JID 不再导致运行时错误时,即为完成。

由索引模型根据 Issue 内容生成。

描述

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
主要语言
TypeScript
星标
10.2k
派生
7.5k
平均合并
6 天 19 小时
30 天内合并 PR
4

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

evolution-foundation/evolution-api 的其他 Issue

查看 evolution-foundation/evolution-api 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。