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

如何通过Flask向Slack文本框写入内容且不自动发送?开发求助

实现Slack文本框预填充内容(不自动发送)

Slack官方API没有直接提供「预填充用户输入框」的接口,但可以通过交互式消息+Deep Link引导的方式间接实现,结合Flask的示例如下:


Flask后端代码示例

from flask import Flask, request, jsonify
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError

app = Flask(__name__)
# 替换为你的Slack Bot Token
slack_client = WebClient(token="YOUR_SLACK_BOT_TOKEN")

@app.route("/prepopulate_message", methods=["POST"])
def prepopulate_message():
    # 定义需要用户审核的预填充内容
    message_content = "这里是要填充到输入框的文本内容"
    
    # 发送带交互按钮的提示消息
    try:
        response = slack_client.chat_postMessage(
            channel="#general",
            text="请点击下方按钮将内容填充至输入框:",
            blocks=[
                {
                    "type": "actions",
                    "elements": [
                        {
                            "type": "button",
                            "text": {"type": "plain_text", "text": "预填充消息"},
                            "action_id": "trigger_prepopulate",
                            "value": message_content
                        }
                    ]
                }
            ]
        )
        return jsonify({"status": "success"})
    except SlackApiError as e:
        return jsonify({"error": e.response["error"]}), 500

@app.route("/slack/interactive", methods=["POST"])
def handle_interaction():
    payload = request.get_json()
    if payload["type"] == "block_actions":
        action = payload["actions"][0]
        if action["action_id"] == "trigger_prepopulate":
            # 用Slack Deep Link实现输入框预填充
            deep_link = f"slack://msg?text={action['value']}&channel={payload['channel']['id']}"
            
            # 返回仅用户可见的引导消息
            return jsonify({
                "response_type": "ephemeral",
                "text": f"点击链接填充消息:<{deep_link}|立即填充>"
            })
    return jsonify({"status": "ok"})

if __name__ == "__main__":
    app.run(debug=True)

核心说明

  • 利用Slack的slack://msg协议Deep Link,用户点击链接后会自动跳转至对应频道的输入框并填充指定文本,完全由用户自主决定是否发送。
  • 需在Slack应用后台开启「交互式组件」权限,并配置请求URL为你的Flask服务的/slack/interactive路径。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 22:43:17