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 |
||
|---|---|---|
| .. | ||
| lib | ||
| tasks | ||
| .dockerignore | ||
| crontab | ||
| docker-entrypoint.sh | ||
| Dockerfile | ||
| graphile.config.ts | ||
| index.ts | ||
| package.json | ||
| README.md | ||
| tsconfig.json | ||
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 stringGRAPHILE_WORKER_CONCURRENCY- Number of concurrent jobs (default: 10)GRAPHILE_WORKER_POLL_INTERVAL- Job poll interval in ms (default: 1000)ZAMMAD_URL- Zammad instance URLZAMMAD_API_TOKEN- Zammad API tokenTWILIO_ACCOUNT_SID- Twilio account SIDTWILIO_AUTH_TOKEN- Twilio auth tokenSIGNAL_CLI_URL- Signal CLI REST API URLWHATSAPP_SERVICE_URL- WhatsApp bridge service URLFACEBOOK_APP_SECRET- Facebook app secretFACEBOOK_PAGE_ACCESS_TOKEN- Facebook page token
Available Scripts
npm run build- Compile TypeScriptnpm run dev- Development mode with watchnpm run start- Start production worker
Task Types
Signal Tasks
receive-signal-message- Process incoming Signal messagessend-signal-message- Send outgoing Signal messagesfetch-signal-messages- Fetch messages from Signal CLI
WhatsApp Tasks
receive-whatsapp-message- Process incoming WhatsApp messagessend-whatsapp-message- Send outgoing WhatsApp messages
Facebook Tasks
receive-facebook-message- Process incoming Facebook messagessend-facebook-message- Send outgoing Facebook messages
Voice Tasks
receive-voice-message- Process incoming voice calls/messagessend-voice-message- Send voice messages via Twiliotwilio-recording- Handle Twilio call recordingsvoice-line-audio-update- Update voice line audiovoice-line-delete- Delete voice linevoice-line-provider-update- Update voice provider settings
Common Tasks
notify-webhooks- Send webhook notificationsimport-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.