OpenClaw LINE Channel 完整設定指南
AI

OpenClaw LINE Channel 完整設定指南

2026-02-01

OpenClaw LINE Channel 完整設定指南

本文整理自實際部署經驗,包含官方文件未明確說明的設定細節與常見陷阱。

目錄

  1. 環境需求
  2. LINE Developers Console 設定
  3. OpenClaw LINE Channel 設定
  4. Webhook 設定與測試
  5. 常見問題與陷阱
  6. 完整設定檢查清單

環境需求

  • OpenClaw 2026.1.30 或更新版本
  • Node.js 18+
  • 可公開存取的 HTTPS 端點(用於 Webhook)
  • LINE Developers 帳號

LINE Developers Console 設定

步驟 1:建立 Provider 與 Channel

  1. 前往 LINE Developers Console
  2. 建立 Provider(如果還沒有)
  3. 建立新的 Messaging API Channel
  4. 填寫 Bot 基本資訊

步驟 2:取得認證資訊

在 Channel 設定頁面中取得以下資訊:

項目位置說明
Channel SecretBasic settings32 字元的十六進位字串
Channel Access TokenMessaging API點擊「Issue」產生,為長字串(約 170+ 字元)

⚠️ 重要:Channel Access Token 產生後只會顯示一次,請立即複製保存。

步驟 3:Messaging API 設定

Messaging API 頁籤中:

  • Auto-reply messages:設為 Disabled(讓 OpenClaw 處理回覆)
  • Greeting messages:依需求設定
  • Webhook URL:稍後設定
  • Use webhook:稍後開啟

OpenClaw LINE Channel 設定

方法一:環境變數設定(推薦)

這是最可靠的設定方式,特別是使用 systemd 管理 Gateway 時。

步驟 1:建立環境變數檔案

cat > ~/.openclaw/.env << 'EOF'
LINE_CHANNEL_ACCESS_TOKEN=你的_Channel_Access_Token
LINE_CHANNEL_SECRET=你的_Channel_Secret
EOF

步驟 2:修改 systemd service 檔案

編輯 ~/.config/systemd/user/openclaw-gateway.service,在 [Service] 區段加入:

EnvironmentFile=/home/你的使用者名稱/.openclaw/.env

完整範例:

[Unit]
Description=OpenClaw Gateway
After=network-online.target
Wants=network-online.target

[Service]
ExecStart="/usr/bin/node" "/home/ubuntu/.npm-global/lib/node_modules/openclaw/dist/index.js" gateway --port 18789
Restart=always
RestartSec=5
KillMode=process
EnvironmentFile=/home/ubuntu/.openclaw/.env
Environment=HOME=/home/ubuntu
# ... 其他 Environment 設定 ...

[Install]
WantedBy=default.target

步驟 3:重新載入並啟動

systemctl --user daemon-reload
systemctl --user restart openclaw-gateway

步驟 4:驗證設定

# 檢查日誌是否顯示 LINE provider 啟動
openclaw logs | grep -i "starting LINE"

# 應該看到類似:
# [line] [default] starting LINE provider (你的Bot名稱)

方法二:設定檔設定

~/.openclaw/openclaw.json 中設定:

{
  "channels": {
    "line": {
      "enabled": true,
      "channelAccessToken": "你的_Channel_Access_Token",
      "channelSecret": "你的_Channel_Secret",
      "dmPolicy": "pairing"
    }
  }
}

⚠️ 注意:根據實測,設定檔方式在某些情況下可能無法正確載入,建議優先使用環境變數方式。

方法三:使用檔案路徑

將 Token 和 Secret 存在獨立檔案中:

# 建立 token 檔案
echo "你的_Channel_Access_Token" > ~/.openclaw/line-token.txt
echo "你的_Channel_Secret" > ~/.openclaw/line-secret.txt

# 設定權限
chmod 600 ~/.openclaw/line-token.txt ~/.openclaw/line-secret.txt

~/.openclaw/openclaw.json 中:

{
  "channels": {
    "line": {
      "enabled": true,
      "tokenFile": "/home/ubuntu/.openclaw/line-token.txt",
      "secretFile": "/home/ubuntu/.openclaw/line-secret.txt",
      "dmPolicy": "pairing"
    }
  }
}

Webhook 設定與測試

使用 Cloudflare Tunnel(快速測試)

# 安裝 cloudflared(如果還沒安裝)
# Ubuntu/Debian
curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb -o cloudflared.deb
sudo dpkg -i cloudflared.deb

# 啟動 tunnel
nohup cloudflared tunnel --url http://localhost:18789 > /tmp/cloudflared.log 2>&1 &

# 取得 tunnel URL
sleep 5
grep -o 'https://[^"]*\.trycloudflare\.com' /tmp/cloudflared.log | head -1

設定 LINE Webhook

  1. 前往 LINE Developers Console → 你的 Channel → Messaging API
  2. 設定 Webhook URL
    https://你的tunnel網址.trycloudflare.com/line/webhook
    
  3. 點擊 Verify 按鈕

⚠️ 預期結果:Verify 會顯示 405 Method Not Allowed 錯誤,這是正常的

原因:LINE 的 Verify 功能使用 GET 請求,但 OpenClaw webhook 只接受 POST 請求。實際訊息傳遞使用 POST,所以功能正常。

  1. Use webhook 開關設為 ON

測試訊息

  1. 用 LINE App 加入你的 Bot 為好友
  2. 傳送任意訊息
  3. 首次訊息會收到 Pairing 要求:
    OpenClaw: access not configured.
    Your lineUserId: Uxxxxx...
    Pairing code: XXXXXXXX
    Ask the bot owner to approve with:
    openclaw pairing approve line <code>
    

核准 Pairing

openclaw pairing approve line <你收到的pairing_code>

核准後,再次傳送訊息即可正常對話。


常見問題與陷阱

🚫 陷阱 1:設定檔結構錯誤

錯誤的結構(不會生效):

{
  "channels": {
    "line": {
      "default": {
        "channelAccessToken": "...",
        "channelSecret": "..."
      }
    }
  }
}
{
  "channels": {
    "line": {
      "accounts": {
        "default": {
          "channelAccessToken": "...",
          "channelSecret": "..."
        }
      }
    }
  }
}

正確的結構

{
  "channels": {
    "line": {
      "enabled": true,
      "channelAccessToken": "...",
      "channelSecret": "..."
    }
  }
}

🚫 陷阱 2:Systemd 環境變數不生效

問題:在 ~/.bashrc~/.profile 設定環境變數,但 systemd service 讀不到。

原因:Systemd user service 不會載入 shell 的環境變數。

解決方案

  1. 使用 EnvironmentFile 指令載入 .env 檔案(推薦)
  2. 或在 service 檔案中直接用 Environment= 設定
# 方法 1:使用 EnvironmentFile(推薦)
EnvironmentFile=/home/ubuntu/.openclaw/.env

# 方法 2:直接設定(較不安全,token 會出現在 systemctl status)
Environment="LINE_CHANNEL_ACCESS_TOKEN=..."
Environment="LINE_CHANNEL_SECRET=..."

🚫 陷阱 3:手動啟動的 Gateway 佔用 Port

問題:執行 systemctl --user restart openclaw-gateway 後報錯 Port 18789 is already in use

原因:之前手動執行 openclaw gateway 的進程還在背景運行。

解決方案

# 找出佔用的進程
pgrep -f "openclaw.*gateway"

# 終止進程
kill <PID>

# 或一次性終止所有
pkill -f "openclaw.*gateway"

# 然後重新啟動
systemctl --user restart openclaw-gateway

🚫 陷阱 4:openclaw channels status 顯示警告但實際正常

問題:即使 LINE 正常運作,openclaw channels status 仍顯示:

Warnings:
- line default: LINE channel access token not configured
- line default: LINE channel secret not configured

原因openclaw channels status CLI 指令檢查的是設定檔,不知道有 .env 環境變數。

判斷方式

# 使用 --probe 檢查實際連線狀態
openclaw channels status --probe

# 如果顯示 "works",表示實際上是正常的
# - LINE default: enabled, configured, stopped, mode:webhook, token:config, works
# 或檢查日誌確認 LINE provider 是否成功啟動
openclaw logs | grep -i "starting LINE"

# 成功會顯示 Bot 名稱
# [line] [default] starting LINE provider (你的Bot名稱)

🚫 陷阱 5:LINE Plugin 重複安裝

問題:嘗試手動安裝 LINE plugin 後出現 duplicate plugin id detected 警告。

原因:OpenClaw 2026.1.30 已內建 LINE plugin,手動安裝會造成重複。

解決方案

# 移除手動安裝的 plugin
rm -rf ~/.openclaw/extensions/line

# 重新啟動 Gateway
systemctl --user restart openclaw-gateway

🚫 陷阱 6:Webhook Verify 失敗

問題:LINE Developers Console 的 Verify 按鈕顯示 405 Method Not Allowed

說明:這是預期行為,不是錯誤。

原因

  • LINE Verify 使用 GET 請求
  • OpenClaw webhook 只接受 POST 請求
  • 實際訊息傳遞使用 POST,所以功能正常

驗證方式:直接用 LINE App 傳訊息給 Bot 測試。

🚫 陷阱 7:忘記設定 AI Provider

問題:LINE 訊息收到,但 Bot 回覆錯誤:

⚠️ Agent failed before reply: No API key found for provider "anthropic"

解決方案

# 設定 AI provider(以 Anthropic 為例)
openclaw auth add anthropic
# 輸入你的 API Key

完整設定檢查清單

LINE Developers Console

  • 建立 Messaging API Channel
  • 取得 Channel Secret
  • 產生並複製 Channel Access Token
  • 關閉 Auto-reply messages
  • 設定 Webhook URL(完整路徑含 /line/webhook
  • 開啟 Use webhook

OpenClaw 伺服器

  • 建立 ~/.openclaw/.env 檔案,包含:
    • LINE_CHANNEL_ACCESS_TOKEN=...
    • LINE_CHANNEL_SECRET=...
  • 修改 systemd service 加入 EnvironmentFile=...
  • 執行 systemctl --user daemon-reload
  • 執行 systemctl --user restart openclaw-gateway
  • 確認日誌顯示 starting LINE provider (Bot名稱)
  • 設定 AI provider(如 openclaw auth add anthropic

Webhook 設定

  • 安裝並啟動 Cloudflare Tunnel(或其他 HTTPS 反向代理)
  • 在 LINE Console 設定完整 Webhook URL
  • 開啟 Use webhook 開關
  • 用 LINE App 傳送測試訊息
  • 執行 openclaw pairing approve line <code> 核准存取

驗證指令

# 檢查 Gateway 狀態
systemctl --user status openclaw-gateway

# 檢查 LINE 連線(看 "works")
openclaw channels status --probe

# 檢查日誌
openclaw logs | grep -i line | tail -10

# 檢查設定
openclaw config get channels.line

附錄:快速設定腳本

#!/bin/bash
# OpenClaw LINE Channel 快速設定腳本

# 設定變數(請替換為你的值)
LINE_TOKEN="你的_Channel_Access_Token"
LINE_SECRET="你的_Channel_Secret"

# 建立 .env 檔案
cat > ~/.openclaw/.env << EOF
LINE_CHANNEL_ACCESS_TOKEN=${LINE_TOKEN}
LINE_CHANNEL_SECRET=${LINE_SECRET}
EOF

echo "✓ 已建立 ~/.openclaw/.env"

# 備份並修改 systemd service
SERVICE_FILE=~/.config/systemd/user/openclaw-gateway.service
if [ -f "$SERVICE_FILE" ]; then
    cp "$SERVICE_FILE" "${SERVICE_FILE}.bak"
    
    # 檢查是否已有 EnvironmentFile
    if ! grep -q "EnvironmentFile=" "$SERVICE_FILE"; then
        sed -i '/\[Service\]/a EnvironmentFile=/home/'"$USER"'/.openclaw/.env' "$SERVICE_FILE"
        echo "✓ 已修改 systemd service"
    else
        echo "⚠ systemd service 已有 EnvironmentFile 設定"
    fi
fi

# 重新載入並重啟
systemctl --user daemon-reload
systemctl --user restart openclaw-gateway

echo "✓ Gateway 已重啟"

# 等待啟動
sleep 5

# 檢查狀態
if openclaw logs | grep -q "starting LINE provider"; then
    echo "✓ LINE provider 啟動成功!"
else
    echo "✗ LINE provider 啟動失敗,請檢查日誌:openclaw logs | grep -i line"
fi

參考資源


最後更新:2026-02-01 基於 OpenClaw 2026.1.30 版本