我正在使用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请求就不会。
有趣的是,我正在使用完全相同的方法来响应slash命令和交互式请求。它适用于第一个,但不适用于后者。
我在8000端口的Python开发服务器上以调试模式运行我的应用程序。使用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
答案 0 :(得分:0)
我在Slack API页面上找到了一个部分,该部分说明了交互式组件的运行状况!
我要遍历的所有内容都在链接至以下的响应交互部分中:https://api.slack.com/messaging/interactivity#ack
响应互动有两个步骤:
这是我用来将原始按钮消息替换为文本“ Hi Erik!”的代码。 汇入要求 从flask导入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
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