Slack Channel Context - Technical Documentation
For quick start: See README.md
Overview
This skill automatically loads channel context files from slack-channel-contexts/ into your session context whenever you're interacting in a Slack channel or thread. This ensures you have the right context for each channel without manual intervention.
In my Openclaw workflow, I use Slack with diffrent channels for different purposes. As a developer, I have dedicated Slack channels for each project. I needed a way to ensure that the Openclaw Agent knew which project I meant when I said something like; run all unit tests. Or if I wanted to discuss a new feature, I wanted to simply be able to start discussing in the appropriate channel and have the Agent know which project I meant. This Skill is the artifact that solved for my case.
Key Design Decision: Context files are named simply as <CHANNEL_ID>.md or <CHANNEL_NAME>.md (e.g., bebops.md, C0AK8SDFS4W.md). There is NO default fallback file - channels without context files return None.
How It Works
Context Loading Process
- 1. Detection: Skill detects when you're in a Slack channel or thread
- Thread Check: If the message is in a thread (has
thread_id or thread_ts), checks SLACK_CONTEXT_LOAD_ON_THREADS setting
- If
SLACK_CONTEXT_LOAD_ON_THREADS=false, skips context loading in threads
- If
SLACK_CONTEXT_LOAD_ON_THREADS=true (default), loads context in threads
- 3. File Discovery: Looks for context files in
slack-channel-contexts/ folder in priority order:
-
slack-channel-contexts/<CHANNEL_ID>.md (e.g.,
C0AK8SDFS4W.md) -
highest priority
-
slack-channel-contexts/<CHANNEL_NAME>.md (e.g.,
bebops.md) - medium priority
- No default fallback - returns
None if no file exists
- 4. Context Injection: Loads the found file into session context
- Caching: Caches the loaded context for 1 hour (3600 seconds) to avoid repeated file reads
Tools
load_channel_context()
Load Slack channel context into the current session.
This tool automatically loads the appropriate channel context file based on the current Slack channel. Call this at session startup to ensure you have full channel awareness.
Signature:
CODEBLOCK0
Parameters:
- -
channel_id (str, required): Slack channel ID (e.g., "C0AK8SDFS4W") - INLINECODE19 (str, required): Slack channel name (e.g., "bebops")
- INLINECODE20 (str, optional): Custom workspace directory (defaults to
~/.openclaw/workspace) - INLINECODE22 (bool, optional): If True, bypass cache and read file fresh (use after editing context files)
- INLINECODE23 (bool, optional): If True, checks
SLACK_CONTEXT_LOAD_ON_THREADS setting (default: False)
Returns:
CODEBLOCK1
Or if no context file exists:
CODEBLOCK2
Example:
CODEBLOCK3
Behavior:
- - Looks for context files in priority order:
1.
slack-channel-contexts/<CHANNEL_ID>.md
2.
slack-channel-contexts/<CHANNEL_NAME>.md
3. No default - returns
None if no file exists
- - Caches results for 1 hour (3600 seconds) by default
- Use
force_reload=True after editing context files to bypass cache
Implementation Files
scripts/skill.py
Location: INLINECODE30
Main implementation file containing the SlackContextLoader class and load_channel_context() function.
scripts/load_context.py
Location: INLINECODE34
Legacy module for backward compatibility. Contains the core SlackContextLoader class.
scripts/reload_all_contexts.py
Location: INLINECODE37
Utility script to reload all channel context files, bypassing cache.
Usage:
CODEBLOCK4
scripts/example.py
Location: INLINECODE39
Demo script showing how the context loader works with different channels.
Usage:
CODEBLOCK5
Environment Variables
Set these in ~/.openclaw/workspace/.env (preferred method):
| Variable | Default | Description |
|---|
| INLINECODE41 | INLINECODE42 | Enable/disable the skill |
| INLINECODE43 |
true | Load context in threaded messages |
|
SLACK_CONTEXT_LOAD_ON_THREAD_START |
true | Load context on thread/session start |
|
SLACK_CONTEXT_CACHE_TTL |
7200 | Cache duration in seconds (120 min) |
|
SLACK_CONTEXT_CONTEXTS_DIR |
~/.openclaw/workspace/slack-channel-contexts/ | Custom contexts directory |
Note: Do not use export - just set the variables directly in ~/.openclaw/workspace/.env.
Context File Structure
Context files follow this structure:
CODEBLOCK6
See examples/EXAMPLE_CHANNEL_CONTEXT.md for a complete working example.
Important Notes
Force Reload After Editing
After editing a context file, you MUST use force_reload=True to bypass the cache:
CODEBLOCK7
No Default Fallback
There is NO default SLACK_CHANNEL_CONTEXT.md file. If a channel doesn't have a context file, the skill returns success: false with an empty context. This is intentional - channels without context simply have no context.
Simple File Names
Context files are named simply as <CHANNEL_ID>.md or <CHANNEL_NAME>.md. The SLACK_CHANNEL_CONTEXT_ prefix has been removed to make file names shorter and more readable.
Troubleshooting
See TROUBLESHOOTING.md for detailed troubleshooting steps.
Files
| File | Purpose |
|---|
| INLINECODE61 | Main implementation |
| INLINECODE62 |
Legacy module |
|
scripts/example.py | Demo script |
|
scripts/reload_all_contexts.py | Utility script |
|
examples/EXAMPLE_CHANNEL_CONTEXT.md | Complete working example |
|
_meta.json | ClawHub publishing metadata |
For quick start: README.md
For troubleshooting: TROUBLESHOOTING.md
Slack 频道上下文 - 技术文档
快速入门:参见 README.md
概述
此技能会在您与 Slack 频道或线程交互时,自动将 slack-channel-contexts/ 中的频道上下文文件加载到您的会话上下文中。这确保您无需手动干预即可获得每个频道的正确上下文。
在我的 Openclaw 工作流中,我使用 Slack 的不同频道来处理不同任务。作为开发者,我为每个项目都设置了专门的 Slack 频道。我需要一种方法,确保当我提到运行所有单元测试时,Openclaw Agent 能知道是哪个项目;或者当我想讨论新功能时,只需在对应频道中开始讨论,Agent 就能自动识别项目。这个技能正是为解决我的需求而生的。
关键设计决策: 上下文文件直接命名为 ID>.md 或 NAME>.md(例如 bebops.md、C0AK8SDFS4W.md)。没有默认回退文件——没有上下文文件的频道将返回 None。
工作原理
上下文加载流程
- 1. 检测:技能检测到您正在 Slack 频道或线程中
- 线程检查:如果消息在线程中(包含 threadid 或 threadts),检查 SLACKCONTEXTLOADONTHREADS 设置
- 如果 SLACK
CONTEXTLOAD
ONTHREADS=false,则跳过线程中的上下文加载
- 如果 SLACK
CONTEXTLOAD
ONTHREADS=true(默认),则在线程中加载上下文
- 3. 文件发现:按优先级顺序在 slack-channel-contexts/ 文件夹中查找上下文文件:
- slack-channel-contexts/
.md(例如 C0AK8SDFS4W.md)——最高优先级
- slack-channel-contexts/.md(例如 bebops.md)——中等优先级
- 无默认回退——如果文件不存在则返回 None
- 4. 上下文注入:将找到的文件加载到会话上下文中
- 缓存:将加载的上下文缓存 1 小时(3600 秒),避免重复读取文件
工具
loadchannelcontext()
将 Slack 频道上下文加载到当前会话中。
此工具会根据当前 Slack 频道自动加载相应的频道上下文文件。请在会话启动时调用此工具,以确保您拥有完整的频道感知能力。
签名:
python
loadchannelcontext(
channel_id: str,
channel_name: str,
workspace_dir: str = None,
force_reload: bool = False,
is_thread: bool = False
) -> dict
参数:
- - channelid (str, 必填):Slack 频道 ID(例如 C0AK8SDFS4W)
- channelname (str, 必填):Slack 频道名称(例如 bebops)
- workspacedir (str, 可选):自定义工作区目录(默认为 ~/.openclaw/workspace)
- forcereload (bool, 可选):如果为 True,则绕过缓存并重新读取文件(编辑上下文文件后使用)
- isthread (bool, 可选):如果为 True,则检查 SLACKCONTEXTLOADON_THREADS 设置(默认为 False)
返回结果:
json
{
success: true,
context: # 频道上下文:#bebops\n\n## 用途...,
channel_id: C0AK8SDFS4W,
channel_name: bebops,
message: 已为 bebops 频道加载上下文
}
或者如果没有上下文文件:
json
{
success: false,
context: ,
channel_id: C0AK8SDFS4W,
channel_name: bebops,
message: 未找到该频道的上下文文件。上下文文件必须创建在 slack-channel-contexts/ 目录中。
}
示例:
python
result = loadchannelcontext(channelid=C0AK8SDFS4W, channelname=bebops)
if result[success]:
print(f已加载上下文:{result[context][:200]}...)
else:
print(f无可用上下文:{result[message]})
行为:
1. slack-channel-contexts/.md
2. slack-channel-contexts/.md
3. 无默认文件——如果文件不存在则返回 None
- - 默认将结果缓存 1 小时(3600 秒)
- 编辑上下文文件后使用 force_reload=True 可绕过缓存
实现文件
scripts/skill.py
位置: scripts/skill.py
主实现文件,包含 SlackContextLoader 类和 loadchannelcontext() 函数。
scripts/load_context.py
位置: scripts/load_context.py
用于向后兼容的旧模块。包含核心的 SlackContextLoader 类。
scripts/reloadallcontexts.py
位置: scripts/reloadallcontexts.py
用于重新加载所有频道上下文文件、绕过缓存的实用脚本。
用法:
bash
python3 ~/.openclaw/workspace/skills/slack-channel-context/scripts/reloadallcontexts.py
scripts/example.py
位置: scripts/example.py
演示脚本,展示上下文加载器如何与不同频道配合使用。
用法:
bash
python3 ~/.openclaw/workspace/skills/slack-channel-context/scripts/example.py
环境变量
请在 ~/.openclaw/workspace/.env 中设置(推荐方法):
| 变量 | 默认值 | 描述 |
|---|
| SLACKCONTEXTENABLED | true | 启用/禁用此技能 |
| SLACKCONTEXTLOADONTHREADS |
true | 在线程消息中加载上下文 |
| SLACKCONTEXTLOADONTHREAD_START | true | 在线程/会话启动时加载上下文 |
| SLACKCONTEXTCACHE_TTL | 7200 | 缓存持续时间(秒,120 分钟) |
| SLACKCONTEXTCONTEXTS_DIR | ~/.openclaw/workspace/slack-channel-contexts/ | 自定义上下文目录 |
注意: 不要使用 export——直接在 ~/.openclaw/workspace/.env 中设置变量即可。
上下文文件结构
上下文文件遵循以下结构:
markdown
频道上下文:#频道名称
用途
[这个频道是做什么的?]
关联项目
规则与指南
近期活动
相关人员
完整的工作示例请参见 examples/EXAMPLECHANNEL_CONTEXT.md。
重要说明
编辑后强制重新加载
编辑上下文文件后,您必须使用 force_reload=True 来绕过缓存:
python
loadchannelcontext(
channel_id=C0AK8SDFS4W,
channel_name=bebops,
force_reload=True
)
无默认回退
没有默认的 SLACKCHANNELCONTEXT.md 文件。如果某个频道没有上下文文件,技能将返回 success: false 并附带空上下文。这是有意为之——没有上下文的频道就是没有上下文。
简单的文件名
上下文文件直接命名为 ID>.md 或 NAME>.md。已移除 SLACKCHANNELCONTEXT_ 前缀,使文件名更短、更易读。
故障排除
详细的故障排除步骤请参见 TROUBLESHOOTING.md。
文件
| 文件 | 用途 |
|---|
| scripts/skill.py | 主实现 |
| scripts/load_context.py |
旧模块 |
| scripts/example.py | 演示脚本 |
| scripts/reloadallcontexts.py | 实用脚本 |
| examples/EXAMPLECHANNELCONTEXT.md | 完整工作示例 |
| _meta.json | ClawHub 发布元数据 |
快速入门:README.md
故障排除:TROUBLESHOOTING.md