Slack 自动化
与 Slack 工作区交互,检查消息、提取数据并自动化常见任务。
快速开始
连接到现有的 Slack 浏览器会话或打开 Slack:
# 连接到现有会话(端口 9222,通常用于已打开的 Slack)
agent-browser connect 9222
# 如果 Slack 尚未运行,则打开它
agent-browser open https://app.slack.com
然后进行快照以查看可用的内容:
agent-browser snapshot -i
核心工作流
- 连接/导航:打开或连接到 Slack
- 快照:获取包含引用(
@e1、@e2等)的交互式元素 - 导航:点击标签、展开部分或导航到特定频道
- 提取/交互:读取数据或执行操作
- 截图:捕获结果的证据
# 示例:检查未读频道
agent-browser connect 9222
agent-browser snapshot -i
# 查找“更多未读”按钮
agent-browser click @e21 # “更多未读”按钮的引用
agent-browser screenshot slack-unreads.png
常见任务
检查未读消息
# 连接到 Slack
agent-browser connect 9222
# 进行快照以定位未读按钮
agent-browser snapshot -i
# 查找:
# - “更多未读”按钮(通常在侧边栏顶部附近)
# - “活动”标签中的“未读”切换(显示未读计数)
# - 带有徽章/粗体文本指示未读的频道名称
# 导航到“活动”标签以统一查看所有未读
agent-browser click @e14 # 活动标签(引用可能不同)
agent-browser wait 1000
agent-browser screenshot activity-unreads.png
# 或检查 DM 标签
agent-browser click @e13 # DM 标签
agent-browser screenshot dms.png
# 或展开侧边栏中的“更多未读”
agent-browser click @e21 # 更多未读按钮
agent-browser wait 500
agent-browser screenshot expanded-unreads.png
导航到频道
# 搜索侧边栏或名称中的频道
agent-browser snapshot -i
# 在列表中查找频道名称(例如,“engineering”、“product-design”)
# 点击频道 treeitem 引用
agent-browser click @e94 # 示例:engineering 频道引用
# 将 `engineering` 替换为点击的频道名称,以便旧频道头无法满足等待条件,而新频道仍在渲染。
agent-browser wait --fn "document.querySelector('[data-qa="channel_header"]')?.innerText.includes('engineering')"
agent-browser screenshot channel.png
查找消息/线程
# 使用 Slack 搜索
agent-browser snapshot -i
agent-browser click @e5 # 搜索按钮(典型引用)
agent-browser fill @e_search "keyword"
agent-browser press Enter
# 匹配提交的查询,以便重复搜索无法重用旧路由。
agent-browser wait --fn "location.pathname.includes('/search') && new URL(location.href).searchParams.get('q') === 'keyword'"
agent-browser screenshot search-results.png
提取频道信息
# 获取所有可见频道列表
agent-browser snapshot --json > slack-snapshot.json
# 解析频道名称和元数据
# 查找 level=2 的 treeitem 元素(各部分下的子频道)
检查频道详情
# 打开一个频道
agent-browser click @e_channel_ref
agent-browser wait 1000
# 获取频道信息(成员、描述等)
agent-browser snapshot -i
agent-browser screenshot channel-details.png
# 滚动查看消息
agent-browser scroll down 500
agent-browser screenshot channel-messages.png
记录笔记/捕获状态
当需要记录 Slack 中的发现时:
# 标注截图(显示元素编号)
agent-browser screenshot --annotate slack-state.png
# 全页截图
agent-browser screenshot --full slack-full.png
# 获取当前 URL 以供参考
agent-browser get url
# 获取页面标题
agent-browser get title
侧边栏结构
了解 Slack 的侧边栏有助于高效导航:
- 线程
- 群组通话
- 草稿和已发送
- 目录
- [节标题 – 外部连接、星标项、频道等]
- [频道列为 treeitem]
- 直接消息
- [DM 列出]
- 应用
- [应用快捷方式]
- [更多未读] 按钮(切换未读频道列表)
要查找的关键引用:
@e12– 首页标签(通常)@e13– DM 标签@e14– 活动标签@e5– 搜索按钮@e21– 更多未读按钮(因会话而异)
Slack 中的标签
点击频道后,您会看到标签:
- 消息 – 频道对话
- 文件 – 共享文件
- 固定项 – 固定消息
- 添加画布 – 协作画布
- 其他标签(取决于工作区设置)
点击标签引用以切换视图并获取不同信息。
从 Slack 提取数据
获取文本内容
# 获取消息或元素的文本
agent-browser get text @e_message_ref
解析可访问性树
# 以 JSON 格式获取完整快照以进行程序化解析
agent-browser snapshot --json > output.json
# 查找:
# - 频道名称(treeitem 中的 name 字段)
# - 消息内容(在 listitem/document 元素中)
# - 用户名(带有用户信息的 button 元素)
# - 时间戳(带有时间信息的 link 元素)
计算未读数量
# 展开未读部分后:
agent-browser snapshot -i | grep -c "treeitem"
# 未读部分中的每个带有频道名称的 treeitem 都算作一个未读
最佳实践
- 连接到现有会话:如果 Slack 已打开,请使用
agent-browser connect 9222。这比打开新浏览器更快。 - 在点击前进行快照:始终先执行
snapshot -i以识别引用,然后再点击按钮。 - 导航后重新快照:导航到新频道或部分后,进行全新快照以找到新的引用。
- 使用 JSON 快照进行解析:当需要提取结构化数据时,使用
snapshot --json获得机器可读的输出。 - 控制交互节奏:在快速交互之间添加
sleep 1以允许 UI 更新。 - 检查可访问性树:可访问性树显示屏幕阅读器(和您的自动化)能看到的内容。如果元素不在快照中,它可能已隐藏或需要滚动。
- 在侧边栏中滚动:如果频道列表很长,使用
agent-browser scroll down 300 --selector ".p-sidebar"在 Slack 侧边栏中滚动。
局限性
- 无法访问 Slack API:这使用浏览器自动化,而非 Slack API。不需要 OAuth、webhook 或 bot token。
- 会话特定:截图和快照绑定到当前浏览器会话。
- 速率限制:Slack 可能会限制快速交互。如有必要,请在命令之间添加延迟。
- 工作区特定:您只能交互自己的工作区——不支持跨工作区自动化。
调试
检查控制台错误
agent-browser console
agent-browser errors
获取当前页面状态
agent-browser get url
agent-browser get title
agent-browser screenshot page-state.png
示例:完整未读检查
#!/bin/bash
# 连接到 Slack
agent-browser connect 9222
# 进行初始快照
echo "=== 检查 Slack 未读 ==="
agent-browser snapshot -i > snapshot.txt
# 检查“活动”标签中的未读
agent-browser click @e14 # 活动标签
agent-browser wait 1000
agent-browser screenshot activity.png
ACTIVITY_RESULT=$(agent-browser get text @e_main_area)
echo "活动:$ACTIVITY_RESULT"
# 检查 DM
agent-browser click @e13 # DM 标签
agent-browser wait 1000
agent-browser screenshot dms.png
# 检查侧边栏中的未读频道
agent-browser click @e21 # 更多未读按钮
agent-browser wait 500
agent-browser snapshot -i > unreads-expanded.txt
agent-browser screenshot unreads.png
# 摘要
echo "=== 摘要 ==="
echo "详见 activity.png、dms.png 和 unreads.png"
参考
- Slack 文档:https://slack.com/help
- Web 体验:https://app.slack.com
- 键盘快捷键:在 Slack 中按
?查看快捷键列表