Slack 上未显示交互式响应

Interactive response not shown on Slack

我正在使用 Python 和 Flask 开发一个简单的 Slack 应用程序。 它应该使用包含按钮的消息来响应斜杠命令。它响应用户点击按钮。

问题是:点击按钮后,交互消息请求的响应消息没有发布到Slack频道。

详情

单击按钮后,我可以在 Python 控制台上看到请求,例如

127.0.0.1 - - [24/Jun/2019 17:30:09] "POST /interactive HTTP/1.1" 200 -

而且我可以在我的 ngrok 检查页面上看到我的应用响应此请求:

HTTP/1.0 200 OK    
Content-Type: application/json    
Content-Length: 25    
Server: Werkzeug/0.14.1 Python/3.7.0    
Date: Mon, 24 Jun 2019 15:41:57 GMT

{
  "text": "Hi there"
}

但响应消息不会显示在 Slack 上。 Slack 上也没有显示服务错误,因此 Slack 似乎收到了 200 OK 响应。

此外,如果我将交互式响应发送到 response_url,它也能正常工作。只是试图直接响应 HTTP 请求并没有。

有趣的是,我使用完全相同的方法来响应斜杠命令和交互式请求。它适用于第一个但不适用于后者。

设置

我 运行 我的应用程序处于 Python 端口 8000 上的开发服务器上的调试模式。服务器使用 ngrok 暴露给 Slack。 ngrok 将我的外部 URL 映射到 localhost:8000。该应用程序是从 Visual Studio 代码中启动的。

请求 URL 已正确配置到斜线命令和交互操作的相应端点。

代码

import requests
from flask import Flask, json

app = Flask(__name__) #create the Flask app

@app.route('/slash', methods=['POST'])
def slash_response():                
    """ endpoint for receiving all slash command requests from Slack """

    # blocks defintion from message builder
    # converting from JSON to array
    blocks = json.loads("""[
        {
            "type": "section",
            "text": {
                "type": "plain_text",
                "text": "Please select an option:",
                "emoji": true
            }
        },
        {
            "type": "actions",
            "elements": [
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "Click me",
                        "emoji": true
                    },
                    "value": "button_1"
                }
            ]
        }
    ]""")

    # compose response message    
    response = {
        "blocks": blocks
    }

    ## convert response message into JSON and send back to Slack
    return json.jsonify(response)

@app.route('/interactive', methods=['POST'])
def interactive_response():                
    """ endpoint for receiving all interactivity requests from Slack """

    # compose response message    
    response = {
        "text": "Hi there"
    }

     ## convert response message into JSON and send back to Slack
    return json.jsonify(response)

if __name__ == '__main__':
    app.run(debug=True, port=8000) #run app in debug mode on port 8000

我在 Slack API 页面上找到了一个部分,它解释了您的交互式组件的运行情况!

我要讲的所有内容都在官方文档的 Responding to the interaction 部分。

响应互动有两个步骤:

  1. 确认响应 - 在收到请求负载后 3 秒内将 OK 200 发送回 slack。您随此响应发送的文本不会改变当前松弛消息的内容。
  2. 组合响应 - 使用 POST 请求将更新后的消息发送到 slack,并将 response_url 打包在请求负载中。

所以 OP 的代码不起作用的原因是 Slack 不再接受完整的消息作为对交互的直接响应。相反,所有响应消息都需要发送到 response_url.

但是,为了实现向后兼容性,仍然可以直接响应包含附件的消息,但不能响应包含布局块的消息。

代码

这是我用来用文本 "Hi Erik!"

替换原始按钮消息的代码
import requests
from flask import Flask, json, request

app = Flask(__name__) #create the Flask app

@app.route('/slash', methods=['POST'])
def slash_response():                
    """ endpoint for receiving all slash command requests from Slack """

    # blocks defintion from message builder
    # converting from JSON to array
    blocks = json.loads("""[
        {
            "type": "section",
            "text": {
                "type": "plain_text",
                "text": "Please select an option:",
                "emoji": true
            }
        },
        {
            "type": "actions",
            "elements": [
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "Click me",
                        "emoji": true
                    },
                    "value": "button_1"
                }
            ]
        }
    ]""")

    # compose response message    
    response = {
        "blocks": blocks
    }

    ## convert response message into JSON and send back to Slack
    return json.jsonify(response)

@app.route('/interactive', methods=['POST'])
def interactive_response():                
    """ endpoint for receiving all interactivity requests from Slack """
    # compose response message   
    data = json.loads(request.form["payload"])
    response = {
        'text': 'Hi Erik!', 
        }

    requests.post(data['response_url'], json=response)
    return '' 


if __name__ == '__main__':
    app.run(debug=True, port=8000) #run app in debug mode on port 8000