关于 slack-bot-builder
slack-bot-builder 是用于构建 Slack 应用的技能,涵盖 Bolt、Block Kit 和 OAuth 安装模式,适合开发 Slack 集成的开发者。
主要功能
- 01使用 Python、JavaScript 和 Java 搭建 Bolt 应用
- 02构建交互式 Block Kit 消息布局
- 03通过 OAuth 在多个工作区安装应用
- 04了解令牌、权限范围和请求处理
适用场景
- 01启动新的 Slack 应用
- 02添加交互式表单和消息
- 03向多个工作区分发应用
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ davila7/ claude-code-templates --skill slack-bot-builder安装后在 Claude Code 中输入/slack-bot-builder确认已加载。
详细介绍
Slack Bot 构建器
模式
Bolt 应用基础模式
Bolt 框架是 Slack 推荐的应用构建方式。 它负责处理身份验证、事件路由、请求验证和 HTTP 请求处理,让你专注于应用逻辑。
主要优势:
- 只需几行代码即可处理事件
- 内置安全检查和负载验证
- 结构清晰、模式一致
- 适用于实验和生产环境
可用语言:Python、JavaScript(Node.js)、Java
适用场景:['开始构建任何新的 Slack 应用', '从旧版 Slack API 迁移', '构建生产环境中的 Slack 集成']
# Python Bolt App
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
import os
# Initialize with tokens from environment
app = App(
token=os.environ["SLACK_BOT_TOKEN"],
signing_secret=os.environ["SLACK_SIGNING_SECRET"]
)
# Handle messages containing "hello"
@app.message("hello")
def handle_hello(message, say):
"""Respond to messages containing 'hello'."""
user = message["user"]
say(f"Hey there <@{user}>!")
# Handle slash command
@app.command("/ticket")
def handle_ticket_command(ack, body, client):
"""Handle /ticket slash command."""
# Acknowledge immediately (within 3 seconds)
ack()
# Open a modal for ticket creation
client.views_open(
trigger_id=body["trigger_id"],
view={
"type": "modal",
"callback_id": "ticket_modal",
"title": {"type": "plain_text", "text": "Create Ticket"},
"submit": {"type": "plain_text", "text": "Submit"},
"blocks": [
{
"type": "input",
"block_id": "title_block",
"element": {
"type": "plain_text_input",
"action_id": "title_input"
},
"label": {"type": "plain_text", "text": "Title"}
},
{
"type": "input",
"block_id": "desc_block",
"element": {
"type": "plain_text_input",
"multiline": True,
"action_id": "desc_input"
},
"label": {"type": "plain_text", "text": "Description"}
},
{
"type": "input",
"block_id": "priority_block",
"element": {
"type": "static_select",
"action_id": "priority_select",
Block Kit UI 模式
Block Kit 是 Slack 用于构建丰富交互式消息的 UI 框架。 使用区块(部分、操作、输入)和元素 (按钮、菜单、文本输入框)来组合消息。
限制:
- 每条消息最多包含 50 个区块
- 模态框/Home 标签页最多包含 100 个区块
- 区块文本最多 3000 个字符
使用 Block Kit Builder 制作原型:https://app.slack.com/block-kit-builder
适用场景:['构建丰富的消息布局', '为消息添加交互式组件', '在模态框中创建表单', '构建 Home 标签页体验']
from slack_bolt import App
import os
app = App(token=os.environ["SLACK_BOT_TOKEN"])
def build_notification_blocks(incident: dict) -> list:
"""Build Block Kit blocks for incident notification."""
severity_emoji = {
"critical": ":red_circle:",
"high": ":large_orange_circle:",
"medium": ":large_yellow_circle:",
"low": ":white_circle:"
}
return [
# Header
{
"type": "header",
"text": {
"type": "plain_text",
"text": f"{severity_emoji.get(incident['severity'], '')} Incident Alert"
}
},
# Details section
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": f"*Incident:*\n{incident['title']}"
},
{
"type": "mrkdwn",
"text": f"*Severity:*\n{incident['severity'].upper()}"
},
{
"type": "mrkdwn",
"text": f"*Service:*\n{incident['service']}"
},
{
"type": "mrkdwn",
"text": f"*Reported:*\n<!date^{incident['timestamp']}^{date_short} {time}|{incident['timestamp']}>"
}
]
},
# Description
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*Description:*\n{incident['description'][:2000]}"
}
},
# Divider
{"type": "divider"},
# Action buttons
{
"type": "actions",
"block_id": f"incident_actions_{incident['id']}",
"elements": [
{
"type": "button",
"text": {"type": "plain_text", "text": "Acknowledge"},
"style": "primary",
"action_id": "acknowle
OAuth 安装模式
通过 OAuth 2.0 让用户在其工作区中安装你的应用。 Bolt 会处理大部分 OAuth 流程,但你需要进行配置 并安全地存储令牌。
OAuth 关键概念:
- 范围定义权限(仅申请所需的最低权限)
- 令牌特定于工作区
- 必须持久化存储安装数据
- 用户之后可以添加范围(增量添加)
面对过多的权限请求时,70% 的用户会放弃安装——只申请你需要的权限!
适用场景:['向多个工作区分发应用', '构建公开的 Slack 应用', '构建企业级集成']
from slack_bolt import App
from slack_bolt.oauth.oauth_settings import OAuthSettings
from slack_sdk.oauth.installation_store import FileInstallationStore
from slack_sdk.oauth.state_store import FileOAuthStateStore
import os
# For production, use database-backed stores
# For example: PostgreSQL, MongoDB, Redis
class DatabaseInstallationStore:
"""Store installation data in your database."""
async def save(self, installation):
"""Save installation when user completes OAuth."""
await db.installations.upsert({
"team_id": installation.team_id,
"enterprise_id": installation.enterprise_id,
"bot_token": encrypt(installation.bot_token),
"bot_user_id": installation.bot_user_id,
"bot_scopes": installation.bot_scopes,
"user_id": installation.user_id,
"installed_at": installation.installed_at
})
async def find_installation(self, *, enterprise_id, team_id, user_id=None, is_enterprise_install=False):
"""Find installation for a workspace."""
record = await db.installations.find_one({
"team_id": team_id,
"enterprise_id": enterprise_id
})
if record:
return Installation(
bot_token=decrypt(record["bot_token"]),
# ... other fields
)
return None
# Initialize OAuth-enabled app
app = App(
signing_secret=os.environ["SLACK_SIGNING_SECRET"],
oauth_settings=OAuthSettings(
client_id=os.environ["SLACK_CLIENT_ID"],
client_secret=os.environ["SLACK_CLIENT_SECRET"],
scopes=[
"channels:history",
"channels:read",
"chat:write",
"commands",
"users:read"
],
user_scopes=[], # User token scopes if needed
installation_store=DatabaseInstallationStore(),
state_store=FileOAuthStateStore(expiration_seconds=600)
)
)
# OAuth routes are handled a
⚠️ 易踩陷阱
| 问题 | 严重程度 | 解决方案 |
|---|---|---|
| 问题 | critical | ## 立即确认,稍后处理 |
| 问题 | critical | ## 正确验证状态 |
| 问题 | critical | ## 切勿硬编码或记录令牌 |
| 问题 | high | ## 仅申请最低限度的必要范围 |
| 问题 | medium | ## 了解并遵守限制 |
| 问题 | high | ## Socket Mode:仅用于开发 |
| 问题 | critical | ## Bolt 会自动处理此问题 |
评论
支持 Markdown