Skip to content

fix(chatwoot): avoid "undefined" caption on group audio messages #1434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

edisonmartinsmkt
Copy link
Contributor

@edisonmartinsmkt edisonmartinsmkt commented May 10, 2025

This PR fixes an issue where incoming audio messages in WhatsApp groups were displaying the text "undefined" as a caption in Chatwoot.

The change ensures that if no caption is present in the audioMessage, an empty string is sent instead — preventing the literal string "undefined" from appearing in the UI.

Before:
🟥 The word "undefined" was shown above the audio player when no caption existed.
image

After:
✅ No caption is displayed, keeping the interface clean.
image

Summary by Sourcery

Fix an issue with audio message captions in WhatsApp group messages by ensuring an empty string is used when no caption is present

Bug Fixes:

  • Resolved an issue where 'undefined' was displayed as a caption for audio messages in WhatsApp groups

Enhancements:

  • Updated caption handling for audio messages to prevent 'undefined' text from appearing in the UI

Copy link
Contributor

sourcery-ai bot commented May 10, 2025

Reviewer's Guide

This pull request primarily resolves an issue where audio messages in WhatsApp groups lacking captions incorrectly displayed "undefined" in Chatwoot; this is now handled by sending an empty string. However, the scope of this PR extends significantly beyond this single fix. It introduces NATS as a new event messaging system, configured similarly to existing RabbitMQ/SQS integrations, and adds new capabilities for fetching WhatsApp Business catalogs and collections via dedicated service methods, controllers, and routes. Message processing is enhanced through improved duplicate message prevention using caching, more robust message editing/deletion logic with database persistence and webhook dispatches, and better support for animated WebP stickers including specific audio conversion parameters. SQS queue management has been refactored using AWS SDK v3 for more reliable queue creation and listing. WebSocket connections now enforce API key-based authentication. Outgoing webhooks can now utilize JWT-based authentication if configured. Services for fetching contacts and chats have been updated to support pagination and return richer data. A notable security enhancement involves replacing execSync with execFileSync to mitigate potential shell injection vulnerabilities during file operations. Various configurations, dependencies (like nats and jsonwebtoken), and Docker files were updated to support these changes.

File-Level Changes

Change Details Files
Corrected 'undefined' display for audio messages without captions in Chatwoot by sending an empty string.
  • Modified logic in getMsgContent to ensure audioMessage.caption defaults to an empty string if originally null or undefined.
src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts
Introduced NATS as a new event messaging system, including its controller, router, configuration, and integration into the event management flow.
  • Implemented NatsController for publishing and subscribing to NATS messages.
  • Added NATS configuration options (URI, events, prefix) and event types.
  • Integrated NATS into the central event manager for emitting events.
  • Updated instance DTOs, schemas, and database to support NATS configuration per instance.
src/api/integrations/event/nats/nats.controller.ts
src/api/integrations/event/nats/nats.router.ts
src/config/env.config.ts
src/api/integrations/event/event.manager.ts
src/api/integrations/event/event.dto.ts
src/validate/instance.schema.ts
src/api/integrations/event/event.schema.ts
src/api/services/monitor.service.ts
package.json
prisma/postgresql-migrations/20250225180031_add_nats_integration/migration.sql
src/api/controllers/instance.controller.ts
src/api/integrations/event/event.router.ts
Added functionality to fetch WhatsApp Business product catalogs and collections, complete with new API endpoints and data structures.
  • Implemented new service methods in whatsapp.baileys.service.ts for fetching catalog and collection data from Baileys.
  • Created a new BusinessController and BusinessRouter to expose these functionalities via API.
  • Defined DTOs and validation schemas for catalog/collection requests and responses.
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
src/api/controllers/business.controller.ts
src/api/routes/business.router.ts
src/api/dto/business.dto.ts
src/validate/business.schema.ts
src/api/server.module.ts
src/api/routes/index.router.ts
Enhanced message handling and processing, including improved message editing, deletion, duplication handling, and better support for animated stickers and audio conversion.
  • Refactored message editing logic to include comprehensive checks, database updates, and dispatch of SEND_MESSAGE_UPDATE event.
  • Implemented caching mechanisms to prevent processing of duplicate incoming messages and message status updates.
  • Added support for converting animated GIFs/WebPs to animated WebP stickers using sharp.
  • Expanded ffmpeg options for audio conversion, allowing more control over output quality.
  • Introduced notConvertSticker option in SendStickerDto.
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
src/config/env.config.ts
src/validate/instance.schema.ts
src/api/integrations/event/event.controller.ts
src/api/types/wa.types.ts
src/api/dto/sendMessage.dto.ts
Modernized SQS queue management using AWS SDK v3 commands for improved reliability in queue creation and listing.
  • Updated SqsController to use asynchronous AWS SDK v3 commands (CreateQueueCommand, ListQueuesCommand, DeleteQueueCommand).
  • Refactored saveQueues to create queues if they don't exist and listQueuesByInstance to fetch existing queues for an instance.
src/api/integrations/event/sqs/sqs.controller.ts
Implemented API key authentication for incoming WebSocket connections to enhance security.
  • Added allowRequest function in WebsocketController to validate API keys provided in query parameters or headers against instance tokens or a global token.
src/api/integrations/event/websocket/websocket.controller.ts
Enabled JWT-based authentication for outgoing webhook requests if a jwt_key is provided in webhook headers.
  • Added jsonwebtoken dependency.
  • Implemented generateJwtToken method in WebhookController.
  • Modified webhook sending logic to generate and include a Bearer JWT if jwt_key is present in headers.
src/api/integrations/event/webhook/webhook.controller.ts
package.json
Updated contact and chat fetching services to support pagination and return more detailed information like saved status and unread counts.
  • Modified getContacts to accept pagination parameters (offset, page) and to return isGroup, isSaved, type fields.
  • Updated the raw SQL query in getChats to support LIMIT and OFFSET for pagination and to include unreadCount and isSaved in results.
src/api/services/channel.service.ts
Replaced execSync with execFileSync in file operations to prevent potential shell injection vulnerabilities.
  • Updated rm -rf calls in cleaningStoreData (monitor service) to use execFileSync.
  • Updated kill command in ProviderFiles constructor (sessions provider) to use execFileSync.
src/api/services/monitor.service.ts
src/api/provider/sessions.ts
Updated various configurations (e.g., NATS, S3 policy skipping), added new dependencies (jsonwebtoken, nats), and refreshed project metadata and Docker files.
  • Added NATS related configurations and an S3 SKIP_POLICY option to environment settings.
  • Included jsonwebtoken and nats as project dependencies.
  • Updated Docker image references, contact emails in documentation and Dockerfile.
  • Updated changelog.
src/config/env.config.ts
package.json
Dockerfile
Docker/swarm/evolution_api_v2.yaml
README.md
src/api/integrations/storage/s3/libs/minio.server.ts
CHANGELOG.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@DavidsonGomes DavidsonGomes changed the base branch from main to develop May 10, 2025 14:24
@DavidsonGomes DavidsonGomes merged commit 383bac0 into EvolutionAPI:develop May 10, 2025
1 check passed
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @edisonmartinsmkt - I've reviewed your changes - here's some feedback:

  • This PR's scope is much larger than the title suggests; consider separating the Chatwoot fix from new features like NATS, Business Catalog, and the security patch.
  • Given the security fix replacing execSync, consider a broader audit for similar shell command execution patterns in the codebase.
  • The significantly refactored raw SQL query in getChats warrants a performance review, especially with large datasets.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants