Skip to content

No attachment fetchData in WhatsApp queued messages #299

@donovan-fournier

Description

@donovan-fournier

Bug Description

In WhatsApp, sending a batch of files from a client is sending several messages, with 1 attachement per message.
The new concurrency mode is especially useful to queue the message and keep the correct order.

However the attachments (from the messages processed from the queue) do not contain the fetchData method (Even if the attachments are there).

It seems a raw deserialisation is done without revive. The raw data seems to contain the data to fetch the media, at least for the WhatsApp adapter.

Configuration:

  • WhatsApp Adapter
  • Postgres state
  • Concurrency: "queue"

Steps to Reproduce

  1. Listen for messages in onDirectMessage
  2. Send several images in a WhatsApp conversation from a client (3 images)

Expected Behavior

  • The 1st message is processed
  • The 2 other messages are queued
  • The 2 other messages are processed (1 in the skipped context, the other as main message)
    • The attachements contain the fetchData to get the medias

Actual Behavior

  • The 1st message is processed
    • 1 attachment is available, with the fetchData
  • The 2 other messages are queued
  • The 2 other messages are processed (1 in the skipped context, the other as main message)
    • Each message contain 1 attachment
    • The attachements DO NOT have fetchData to get the medias

Code Sample

function createBot() {
  const env = buildApiEnvironment();
  const api = getApi(env);

  const bot = new Chat({
    userName: "chat",
    adapters: {
      whatsapp: createWhatsAppAdapter({
        accessToken: env.WHATSAPP_ACCESS_TOKEN,
        appSecret: env.WHATSAPP_APP_SECRET,
        phoneNumberId: env.WHATSAPP_PHONE_NUMBER_ID,
        verifyToken: env.WHATSAPP_VERIFY_TOKEN,
        userName: "chat",
      }),
    },
    state: createPostgresState({
      url: env.NEON_URL,
    }),
    concurrency: "queue",
  });
  bot.onDirectMessage(async (thread, message, _channel, context) => {
    console.log("Process message, skipped=", context?.skipped.length ?? 0);
    const allMessages = [...(context?.skipped ?? []), message];
    for (const m of allMessages) {
      console.log("message", message.id);

      console.log("attachements:", m.attachments.length);
      for (const a of m.attachments) {
        console.log(
          "- attachement:",
          a.mimeType,
          a.url,
          a.fetchData !== undefined,
        );
      }

      const raw = message.raw as WhatsAppRawMessage;
      console.log("raw image", raw.message.image);
    }
    await new Promise((resolve) => setTimeout(resolve, 1000)); // Force the queuing
  });

  bot.onAction(CHAT_ACTION_SELECT_ORG, async (event) =>
    (await api.inbound.manageInboundWhatsAppMessage()).executeAction(event),
  );

  return bot;
}

Chat SDK Version

4.22.0

Node.js Version

No response

Platform Adapter

Other

Operating System

macOS

Additional Context

Logs from the tests with the provided sample

Process message, skipped=0
message wamid.message_1
attachements: 1
- attachement: image/jpeg undefined true
raw image {
  mime_type: 'image/jpeg',
  sha256: '...',
  id: '...',
  url: '...'
}

[chat-sdk] message-queued {
  threadId: 'whatsapp:thread_id',
  lockKey: 'whatsapp:thread_id',
  messageId: 'wamid.message_2',
  queueDepth: 1
}
[chat-sdk] message-queued {
  threadId: 'whatsapp:thread_id',
  lockKey: 'whatsapp:thread_id',
  messageId: 'wamid.message_3',
  queueDepth: 2
}
[chat-sdk] message-dequeued {
  threadId: 'whatsapp:thread_id',
  lockKey: 'whatsapp:thread_id',
  messageId: 'wamid.message_3',
  skippedCount: 1,
  totalSinceLastHandler: 2
}
Process message , skipped=1
message wamid.message_3
attachements: 1
- attachement: image/jpeg undefined false
raw image {
  mime_type: 'image/jpeg',
  sha256: '...',
  id: '...',
  url: '...'
}
message wamid.message_3
attachements: 1
- attachement: image/jpeg undefined false
raw image {
  mime_type: 'image/jpeg',
  sha256: '...',
  id: '...',
  url: '...'
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions