Agent LINE
A TypeScript CLI tool that enables AI agents and humans to interact with LINE through a simple command interface. Features QR code login and email/password authentication for the LINE desktop client protocol.
Key Concepts
Before diving in, a few things about LINE's architecture:
- MIDs = LINE's unique identifiers. Format varies by entity type:
- for users (e.g.,
u0123456789abcdef0123456789abcdef
)
- for groups (e.g.,
c0123456789abcdef0123456789abcdef
)
- for rooms (e.g.,
r0123456789abcdef0123456789abcdef
)
- QR code login = the primary authentication method. The CLI generates a QR code URL, you scan it with your phone, and the session is established.
- Email/password login = an alternative when QR scanning isn't practical.
- Auth token reuse = after initial login, the CLI stores an auth token locally. Subsequent commands reuse it without re-authentication.
- Device types = the CLI registers as by default — a secondary device that coexists with the LINE desktop app. Override with :
- (default) — secondary device, V3-capable, won't kick LINE desktop
- / — replaces the desktop session (kicks LINE desktop app)
- — secondary but limited API (no V3 token refresh)
- Chat ID = an MID that identifies a conversation. Use to discover them.
Quick Start
bash
# QR code login (default, recommended)
agent-line auth login
# List chat rooms
agent-line chat list --pretty
# List messages in a chat
agent-line message list <chat-id>
# Send a message
agent-line message send <chat-id> "Hi"
Authentication
LINE offers three authentication methods:
Method 1: QR Code Login (Recommended)
The default and most common method. No credentials needed.
The CLI prints a QR code URL to stderr. The user scans it with the LINE app on their phone. Once scanned, authentication completes automatically.
Flow:
- CLI requests a QR code session from LINE's server
- A URL is printed to stderr (e.g.,
https://line.me/R/au/q/...
)
- User scans the QR code with their LINE mobile app
- CLI detects the scan and completes login
- Auth token is stored locally for future use
Method 2: Email/Password Login
For environments where QR scanning isn't possible:
bash
agent-line auth login --email user@example.com --password pass123
Method 3: Token Login
If you already have a valid auth token:
bash
agent-line auth login --token <auth-token>
Device Override
To specify a device type explicitly:
bash
agent-line auth login --device DESKTOPMAC
agent-line auth login --device DESKTOPWIN
Agent Behavior (MANDATORY)
When a command fails because no account is configured, the agent MUST drive the auth flow itself:
Step 1: Check auth status
If authenticated, retry the original command.
Step 2: Attempt login
Possible responses:
{"authenticated": true, ...}
→ Success. Retry original command.
{"next_action": "scan_qr", "qr_url": "...", "qr_html_path": "/tmp/line-qr-xxx.html", ...}
→ QR code has been generated. The CLI attempts to open it in the user's browser automatically. If it didn't open, run (macOS) to show the QR code. Tell the user to scan the QR code with the LINE mobile app. The command blocks until the user scans — once scanned, it outputs {"authenticated": true, ...}
.
{"error": "not_connected", ...}
→ Network issue. Check connectivity and retry.
{"error": "not_authenticated", ...}
→ Credentials expired. Re-run .
Important: QR login works in both interactive and non-interactive (agent) sessions. The CLI generates an HTML page with the QR code and opens it in the user's default browser. No TTY is required.
Step 3: Retry the original command
After successful auth, immediately execute whatever the user originally asked for.
IMPORTANT: NEVER guide the user to open a web browser, use DevTools, or manually copy tokens. Always use
.
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
and
tools to manage your memory file.
Reading Memory
At the
start of every task, read
~/.config/agent-messenger/MEMORY.md
using the
tool to load any previously discovered chat IDs, friend names, and preferences.
- If the file doesn't exist yet, that's fine. Proceed without it and create it when you first have useful information to store.
- If the file can't be read (permissions, missing directory), proceed without memory. Don't error out.
Writing Memory
After discovering useful information, update
~/.config/agent-messenger/MEMORY.md
using the
tool. Write triggers include:
- After login, remember the from the output
- After discovering chat IDs and participant names (from )
- After the user gives you an alias or preference ("call this the work chat", "my group chat with Alice is X")
- After discovering chat structure (group chats, 1:1 chats)
When writing, include the
complete file content. The
tool overwrites the entire file.
What to Store
- Account ID (MID) from login
- Chat IDs with participant names or display names
- User-given aliases ("work chat", "family group")
- Commonly referenced chat IDs
- Any user preference expressed during interaction
What NOT to Store
Never store tokens, passwords, credentials, or any sensitive data. Never store full message content (just IDs and chat context). Never store auth tokens.
Handling Stale Data
If a memorized chat ID returns an error, remove it from
. Don't blindly trust memorized data. Verify when something seems off. Prefer re-listing over using a memorized ID that might be stale.
Format / Example
markdown
# Agent Messenger Memory
## LINE Account
- Account ID: `u0123456789abcdef0123456789abcdef`
- Device type: DESKTOPMAC
## Chat Rooms
- `c9876543210abcdef9876543210abcdef` - Work group chat (Alice, Bob, Charlie)
- `u1111111111abcdef1111111111abcdef` - 1:1 with Alice
- `c2222222222abcdef2222222222abcdef` - Family group
## Aliases
- "work" -> `c9876543210abcdef9876543210abcdef` (Work group chat)
- "alice" -> `u1111111111abcdef1111111111abcdef` (1:1 with Alice)
## Notes
- User prefers --pretty output
- Work chat is the most frequently used
Memory lets you skip repeated
calls. When you already know a chat ID from a previous session, use it directly.
Commands
Auth Commands
bash
# QR code login (default)
agent-line auth login
agent-line auth login --pretty
# Email/password login
agent-line auth login --email <email> --password <password>
agent-line auth login --email <email> --password <password> --pretty
# Token login
agent-line auth login --token <auth-token>
agent-line auth login --token <auth-token> --pretty
# Device override
agent-line auth login --device DESKTOPMAC
agent-line auth login --device DESKTOPWIN
# Check auth status
agent-line auth status
agent-line auth status --account <account-id>
agent-line auth status --pretty
# List all authenticated accounts
agent-line auth list
agent-line auth list --pretty
# Switch active account
agent-line auth use <account-id>
agent-line auth use <account-id> --pretty
# Logout
agent-line auth logout
agent-line auth logout <account-id>
agent-line auth logout --pretty
Whoami Command
bash
# Show current authenticated user
agent-line whoami
agent-line whoami --pretty
Output includes:
- - your LINE MID
- - your display name
- - your status message
- - your profile picture URL
Friend Commands
bash
# List all LINE friends
agent-line friend list
agent-line friend list --pretty
Output includes:
- - friend's MID
- - friend's display name
- - friend's status message
- - friend's profile picture URL
Chat Commands
bash
# List all chat rooms
agent-line chat list
agent-line chat list --pretty
Output includes:
- - MID of the chat room
- - chat type (user, group, room, square)
- - chat name
- - number of members (groups only)
Message Commands
bash
# List messages in a chat room
agent-line message list <chat-id>
agent-line message list <chat-id> -n 50
agent-line message list <chat-id> --pretty
# Send a text message
agent-line message send <chat-id> "Hello world"
agent-line message send <chat-id> "Hello world" --pretty
Message List Output
Each message includes:
- - unique message identifier
- - message type (text, image, sticker, etc.)
- - sender's MID
- - message text content
- - Unix timestamp (milliseconds)
Output Format
JSON (Default)
All commands output JSON by default for AI consumption:
json
{
"chat_id": "c0123456789abcdef0123456789abcdef",
"type": "group",
"display_name": "Alice, Bob",
"member_count": 3,
"unread_count": 5,
"last_message": {
"author_id": "u1111111111abcdef1111111111abcdef",
"text": "Hello everyone!",
"sent_at": 1705312200000
}
}
Pretty (Human-Readable)
Use
flag for formatted output:
bash
agent-line chat list --pretty
Global Options
| Option | Description |
|---|
| Human-readable output instead of JSON |
Common Patterns
See
references/common-patterns.md
for typical AI agent workflows.
Templates
See
directory for runnable examples:
- - Send messages with error handling
- - Monitor a chat for new messages
- - Generate chat summary
Error Handling
All commands return consistent error format:
json
{
"error": "not_connected",
"message": "Not connected to LINE. Run:\n agent-line auth login"
}
Common errors:
- - not authenticated. Run .
- - credentials expired or invalid. Re-run to get a fresh session.
- - QR code expired before user scanned it. Run again to generate a new QR code.
- - the stored auth token is no longer valid. Re-authenticate with .
- - email/password incorrect or account issue.
- - couldn't reach LINE servers. Check connectivity.
- - too many requests. Wait a moment and retry.
Configuration
Credentials stored in
~/.config/agent-messenger/line-credentials.json
(0600 permissions).
Config format:
json
{
"current_account": "u0123456789abcdef0123456789abcdef",
"accounts": {
"u0123456789abcdef0123456789abcdef": {
"account_id": "u0123456789abcdef0123456789abcdef",
"auth_token": "...",
"device_type": "DESKTOPMAC",
"created_at": "2025-01-15T10:30:00.000Z",
"updated_at": "2025-01-15T10:30:00.000Z"
}
}
}
SDK: Programmatic Usage
is available as a TypeScript SDK for building scripts and automations.
Setup
typescript
import { LineClient } from 'agent-messenger/line'
const client = await new LineClient().login()
Example
typescript
try {
// List chats
const chats = await client.getChats()
// Send a message
if (chats.length === 0) throw new Error('No chats found')
const chatId = chats[0].chat_id
const result = await client.sendMessage(chatId, 'Hello from SDK!')
// Read messages
const messages = await client.getMessages(chatId, { count: 50 })
} finally {
client.close()
}
Full API Reference
See the
LINE SDK documentation for complete method signatures, types, schemas, and examples.
Limitations
- No auto-extraction of credentials (requires interactive login via QR code or email/password)
- E2EE (Letter Sealing) may prevent reading some message content
- No file upload support yet
- No sticker or rich message sending (text only)
- No group creation or management
- No group creation or management commands (list only)
- No reactions or emoji responses
- No message editing or deletion
- No voice/video call support
- Chat IDs are MIDs and not human-readable. Use to discover them.
Troubleshooting
agent-line: command not found
is NOT the npm package name. The npm package is
.
If the package is installed globally, use
directly:
bash
agent-line chat list --pretty
If the package is NOT installed, use
to install and run:
bash
npx -y --package agent-messenger agent-line chat list --pretty
bunx --package agent-messenger agent-line chat list --pretty
pnpm dlx --package agent-messenger agent-line chat list --pretty
Note: If the user prefers a different package runner, use the matching command above.
NEVER run , , or without
--package agent-messenger
. It will fail or install a wrong package since
is not the npm package name.
QR code expired
QR codes have a short TTL. If the user doesn't scan in time:
- Run again to generate a fresh QR code
- Scan promptly with the LINE mobile app
Token expired
If commands start failing with
or
:
- Run to re-authenticate
- The old token is replaced automatically
E2EE messages unreadable
Some chats with Letter Sealing enabled may return empty or encrypted message content. This is a known limitation. The CLI cannot decrypt E2EE messages.
References
- Authentication Guide
- Common Patterns