Agent whatsapp

From agent-messenger/agent-messenger · View interactive version

Agent whatsapp is an AI agent skill from the GitHub repository agent-messenger/agent-messenger. Interact with WhatsApp - send messages, read chats, manage conversations

How to use this skill

Skill file

Agent WhatsApp

A Baileys-backed WhatsApp CLI for AI agents. Links as a companion device via QR code or pairing code, so your phone stays connected. Each command connects on demand and disconnects when done.

Use one of these entrypoints:

Key Concepts

Before diving in, a few things about WhatsApp's architecture:

Quick Start

# Link as companion device via QR code (scan with your phone)
agent-whatsapp auth login --qr

# Or link via pairing code (enter the code on your phone)
agent-whatsapp auth login --phone +1234567890

# List chats
agent-whatsapp chat list

# Send a message
agent-whatsapp message send +1234567890 "Hello from agent-whatsapp"

Authentication Flow

WhatsApp supports two authentication methods. Both register the CLI as a companion (linked) device, so your phone session is never affected.

Agent Behavior (MANDATORY)

When a command fails because no account is configured, the agent MUST drive the auth flow itself. Never tell the user to run commands. The agent runs everything.

Step 1: Check for existing accounts

agent-whatsapp auth list

If accounts exist, use agent-whatsapp auth use <account-id> and retry the original command.

Step 2: If no accounts, start QR login (preferred)

agent-whatsapp auth login --qr
# -> QR code displayed in terminal + opened in browser
# -> {"next_action":"scan_qr","qr_url":"...","message":"Scan with WhatsApp on your phone"}

Tell the user to scan the QR code with WhatsApp: Settings > Linked Devices > Link a Device > scan the QR code.

Alternative: Pairing code login

If QR scanning is not possible (e.g. headless environment), ask for the phone number and use pairing code:

agent-whatsapp auth login --phone +1234567890
# -> {"pairing_code":"A1B2-C3D4","message":"Enter this code in WhatsApp > Linked Devices > Link with phone number"}

Tell the user the pairing code and ask them to enter it on their phone.

Step 3: Wait for confirmation

The CLI waits until the user confirms on their phone. Once confirmed:

# -> {"authenticated":true,...}

Step 4: Retry the original command

After successful auth, immediately execute whatever the user originally asked for.

Common Auth Commands

agent-whatsapp auth status              # Check current auth state
agent-whatsapp auth status --account <id>  # Check specific account
agent-whatsapp auth list                # List all linked accounts
agent-whatsapp auth use <id>            # Switch active account
agent-whatsapp auth logout              # Unlink current account
agent-whatsapp auth logout --account <id>  # Unlink specific account

Memory

The agent maintains a ~/.config/agent-messenger/MEMORY.md file as persistent memory across sessions. This is agent-managed. The CLI does not read or write this file. Use the Read and Write tools to manage your memory file.

Reading Memory

At the start of every task, read ~/.config/agent-messenger/MEMORY.md using the Read tool to load any previously discovered chat IDs, contact names, and preferences.

Writing Memory

After discovering useful information, update ~/.config/agent-messenger/MEMORY.md using the Write tool. Write triggers include:

When writing, include the complete file content. The Write tool overwrites the entire file.

What to Store

What NOT to Store

Never store auth credentials, session keys, or any sensitive data. Never store full message content (just IDs and chat context).

Handling Stale Data

If a memorized JID returns an error, remove it from MEMORY.md. Don't blindly trust memorized data. Prefer re-listing over using a memorized JID that might be stale.

Format / Example

# Agent Messenger Memory

## WhatsApp Account

- Phone: +1234567890
- JID: 1234567890@s.whatsapp.net

## Chats

- `1234567890@s.whatsapp.net` - Alice (1:1)
- `5678901234@s.whatsapp.net` - Bob (1:1)
- `123456789-123345@g.us` - Project Team (group, 8 members)
- `987654321-654321@g.us` - Family (group, 5 members)

## Aliases

- "alice" -> `1234567890@s.whatsapp.net`
- "project" -> `123456789-123345@g.us` (Project Team)
- "family" -> `987654321-654321@g.us` (Family group)

## Notes

- User prefers --pretty output
- Project Team is the most frequently used chat

Memory lets you skip repeated chat list calls. When you already know a JID from a previous session, use it directly.

Commands

Auth Commands

# Link as companion device via QR code (no phone number needed)
agent-whatsapp auth login --qr

# Link as companion device via pairing code
agent-whatsapp auth login --phone +1234567890

# Check auth status
agent-whatsapp auth status
agent-whatsapp auth status --account <id>

# List linked accounts
agent-whatsapp auth list

# Switch active account
agent-whatsapp auth use <id>

# Unlink account
agent-whatsapp auth logout
agent-whatsapp auth logout --account <id>

Whoami Command

# Show current authenticated user
agent-whatsapp whoami
agent-whatsapp whoami --pretty
agent-whatsapp whoami --account <account-id>

Output includes the authenticated user's identity information.

Chat Commands

# List chats (sorted by most recent activity)
agent-whatsapp chat list
agent-whatsapp chat list --limit 50
agent-whatsapp chat list --account <id>

# Search chats by name
agent-whatsapp chat search "project"
agent-whatsapp chat search "project" --limit 10
agent-whatsapp chat search "project" --account <id>

Output includes:

Message Commands

# List messages in a chat
agent-whatsapp message list <chat> --limit 20
agent-whatsapp message list +1234567890 --limit 50
agent-whatsapp message list 123456789-123345@g.us --limit 10
agent-whatsapp message list +1234567890 --limit 20 --account <id>

# Send a text message
agent-whatsapp message send <chat> <text>
agent-whatsapp message send +1234567890 "Hello!"
agent-whatsapp message send 123456789-123345@g.us "Hello team!"
agent-whatsapp message send +1234567890 "Hello!" --account <id>

# Edit one of your own messages (within ~15 minutes)
agent-whatsapp message edit <chat> <message-id> <text>
agent-whatsapp message edit +1234567890 ABC123DEF456 "Updated text"

# React to a message
agent-whatsapp message react <chat> <message-id> <emoji>
agent-whatsapp message react +1234567890 ABC123DEF456 "πŸ‘"
agent-whatsapp message react +1234567890 ABC123DEF456 "πŸ‘" --from-me
agent-whatsapp message react +1234567890 ABC123DEF456 "πŸ‘" --account <id>

The <chat> argument accepts:

The --from-me flag on message react indicates the target message was sent by you (outgoing). Without it, the reaction targets an incoming message.

Output Format

JSON (Default)

All commands output JSON by default for AI consumption:

{
  "jid": "1234567890@s.whatsapp.net",
  "name": "Alice",
  "unread_count": 3,
  "last_message": {
    "id": "ABC123DEF456",
    "text": "See you tomorrow!",
    "from": "1234567890@s.whatsapp.net",
    "timestamp": 1705312200
  }
}

Pretty (Human-Readable)

Use --pretty flag for formatted output:

agent-whatsapp chat list --pretty

Global Options

| Option | Description |

| --------------- | ------------------------------------------ |

| --pretty | Human-readable output instead of JSON |

| --account <id>| Use a specific account for this command |

Common Patterns

Check unread messages

# List chats to see unread counts
agent-whatsapp chat list --limit 20

# Read messages from a specific chat
agent-whatsapp message list +1234567890 --limit 10

Send a message to a contact

# By phone number (simplest)
agent-whatsapp message send +1234567890 "Hey, are we still on for tomorrow?"

# By JID
agent-whatsapp message send 1234567890@s.whatsapp.net "Hey, are we still on for tomorrow?"

Send a message to a group

# Find the group first
agent-whatsapp chat search "Project Team"

# Send to the group JID
agent-whatsapp message send 123456789-123345@g.us "Status update: deployment complete."

React to the latest message

# Get the latest message ID
agent-whatsapp message list +1234567890 --limit 1

# React to it
agent-whatsapp message react +1234567890 <message-id> "πŸ‘"

Edit one of your own messages

# Get the message ID of a message you sent
agent-whatsapp message list +1234567890 --limit 5

# Edit it (only your own messages, within ~15 minutes)
agent-whatsapp message edit +1234567890 <message-id> "Updated text"

The SDK exposes the same capability via WhatsAppClient.editMessage:

import { WhatsAppClient } from 'agent-messenger/whatsapp'

const client = await new WhatsAppClient().login()
const sent = await client.sendMessage('+1234567890', 'Deploying…')
await client.editMessage('+1234567890', sent.id, 'Deployed βœ…')

Error Handling

All commands return consistent error format:

{
  "error": "No WhatsApp account linked. Run: agent-whatsapp auth login --qr"
}

Common errors:

Notes

Troubleshooting

`agent-whatsapp: command not found`

agent-whatsapp is NOT the npm package name. The npm package is agent-messenger.

If the package is installed globally, use agent-whatsapp directly:

agent-whatsapp chat list --pretty

If the package is NOT installed, use --package to install and run:

npx -y --package agent-messenger agent-whatsapp chat list --pretty
bunx --package agent-messenger agent-whatsapp chat list --pretty
pnpm dlx --package agent-messenger agent-whatsapp chat list --pretty

NEVER run npx agent-whatsapp, bunx agent-whatsapp, or pnpm dlx agent-whatsapp without --package agent-messenger. It will fail or install a wrong package since agent-whatsapp is not the npm package name.

QR code not scanning

  1. Make sure WhatsApp is open on your phone
  2. Go to Settings > Linked Devices > Link a Device
  3. Point your phone camera at the QR code in the terminal or browser
  4. If the QR code expires, run auth login --qr again for a fresh code
  5. If QR scanning fails, try pairing code: auth login --phone <number>

Pairing code not working

  1. Make sure WhatsApp is open on your phone
  2. Go to Settings > Linked Devices > Link a Device
  3. Choose "Link with phone number instead" if you see a QR code
  4. Enter the pairing code shown in the terminal
  5. If the code expires, run auth login --phone <number> again for a fresh code

Empty chat list after linking a new device

WhatsApp companion devices receive their chat list from the primary phone β€” there's no server API to fetch it directly. On a freshly linked device, chat list may return only group chats (or nothing at all) for individual/DM conversations.

This is a WhatsApp protocol limitation, not a bug.

Connection drops

WhatsApp may disconnect linked devices that are inactive for extended periods. If commands start failing:

# Check if still linked
agent-whatsapp auth status

# Re-link if needed
agent-whatsapp auth login --qr
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.