gpt4 book ai didi

python-3.x - Slack 上未显示交互式响应

转载 作者:行者123 更新时间:2023-12-05 03:00:33 31 4
gpt4 key购买 nike

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

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

详情

单击按钮后,我可以在我的 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 上以 Debug模式运行我的应用程序。服务器使用 ngrok 暴露给 Slack。 ngrok 将我的外部 URL 映射到 localhost:8000。该应用从 Visual Studio Code 中启动。

请求 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 请求和它们打包在请求负载中的 response_url 将更新后的消息发送到 slack。

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

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

代码

这是我用来用文本“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

关于python-3.x - Slack 上未显示交互式响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56740445/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com