You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

触发Global shortcut后如何向Slack频道发送消息?

解决Slack全局快捷方式向当前打开频道发消息的问题

由于全局快捷方式触发时仅返回triggerId,无法直接获取频道信息,需要通过模态框交互间接获取当前频道上下文,具体步骤如下:

1. 用triggerId打开模态框

拿到全局快捷方式触发后的triggerId,调用Slack的views.open API弹出一个确认模态框,引导用户确认操作。这个模态框的核心是触发后续交互,从而拿到频道信息。

示例代码(以Python为例):

import requests

def open_confirm_modal(trigger_id):
    api_url = "https://slack.com/api/views.open"
    auth_headers = {"Authorization": "Bearer 你的Slack机器人Token"}
    modal_config = {
        "trigger_id": trigger_id,
        "view": {
            "type": "modal",
            "callback_id": "global_shortcut_send_msg",
            "title": {"type": "plain_text", "text": "发送消息到当前频道"},
            "blocks": [
                {
                    "type": "section",
                    "text": {"type": "plain_text", "text": "确认要向当前打开的频道发送消息吗?"}
                }
            ],
            "submit": {"type": "plain_text", "text": "确认发送"}
        }
    }
    response = requests.post(api_url, json=modal_config, headers=auth_headers)
    return response.json()

2. 处理模态框提交交互

当用户点击模态框的「确认发送」按钮后,Slack会向你的应用发送交互请求,请求负载中会包含用户当前打开的频道ID(位于payload.channel.id)。此时即可调用chat.postMessage API发送消息到目标频道。

示例代码:

def handle_modal_submission(payload):
    target_channel_id = payload["channel"]["id"]
    trigger_user_id = payload["user"]["id"]
    api_url = "https://slack.com/api/chat.postMessage"
    auth_headers = {"Authorization": "Bearer 你的Slack机器人Token"}
    message_data = {
        "channel": target_channel_id,
        "text": f"全局快捷方式触发的消息:由<@{trigger_user_id}>发起"
    }
    response = requests.post(api_url, json=message_data, headers=auth_headers)
    return response.json()

3. 配置必要权限

确保你的Slack应用已申请以下权限,并重新安装到工作区:

  • chat:write:允许机器人发送消息
  • commands:支持快捷方式功能
  • views:open:允许打开模态框
  • channels:read:读取频道基础信息(按需添加)

内容的提问来源于stack exchange,提问作者rw412

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 20:13:22