link-stack/apps/bridge-worker
Darren Clarke 3d8f794cab Add user ID support for Baileys 7 LIDs and Signal UUIDs
Baileys 7 uses LIDs (Linked IDs) instead of phone numbers in remoteJid for
some messages. This caused messages to be matched to wrong tickets because
the LID was used as the sender identifier. This commit adds proper support
for both phone numbers and user IDs across WhatsApp and Signal channels.

Changes:

Database:
- Add migration for whatsapp_user_id and signal_user_id fields on users table

Zammad controllers:
- Update user lookup with 3-step fallback: phone → dedicated user_id field →
  user_id in phone field (legacy)
- Store user IDs in dedicated fields when available
- Update phone field when we receive actual phone number for legacy records
- Fix redundant condition in Signal controller

Bridge services:
- Extract both phone (from senderPn/participantPn) and LID (from remoteJid)
- Send both identifiers to Zammad via webhooks
- Use camelCase (userId) in bridge-whatsapp, convert to snake_case (user_id)
  in bridge-worker for Zammad compatibility

Baileys 7 compliance:
- Remove broken loadAllUnreadMessages() call (removed in Baileys 7)
- Return descriptive error directing users to use webhooks instead

Misc:
- Add docs/ to .gitignore
2026-01-15 13:08:56 +01:00
..
lib WhatsApp/Signal/Formstack/admin updates 2025-11-21 14:55:28 +01:00
tasks Add user ID support for Baileys 7 LIDs and Signal UUIDs 2026-01-15 13:08:56 +01:00
.dockerignore Bridge worker updates 2024-04-21 16:59:50 +02:00
crontab WhatsApp/Signal/Formstack/admin updates 2025-11-21 14:55:28 +01:00
docker-entrypoint.sh WhatsApp/Signal/Formstack/admin updates 2025-11-21 14:55:28 +01:00
Dockerfile WhatsApp/Signal/Formstack/admin updates 2025-11-21 14:55:28 +01:00
graphile.config.ts WhatsApp/Signal/Formstack/admin updates 2025-11-21 14:55:28 +01:00
index.ts WhatsApp/Signal/Formstack/admin updates 2025-11-21 14:55:28 +01:00
package.json Bump version to 3.4.0-beta.4 2026-01-14 11:33:11 +01:00
README.md WhatsApp/Signal/Formstack/admin updates 2025-11-21 14:55:28 +01:00
tsconfig.json Worker build updates 2024-06-28 07:49:39 +02:00

Bridge Worker

Background job processor for handling asynchronous tasks in the CDR Link communication bridge system.

Overview

Bridge Worker uses Graphile Worker to process queued jobs for message handling, media conversion, webhook notifications, and scheduled tasks. It manages the flow of messages between various communication channels (Signal, WhatsApp, Facebook, Voice) and the Zammad ticketing system.

Features

  • Message Processing: Handle incoming/outgoing messages for all supported channels
  • Media Conversion: Convert audio/video files between formats
  • Webhook Notifications: Notify external systems of events
  • Scheduled Tasks: Cron-based job scheduling
  • Job Queue Management: Reliable job processing with retries
  • Multi-Channel Support: Signal, WhatsApp, Facebook, Voice (Twilio)

Development

Prerequisites

  • Node.js >= 20
  • npm >= 10
  • PostgreSQL database
  • Redis (for caching)
  • FFmpeg (for media conversion)

Setup

# Install dependencies
npm install

# Build TypeScript
npm run build

# Run development server with auto-reload
npm run dev

# Start production worker
npm run start

Environment Variables

Required environment variables:

  • DATABASE_URL - PostgreSQL connection string
  • GRAPHILE_WORKER_CONCURRENCY - Number of concurrent jobs (default: 10)
  • GRAPHILE_WORKER_POLL_INTERVAL - Job poll interval in ms (default: 1000)
  • ZAMMAD_URL - Zammad instance URL
  • ZAMMAD_API_TOKEN - Zammad API token
  • TWILIO_ACCOUNT_SID - Twilio account SID
  • TWILIO_AUTH_TOKEN - Twilio auth token
  • SIGNAL_CLI_URL - Signal CLI REST API URL
  • WHATSAPP_SERVICE_URL - WhatsApp bridge service URL
  • FACEBOOK_APP_SECRET - Facebook app secret
  • FACEBOOK_PAGE_ACCESS_TOKEN - Facebook page token

Available Scripts

  • npm run build - Compile TypeScript
  • npm run dev - Development mode with watch
  • npm run start - Start production worker

Task Types

Signal Tasks

  • receive-signal-message - Process incoming Signal messages
  • send-signal-message - Send outgoing Signal messages
  • fetch-signal-messages - Fetch messages from Signal CLI

WhatsApp Tasks

  • receive-whatsapp-message - Process incoming WhatsApp messages
  • send-whatsapp-message - Send outgoing WhatsApp messages

Facebook Tasks

  • receive-facebook-message - Process incoming Facebook messages
  • send-facebook-message - Send outgoing Facebook messages

Voice Tasks

  • receive-voice-message - Process incoming voice calls/messages
  • send-voice-message - Send voice messages via Twilio
  • twilio-recording - Handle Twilio call recordings
  • voice-line-audio-update - Update voice line audio
  • voice-line-delete - Delete voice line
  • voice-line-provider-update - Update voice provider settings

Common Tasks

  • notify-webhooks - Send webhook notifications
  • import-label-studio - Import Label Studio annotations

Architecture

Job Processing

Jobs are queued in PostgreSQL using Graphile Worker:

await addJob('send-signal-message', {
  to: '+1234567890',
  message: 'Hello world'
})

Cron Schedule

Scheduled tasks are configured in crontab:

  • Periodic message fetching
  • Cleanup tasks
  • Health checks

Error Handling

  • Automatic retries with exponential backoff
  • Dead letter queue for failed jobs
  • Comprehensive logging with winston

Media Handling

Supports conversion between formats:

  • Audio: MP3, OGG, WAV, M4A
  • Uses fluent-ffmpeg for processing
  • Automatic format detection

Integration Points

  • Zammad: Creates/updates tickets via API
  • Signal CLI: REST API for Signal messaging
  • WhatsApp Bridge: HTTP API for WhatsApp
  • Twilio: Voice and SMS capabilities
  • Facebook: Graph API for Messenger

Docker Support

# Build image
docker build -t link-stack/bridge-worker .

# Run with docker-compose
docker-compose -f docker/compose/bridge.yml up

The worker includes cron support via built-in crontab.