# Cài đặt Chat Channels cho OpenClaw

Sau khi cài đặt chúng ta đã có thể chat với OpenClaw qua giao diện web. Nhưng để thuận tiện OpenClaw cung cấp rất nhiều các kênh chat khác nhau như Telegram, Discord, Zalo...

# Cài Discord Bot cho OpenClaw

### Mục tiêu

Cấu hình Discord Bot để chat với OpenClaw qua DM.

Kết quả sau khi cài:

- Có một Bot Discord online.
- Có thể DM và nhận phản hồi từ Bot.

### Điều kiện trước khi làm

OpenClaw Gateway đã chạy bằng Docker/Portainer.

Đã cài đặt backend bất kỳ, có thể chat với OpenClaw thông qua web hoặc nền tảng khác.

## Tóm tắt nhanh

Các bước chính:

1. Tạo Discord Application
2. Tạo Bot và copy Bot Token
3. Bật Message Content Intent + Server Members Intent
4. Cấu hình Installation → Guild Install → bot + applications.commands
5. Invite bot vào server riêng
6. Thêm DISCORD\_BOT\_TOKEN vào Portainer
7. Cài plugin @openclaw/discord
8. Bật channels.discord trong OpenClaw config
9. Restart OpenClaw Gateway
10. Pair Discord DM với OpenClaw
11. Chat với OpenClaw qua DM bot

### Tạo Discord Application và Bot

Vào [Discord Developer Portal](https://discord.com/developers/applications) &gt; New Application &gt; Đặt tên *(Ex: OClaw)*

Ở menu bên trái chọn **Bot** &gt; Reset Token &gt; Copy Bot Token *(lưu lại token này để dùng sau)*

### Bật Gateway Intents cho Bot

Vẫn trong tab **Bot** &gt; Privileged Gateway Intents

- Message Content Intent: ON → Cho phép bot đọc nội dung tin nhắn.
- Server Members Intent: ON → Hữu ích nếu sau này muốn allowlist theo user/role/server.
- Presence Intent: OFF

**Save Changes**

### Cấu hình Installation

Chuyển sang tab **Installation**

**Installation Contexts**

- Guild Install: ON
- User Install: optional

**Install Link**

- Chọn Discord Provided Link

**Default Install Settings**

- Với Guild Install, thêm scopes:
    - `<span class="editor-theme-code">bot</span>`
    - `<span class="editor-theme-code">applications.commands</span>`

Khi chọn bot, Discord sẽ hiện phần quyền cho bot. Chọn tối thiểu:

- `<span class="editor-theme-code">View Channels</span>`
- `<span class="editor-theme-code">Send Messages</span>`
- `<span class="editor-theme-code">Read Message History</span>`
- `<span class="editor-theme-code">Embed Links</span>`
- `<span class="editor-theme-code">Attach Files</span>`
- `<span class="editor-theme-code">Add Reactions</span>`

Nếu muốn dùng thread/forum channel, bật thêm:

- `<span class="editor-theme-code">Send Messages in Threads</span>`

Sau đó copy **Install Link**, mở bằng trình duyệt và mời bot vào server riêng của bạn.

### Bật Developer Mode và lấy ID

Trong Discord app &gt; User Settings &gt; Advanced &gt; Developer Mode: ON

Lấy các ID cần thiết:

- Right-click server icon → Copy Server ID
- Right-click avatar của bạn → Copy User ID

Các giá trị cần có:

- DISCORD\_BOT\_TOKEN=... // *→ Không gửi cho người khác.*
- DISCORD\_SERVER\_ID=... // *→ Dùng nếu sau này muốn cấu hình bot trong server/channel.*
- DISCORD\_USER\_ID=... // *→ Dùng để kiểm soát ai được phép chat với bot.*

### Thêm Discord Bot Token vào Portainer stack

Vào Portainer &gt; Stacks &gt; openclaw &gt; Editor

Trong phần Environment variables, thêm:

- `<span class="editor-theme-code">DISCORD_BOT_TOKEN=token_bot_discord_của_bạn</span>`

Trong compose, ở service openclaw-gateway, thêm vào phần environment:

- `<span class="editor-theme-code">DISCORD_BOT_TOKEN: ${DISCORD_BOT_TOKEN}</span>`

Bấm **Update the stack**

### Test Discord Bot Token

Chạy:

```
docker exec -it openclaw-gateway sh -lc '
node -e "
fetch(\"https://discord.com/api/v10/users/@me\", {
  headers: { Authorization: \"Bot \" + process.env.DISCORD_BOT_TOKEN }
})
.then(async r => {
  console.log(\"status\", r.status);
  console.log(await r.text());
})
.catch(e => {
  console.error(e);
  process.exit(1);
});
"
'
```

Kết quả mong muốn:

```
status 200
{"id":"...","username":"...","bot":true,...}
```

Nếu ra:

```
status 401
```

thì token sai hoặc đã bị reset.

### Cài plugin Discord cho OpenClaw

Chỉ cấu hình `<span class="editor-theme-code">channels.discord</span>` là chưa đủ. OpenClaw cần plugin Discord.

Kiểm tra trạng thái channel:

```
docker exec -it openclaw-gateway \
  node dist/index.js channels list --all | grep -i discord
```

Nếu thấy:

```
Discord: not installed, configured, disabled, run openclaw plugins install @openclaw/discord
```

thì cài plugin:

```
docker exec -it openclaw-gateway \
  node dist/index.js plugins install @openclaw/discord
```

Sau đó kiểm tra lại:

```
docker exec -it openclaw-gateway \
  node dist/index.js channels list --all | grep -i discord
```

### Bật Discord channel trong OpenClaw config

Ghi config Discord:

```
docker exec -it openclaw-gateway \
  node dist/index.js config set --batch-json '[
    {
      "path": "channels.discord.enabled",
      "value": true
    },
    {
      "path": "channels.discord.token",
      "value": {
        "source": "env",
        "provider": "default",
        "id": "DISCORD_BOT_TOKEN"
      }
    },
    {
      "path": "channels.discord.groupPolicy",
      "value": "allowlist"
    }
  ]'
```

Kiểm tra config:

```
docker exec -it openclaw-gateway \
  node dist/index.js config get channels.discord --json
```

Kết quả mong muốn:

```
{
  "enabled": true,
  "token": {
    "source": "env",
    "provider": "default",
    "id": "DISCORD_BOT_TOKEN"
  },
  "groupPolicy": "allowlist"
}
```

Restart gateway:

```
docker restart openclaw-gateway
```

Bot trên Discord nên chuyển sang **Online** là thành công.

## Pair Discord DM với OpenClaw

Mở DM với bot Discord, gửi `<span class="editor-theme-code">hello</span>` hoặc `<span class="editor-theme-code">pair</span>` &gt; Bot sẽ trả về pairing code.

Trên Docker host, kiểm tra pairing request:

```
docker exec -it openclaw-gateway \
  node dist/index.js pairing list discord
```

Approve pairing code:

```
docker exec -it openclaw-gateway \
  node dist/index.js pairing approve discord <CODE>
```

Sau đó quay lại Discord DM và test:

```
Bạn đang hoạt động chưa? Trả lời ngắn gọn.
```

Nếu bot trả lời được, Discord DM đã hoạt động.

### Phụ lục

#### Bot vẫn Offline

Kiểm tra:

```
docker exec -it openclaw-gateway \
  node dist/index.js channels list --all | grep -i discord
```

Nếu thấy `<span class="editor-theme-code">not installed</span>`, cài plugin:

```
docker exec -it openclaw-gateway \
  node dist/index.js plugins install @openclaw/discord
```

Restart:

```
docker restart openclaw-gateway
```

#### Token rỗng trong container

Kiểm tra:

```
docker exec -it openclaw-gateway sh -lc '
if [ -n "$DISCORD_BOT_TOKEN" ]; then
  echo "OK"
else
  echo "EMPTY"
fi
'
```

Nếu ra `<span class="editor-theme-code">EMPTY</span>`, kiểm tra lại Portainer Environment variables và compose `<span class="editor-theme-code">environment:</span>`.

#### Token sai

Test:

```
docker exec -it openclaw-gateway sh -lc '
node -e "
fetch(\"https://discord.com/api/v10/users/@me\", {
  headers: { Authorization: \"Bot \" + process.env.DISCORD_BOT_TOKEN }
})
.then(async r => {
  console.log(\"status\", r.status);
  console.log(await r.text());
})
"
'
```

Nếu ra `<span class="editor-theme-code">401</span>`, reset token trong Discord Developer Portal rồi cập nhật lại Portainer.

#### Bot online nhưng không phản hồi DM

Kiểm tra pairing:

```
docker exec -it openclaw-gateway \
  node dist/index.js pairing list discord
```

Approve pairing code nếu có:

```
docker exec -it openclaw-gateway \
  node dist/index.js pairing approve discord <CODE>
```

Xem log Discord:

```
docker exec -it openclaw-gateway \
  node dist/index.js channels logs --channel discord --lines 200
```

# Cài Telegram Bot cho OpenClaw

### Mục tiêu

Cấu hình Telegram Bot để chat với OpenClaw qua DM.

Luồng hoạt động:

- Bạn
- → Telegram DM Bot
- → OpenClaw Gateway
- → Model
- → trả lời lại Telegram

### Tóm tắt nhanh

Các bước chính:

1. Tạo bot bằng @BotFather
2. Copy Telegram Bot Token
3. Thêm TELEGRAM\_BOT\_TOKEN vào Portainer
4. Cài plugin @openclaw/telegram nếu chưa có
5. Bật channels.telegram
6. Restart OpenClaw Gateway
7. DM bot để lấy pairing code
8. Approve pairing code
9. Chat với OpenClaw qua Telegram DM

### Tạo Telegram Bot

Mở Telegram, tìm: `<span class="editor-theme-code">@BotFather</span>` &gt; gửi `<span class="editor-theme-code">/newbot</span>`

Làm theo hướng dẫn:

- Đặt tên hiển thị cho bot
- Đặt username cho bot, bắt buộc kết thúc bằng bot

Sau khi tạo xong, BotFather sẽ trả về token dạng `<span class="editor-theme-code">1234567890:ABCDEF_xxxxxxxxxxxxxxxxx</span>`

Lưu token lại. Không gửi token cho người khác.

### Thêm token vào Portainer Stack

**Portainer** → Stacks → openclaw → Editor

Trong phần Environment variables, thêm:

- `<span class="editor-theme-code">TELEGRAM_BOT_TOKEN=token_telegram_bot_của_bạn</span>`

Trong compose, thêm vào service openclaw-gateway.environment:

- `<span class="editor-theme-code">TELEGRAM_BOT_TOKEN: ${TELEGRAM_BOT_TOKEN}</span>`

**Update the stack.**

### Kiểm tra token trong container

Chạy:

```
docker exec -it openclaw-gateway sh -lc '
if [ -n "$TELEGRAM_BOT_TOKEN" ]; then
  echo "OK: TELEGRAM_BOT_TOKEN is set"
else
  echo "ERROR: TELEGRAM_BOT_TOKEN is empty"
fi
'
```

Test token với Telegram API:

```
docker exec -it openclaw-gateway sh -lc '
node -e "
fetch(\"https://api.telegram.org/bot\" + process.env.TELEGRAM_BOT_TOKEN + \"/getMe\")
.then(async r => {
  console.log(\"status\", r.status);
  console.log(await r.text());
})
.catch(e => {
  console.error(e);
  process.exit(1);
});
"
'
```

Kết quả đúng:

```
status 200
{"ok":true,...}
```

Nếu `<span class="editor-theme-code">ok:false</span>` hoặc lỗi `<span class="editor-theme-code">401</span>`, token sai hoặc đã bị reset.

### Cài plugin Telegram

Kiểm tra trạng thái Telegram channel:

```
docker exec -it openclaw-gateway \
  node dist/index.js channels list --all | grep -i telegram
```

Nếu thấy `<span class="editor-theme-code">not installed</span>`, cài plugin:

```
docker exec -it openclaw-gateway \
  node dist/index.js plugins install @openclaw/telegram
```

Kiểm tra lại:

```
docker exec -it openclaw-gateway \
  node dist/index.js channels list --all | grep -i telegram
```

## Bật Telegram channel

Ghi config Telegram:

```
docker exec -it openclaw-gateway \
  node dist/index.js config set --batch-json '[
    {
      "path": "channels.telegram.enabled",
      "value": true
    },
    {
      "path": "channels.telegram.dmPolicy",
      "value": "pairing"
    },
    {
      "path": "channels.telegram.groupPolicy",
      "value": "allowlist"
    }
  ]'
```

Ý nghĩa:

```
// Người nhắn DM lần đầu cần pairing code.
dmPolicy = pairing 

// Không cho bot hoạt động tự do trong group nếu chưa cấu hình.
groupPolicy = allowlist
```

Restart OpenClaw

`<span class="editor-theme-code">docker restart openclaw-gateway</span>`

### Pair Telegram DM

Mở Telegram, nhắn cho bot `<span class="editor-theme-code">/start</span>` hoặc `<span class="editor-theme-code">hello</span>`

Bot sẽ trả về pairing code.

Trên Docker host, xem pairing request:

```
docker exec -it openclaw-gateway \
  node dist/index.js pairing list telegram
```

Approve code:

```
docker exec -it openclaw-gateway \
  node dist/index.js pairing approve telegram <CODE>
```

Ví dụ:

```
docker exec -it openclaw-gateway \
  node dist/index.js pairing approve telegram ABCD1234
```

Pairing code thường hết hạn sau khoảng 1 giờ.

### Test chat

Trong Telegram DM với bot, gửi tin nhắn bất kỳ.

Nếu bot trả lời được, Telegram đã hoạt động.

### Troubleshooting

#### Bot không trả lời

Kiểm tra channel:

```
docker exec -it openclaw-gateway \
  node dist/index.js channels list --all | grep -i telegram
```

Xem log channel:

```
docker exec -it openclaw-gateway \
  node dist/index.js channels logs --channel telegram --lines 200
```

#### Token rỗng

```
docker exec -it openclaw-gateway sh -lc '
if [ -n "$TELEGRAM_BOT_TOKEN" ]; then echo OK; else echo EMPTY; fi
'
```

Nếu ra `<span class="editor-theme-code">EMPTY</span>`, kiểm tra lại Portainer Environment variables và compose.

#### Token sai

Test lại Telegram API:

```
docker exec -it openclaw-gateway sh -lc '
node -e "
fetch(\"https://api.telegram.org/bot\" + process.env.TELEGRAM_BOT_TOKEN + \"/getMe\")
.then(async r => {
  console.log(\"status\", r.status);
  console.log(await r.text());
})
"
'
```

Nếu response có lỗi `<span class="editor-theme-code">401</span>`, tạo/reset token trong BotFather rồi cập nhật lại Portainer.

#### Bot trả pairing code nhưng approve xong vẫn không trả lời

Kiểm tra pairing:

```
docker exec -it openclaw-gateway \
  node dist/index.js pairing list telegram
```

Xem log:

```
docker logs --tail 160 openclaw-gateway | grep -iE "telegram|pairing|error"
```