# Grok Bot

URL: https://swb.one/zh-Hans/docs/setup/grok-bot

Grok Bot 是 xAI 的桌面智能体应用。routine 是一项已保存的任务。这种连接使用 Webhook，也就是 SwB 为启动 routine 而调用的地址。

## 开始之前 [#开始之前]

在受支持的电脑上安装 [Grok Bot](https://x.ai/bot)，并用包含 Grok Bot 的套餐登录。

为 SwB 设置公网 HTTPS 地址和所有者登录。启用 `grok-routine` 模块。选定一个 SwB 项目。使用 OAuth 时，你在浏览器中批准访问。

## 复制设置提示词 [#复制设置提示词]

https://swb.one/prompts/grok-bot.md

```
Read the setup guide at https://swb.one/docs/setup/grok-bot.

Help me connect Grok Bot to SwB. SwB uses a webhook to start a Grok Bot routine. A webhook is a request to a URL when an event occurs.

Check your tools first. If the SwB tools are missing, ask me to open the Grok Bot desktop app. Use `Connect apps > Add a custom connector > Remote HTTPS`. Ask me to enter my public SwB MCP address, such as `https://swb.example.com/mcp`. If Grok Bot opens SwB sign-in, ask me to approve access. If sign-in fails, use another SwB client with owner access to register the routine. If Grok Bot supports bearer keys, set up its key with https://swb.one/prompts/mcp-client.md.

Call `connect(what="grok-bot")` from a client with owner access. Do each step marked "you" yourself. Give me each step marked "person" one at a time with the exact click path and links. Wait for me to say done after each step. Call `connect` again with the same choices to check progress.

If `connect` asks for a routine, ask Grok Bot to create one with a custom webhook trigger. Use the routine prompt that `connect` returns. Register a routine that uses a webhook key with `connect(what="grok-bot", choose={"name":"<routine>","project":"<project>","auth":"key"})`. For a signed trigger, use `auth="signature"`.

Ask me to open the routine's webhook settings. Ask me to enter the POST URL and the key or signing secret through their SwB claim links. Keep the values out of this chat, the routine prompt and tool arguments. Pass `header` only if the trigger uses a custom header.

Run the `send` call that `connect` returns. Call `connect` again to check that Grok accepted the webhook. Use `wait` or `inbox` to collect the reply. Show me the reply if it arrives.

For a message sent to the routine, use the saved routine prompt. Answer with `reply` on the conversation from the webhook payload. Use `wait` or `inbox` to read other messages in this chat.
```

把 SwB 添加到 Grok Bot 之后，再把提示词粘贴到 Bot 聊天中。

## 连接 Grok Bot [#连接-grok-bot]

### 创建 Bot [#创建-bot]

使用初始引导时创建的 Bot，或在侧边栏点击 **+** 并选择 **New Bot**。输入名称，选择 **Create**。

### 打开 Connect apps [#打开-connect-apps]

在侧边栏中打开 **Connect apps**，选择 **Add a custom connector**。

### 填写地址 [#填写地址]

在 **Name** 中填 `SwB`。在 **Type** 下选择 **Remote HTTPS**。在 **Server URL** 中填 `https://swb.example.com/mcp`。选择 **Add plugin**。

### 登录 [#登录]

可选的 OAuth 客户端字段留空。如果 Grok Bot 打开了 SwB 登录页面，以所有者身份登录，并批准访问。 

### 打开 routine [#打开-routine]

在 Bot 聊天的 **Details** 下打开 routine，检查 **Webhook** 触发器。

### 保存 Webhook URL [#保存-webhook-url]

打开 Webhook 地址对应的认领链接。从 routine 中复制 **POST to**，粘贴到 **Webhook URL** 中，然后选择 **Save webhook url**。

### 保存密钥 [#保存密钥]

打开 Webhook 密钥对应的认领链接。从 routine 中复制 **key**，粘贴到 **Token** 中，然后选择 **Save token**。

### 告知 Grok Bot [#告知-grok-bot]

在 Bot 聊天中发送“done”。Grok Bot 随后会测试 routine。

## 智能体会做什么 [#智能体会做什么]

让一个以所有者身份登录的智能体调用 `connect(what="grok-bot")`。SwB 返回 MCP 地址、OAuth 状态和一段 routine 提示词。这段提示词利用 Webhook 的 `trust` 字段，告诉 routine 可以执行哪些请求。routine 用 `reply` 回复。

让 Grok Bot 用这段提示词创建 routine。在你的所有者客户端中，用 `connect(what="grok-bot", choose={"name":"<routine>","project":"<project>","auth":"key"})` 注册它。SwB 分别为地址和密钥生成认领链接。两项都保存后，运行 `connect` 返回的 `send` 调用。

向 routine 发送消息时，SwB 调用它的 Webhook 来启动它。

## 凭据 [#凭据]

| 令牌 | 显示位置 | 格式 | 去向 |
| - | - | - | - |
| Webhook URL | routine **Details** 中 **Webhook** 下的 **POST to** | HTTPS 地址 | 对应 SwB 认领链接页面上的 **Webhook URL** |
| Webhook 密钥 | routine **Details** 中 **Webhook** 下的 **key** | 随机字符串 | 对应 SwB 认领链接页面上的 **Token** |
| SwB 登录令牌 | 你登录时由 Grok Bot 获取 | 随机字符串 | Grok Bot 替你保存 |

认领链接是 SwB 上的一次性私密页面，一个链接对应一个令牌。每个值只粘贴到它对应的页面。

## 检查连接 [#检查连接]

打开 routine 的 **Details**，检查 **Webhook** 触发器。从你的所有者客户端再次调用 `connect(what="grok-bot")`，状态行会给出 routine 的名称。在发送方客户端用 `inbox` 查看测试回复。

## 排查问题 [#排查问题]

如果 OAuth 登录失败，换用另一个 SwB 所有者客户端注册 routine。如果连接器接受 Bearer 密钥，使用[配对密钥](/zh-Hans/docs/setup/mcp-client#配对客户端)。

复制 **key** 的值。使用默认请求头时，SwB 会加上 `Authorization: Bearer`。

用 `grok-routine:routine:<routine>` 启动 routine。用 `agent:grok` 向 Grok 邮箱发送消息。

认领链接只能使用一次，30 分钟后过期。需要新链接时，再次调用 `connect`。要更改已保存的值，调用 `connect` 并把 `choose.replace` 设为 `url` 或 `secret`。
