Skip to content

fix(whatsapp): wire up initWhatsApp on main bot startup#48

Open
bizapen wants to merge 1 commit intoearlyaidopters:mainfrom
bizapen:fix/wire-up-initwhatsapp
Open

fix(whatsapp): wire up initWhatsApp on main bot startup#48
bizapen wants to merge 1 commit intoearlyaidopters:mainfrom
bizapen:fix/wire-up-initwhatsapp

Conversation

@bizapen
Copy link
Copy Markdown

@bizapen bizapen commented Apr 25, 2026

Bug

The /wa command always returns "WhatsApp not connected. Make sure WHATSAPP_ENABLED=true and the service is running." even when WHATSAPP_ENABLED=true is set in .env and the user has scanned the QR via the wa-daemon.

Root cause: src/index.ts never calls initWhatsApp(). The in-process whatsapp-web.js Client defined in src/whatsapp.ts is imported and exported by the module but never instantiated by the main bot. As a result, when /wa runs and reaches getWaChats(), the client reference is null and the function throws WhatsApp not connected. Setting WHATSAPP_ENABLED=true in .env has no effect because nothing reads it.

The setup wizard offers WhatsApp as a feature and configures the daemon, but the main bot never wires the in-process client into its startup sequence.

Fix

src/index.ts:

  • Import WHATSAPP_ENABLED from config, initWhatsApp from whatsapp, notifyWhatsAppIncoming from bot
  • After the dashboard starts on the main agent, if WHATSAPP_ENABLED is true, call initWhatsApp() with a callback that forwards incoming WhatsApp messages to Telegram via notifyWhatsAppIncoming
  • Errors during init are logged but never block bot startup, so a broken WhatsApp setup cannot bring the whole bot down

package.json / package-lock.json:

  • Bump whatsapp-web.js 1.34.6 to 1.34.7 to fix the upstream Cannot read properties of undefined (reading 'waitForChatLoading') TypeError when calling fetchMessages to open a chat. WhatsApp Web internals shifted recently and 1.34.7 catches up.

Verified working end to end on macOS Apple Silicon

  • /wa lists chats with unread counts
  • Selecting a chat by number reads message history
  • r <text> and r <num> <text> quick-reply both deliver to WhatsApp
  • Incoming WhatsApp messages produce a Telegram 📱 Name — new message ping
  • Bot still starts cleanly when WHATSAPP_ENABLED is unset or false (logs "WhatsApp bridge disabled")
  • Bot still starts cleanly when WhatsApp init throws (logs error, /wa returns the existing not-connected message)

The /wa command was always responding 'WhatsApp not connected' because
src/index.ts never called initWhatsApp() — the in-process Client was
imported and exported but never instantiated. Result: WHATSAPP_ENABLED
in .env had no effect, and the entire WhatsApp bridge feature was
silently broken in v1.1.0.

Changes:
- src/index.ts: import WHATSAPP_ENABLED, initWhatsApp,
  notifyWhatsAppIncoming. After dashboard starts on the main agent,
  if WHATSAPP_ENABLED is true, call initWhatsApp() with a callback
  that forwards incoming WhatsApp messages to Telegram via
  notifyWhatsAppIncoming. Errors are logged but never block bot
  startup, so a broken WhatsApp setup cannot bring the bot down.
- package.json / package-lock.json: bump whatsapp-web.js 1.34.6 to
  1.34.7 to fix 'waitForChatLoading' TypeError when opening a chat
  via fetchMessages (Meta updated WhatsApp Web internals).

Verified working: /wa lists chats, opening by number reads history,
'r <text>' sends replies, incoming WhatsApp messages produce a
Telegram notification ping.
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