通过OpenClaw实现副业收入:《OpenClaw赚钱实录:从“养龙虾“到可持续变现的实践指南》
Plugin required
Twitch ships as a plugin and is not bundled with the core install. Install via CLI (npm registry):
openclaw pluginsinstall@openclaw/twitchLocal checkout (when running from a git repo):
openclaw pluginsinstall./extensions/twitchDetails: Plugins
Quick setup (beginner)
- 为机器人创建一个专用的 Twitch 账号(或使用现有账号)。
- 生成凭证:Twitch Token Generator
- 选择Bot Token
- 确认已勾选作用域
chat:read和chat:write - 复制Client ID和Access Token
- 查找你的 Twitch 用户 ID:https://www.streamweasels.com/tools/convert-twitch-username-to-user-id/
- 配置 token:
- 环境变量:
OPENCLAW_TWITCH_ACCESS_TOKEN=...(仅默认账户) - 或配置文件:
channels.twitch.accessToken - 如果两者都设置,配置文件优先(环境变量仅作为默认账户的备选)。
- 环境变量:
- 启动 gateway。
⚠️ 重要提示:请添加访问控制(allowFrom或allowedRoles)以防止未授权用户触发机器人。requireMention默认为true。
最小配置示例:
{ channels: { twitch: { enabled: true, username: "openclaw", // 机器人的 Twitch 账号 accessToken: "oauth:abc123...", // OAuth Access Token(或使用 OPENCLAW_TWITCH_ACCESS_TOKEN 环境变量) clientId: "xyz789...", // 从 Token Generator 获取的 Client ID channel: "vevisk", // 要加入的 Twitch 频道(必需) allowFrom: ["123456789"], // (推荐)仅允许你的 Twitch 用户 ID - 从 https://www.streamweasels.com/tools/convert-twitch-username-to-user-id/ 获取 }, }, }What it is
- Gateway 拥有的一个 Twitch 频道。
- 确定性路由:回复始终回到 Twitch。
- 每个账户映射到一个独立的会话键
agent::twitch:。 username是机器人的账户(谁进行身份验证),channel是要加入的聊天室。
Setup (detailed)
Generate credentials
使用 Twitch Token Generator:
- 选择Bot Token
- 确认已勾选作用域
chat:read和chat:write - 复制Client ID和Access Token
无需手动注册应用程序。Token 会在几小时后过期。
Configure the bot
环境变量(仅默认账户):
OPENCLAW_TWITCH_ACCESS_TOKEN=oauth:abc123...或配置文件:
{ channels: { twitch: { enabled: true, username: "openclaw", accessToken: "oauth:abc123...", clientId: "xyz789...", channel: "vevisk", }, }, }如果同时设置了环境变量和配置文件,配置文件优先。
Access control (recommended)
{ channels: { twitch: { allowFrom: ["123456789"], // (推荐)仅允许你的 Twitch 用户 ID }, }, }优先使用allowFrom作为硬性允许列表。如果要使用基于角色的访问控制,请改用allowedRoles。
可用角色:"moderator"、"owner"、"vip"、"subscriber"、"all"。
为什么使用用户 ID?用户名可以更改,可能导致身份冒充。用户 ID 是永久不变的。查找你的 Twitch 用户 ID:https://www.streamweasels.com/tools/convert-twitch-username-to-user-id/(将你的 Twitch 用户名转换为 ID)
Token refresh (optional)
来自 Twitch Token Generator 的 token 无法自动刷新——过期后请重新生成。
如需自动刷新 token,请在 Twitch Developer Console 创建自己的 Twitch 应用程序,并添加到配置中:
{ channels: { twitch: { clientSecret: "your_client_secret", refreshToken: "your_refresh_token", }, }, }机器人会在 token 过期前自动刷新,并记录刷新事件。
Multi-account support
使用channels.twitch.accounts配合每个账户的独立 token。通用模式请参见gateway/configuration。
示例(一个机器人账户在两个频道中):
{ channels: { twitch: { accounts: { channel1: { username: "openclaw", accessToken: "oauth:abc123...", clientId: "xyz789...", channel: "vevisk", }, channel2: { username: "openclaw", accessToken: "oauth:def456...", clientId: "uvw012...", channel: "secondchannel", }, }, }, }, }注意:每个账户需要自己的 token(每个频道一个 token)。
Access control
Role-based restrictions
{ channels: { twitch: { accounts: { default: { allowedRoles: ["moderator", "vip"], }, }, }, }, }Allowlist by User ID (most secure)
{ channels: { twitch: { accounts: { default: { allowFrom: ["123456789", "987654321"], }, }, }, }, }Role-based access (alternative)
allowFrom是一个硬性允许列表。设置后,仅允许这些用户 ID。如果你希望使用基于角色的访问,请不设置allowFrom,而是配置allowedRoles:
{ channels: { twitch: { accounts: { default: { allowedRoles: ["moderator"], }, }, }, }, }Disable @mention requirement
默认情况下,requireMention为true。要禁用并响应所有消息:
{ channels: { twitch: { accounts: { default: { requireMention: false, }, }, }, }, }Troubleshooting
首先,运行诊断命令:
openclaw doctor openclaw channels status--probeBot doesn’t respond to messages
检查访问控制:确保你的用户 ID 在allowFrom中,或者临时移除allowFrom并设置allowedRoles: ["all"]进行测试。
检查机器人是否在频道中:机器人必须加入channel指定的频道。
Token issues
“连接失败”或身份验证错误:
- 确认
accessToken是 OAuth 访问令牌的值(通常以oauth:前缀开头) - 检查 token 是否具有
chat:read和chat:write作用域 - 如果使用 token 刷新,请确认设置了
clientSecret和refreshToken
Token refresh not working
检查日志中的刷新事件:
Using env token source for mybot Access token refreshed for user 123456 (expires in 14400s)如果看到 “token refresh disabled (no refresh token)”:
- 确保提供了
clientSecret - 确保提供了
refreshToken
Config
Account config:
username- 机器人用户名accessToken- 具有chat:read和chat:write的 OAuth 访问令牌clientId- Twitch Client ID(来自 Token Generator 或你自己的应用)channel- 要加入的频道(必需)enabled- 启用此账户(默认:true)clientSecret- 可选:用于自动刷新 tokenrefreshToken- 可选:用于自动刷新 tokenexpiresIn- Token 过期时间(秒)obtainmentTimestamp- Token 获取时间戳allowFrom- 用户 ID 允许列表allowedRoles- 基于角色的访问控制("moderator" | "owner" | "vip" | "subscriber" | "all")requireMention- 是否需要 @提及(默认:true)
Provider options:
channels.twitch.enabled- 启用/禁用频道启动channels.twitch.username- 机器人用户名(简化单账户配置)channels.twitch.accessToken- OAuth 访问令牌(简化单账户配置)channels.twitch.clientId- Twitch Client ID(简化单账户配置)channels.twitch.channel- 要加入的频道(简化单账户配置)channels.twitch.accounts- 多账户配置(包含上述所有账户字段)
完整示例:
{ channels: { twitch: { enabled: true, username: "openclaw", accessToken: "oauth:abc123...", clientId: "xyz789...", channel: "vevisk", clientSecret: "secret123...", refreshToken: "refresh456...", allowFrom: ["123456789"], allowedRoles: ["moderator", "vip"], accounts: { default: { username: "mybot", accessToken: "oauth:abc123...", clientId: "xyz789...", channel: "your_channel", enabled: true, clientSecret: "secret123...", refreshToken: "refresh456...", expiresIn: 14400, obtainmentTimestamp: 1706092800000, allowFrom: ["123456789", "987654321"], allowedRoles: ["moderator"], }, }, }, }, }Tool actions
代理可以调用twitch并执行以下操作:
send- 向频道发送消息
示例:
{ action: "twitch", params: { message: "Hello Twitch!", to: "#mychannel", }, }Safety & ops
- 将 token 视为密码- 切勿将 token 提交到 git
- 对长期运行的机器人使用自动 token 刷新
- 使用用户 ID 允许列表而不是用户名进行访问控制
- 监控日志以查看 token 刷新事件和连接状态
- 最小化 token 作用域- 仅请求
chat:read和chat:write - 如果卡住:确认没有其他进程拥有该会话后,重启 gateway
Limits
- 每条消息500 个字符(在单词边界自动分块)
- 分块前会去除 Markdown 格式
- 无速率限制(使用 Twitch 内置的速率限制)