用途:把真实 Slack 工作区接到本地运行的 PostHog Conversations(SupportHog Slack 应用),使 Slack 消息转为支持工单,工单回复再发回 Slack。Slack 只有 SaaS 版,所以“本地”指用一个临时 Slack 工作区与应用,让 OAuth 和事件能到达本机。
关键思路:OAuth 连接由浏览器跳转完成,localhost 可达,无需隧道;入站事件由 Slack 云端服务器发起,无法访问 localhost,必须用公网隧道。因此应用与 OAuth 留在 localhost,只把 Event Subscriptions(及 Interactivity)指向隧道,也顺带避开免费隧道限流。
相关端点(前缀 /api/conversations/):v1/slack/authorize 返回 OAuth URL(需登录,浏览器/localhost);v1/slack/callback 为 OAuth 重定向目标,由 SITE_URL 构造(浏览器/localhost);v1/slack/events 为入站事件 Webhook(Slack 服务器/隧道);v1/slack/interactivity 为交互组件回调(Slack 服务器/隧道)。回调需在 SITE_URL 对应源上已登录,因为会话 Cookie 按源隔离。
步骤:1)在 .env.local 配置 SUPPORT_SLACK_APP_CLIENT_ID、SUPPORT_SLACK_APP_CLIENT_SECRET、SUPPORT_SLACK_SIGNING_SECRET 并重启后端;这些是 django-constance 动态设置,DB 中已存值会覆盖环境变量,若仍显示未配置,检查 /admin/constance/config/。2)在 api.slack.com/apps 创建应用:App Home 启用 bot 用户;OAuth & Permissions 添加 Bot Token Scopes(channels:history、channels:read、chat:write、chat:write.customize、files:read、files:write、groups:history、groups:read、reactions:read、users:read、users:read.email);Redirect URLs 添加 http://localhost:8010/api/conversations/v1/slack/callback;复制 Client ID、Client Secret、Signing Secret。3)启动指向 8010 端口 Caddy 的 HTTPS 隧道并重写 Host 头:ngrok http --host-header=localhost 8010,或 cloudflared tunnel --url http://localhost:8010 --http-host-header localhost;用 curl -sS https://<tunnel-host>/_preflight 验证到达 Django。然后在 Slack 应用把 Event Subscriptions 与 Interactivity 的 Request URL 设为隧道地址下的 /api/conversations/v1/slack/events 与 /interactivity,并订阅 app_mention、message.channels(私有频道加 message.groups)、reaction_added、member_joined_channel、member_left_channel,改后需重装应用。4)访问 http://localhost:8010 登录,在 Support settings 连接 Slack,邀请 bot 入频道并发消息测试。
限制:需临时工作区与自建 Slack 应用;隧道必须重写 Host 头,否则返回空 200 白屏;回调若被 Slack 拒绝 http,可改用隧道地址并在该源登录;故障排查见 references/troubleshooting.md。