如何使用 Flask 在 Slack 应用程序中 运行 对话框?

How to run dialog in slack app with flask?

我正在创建与 Slack 集成的待办事项应用程序。我需要使用 dialog.open 属性 的 slack。

我设法完成了 slack api 教程,但最终无法理解对话框如何与外部系统集成。我创建了在 slack 中的斜杠命令之后运行的代码。它应该打开对话框并将其显示给用户,但事实并非如此。我打印了部分代码以查看内部发生了什么 - 看起来整个代码都有效并且服务器 returns 200.

@app.route('/helpdesk', methods=['POST'])
def helpdesk():
    print(request.form)

    api_url = 'https://slack.com/api/dialog.open'

    user_id = request.form['user_id']
    trigger_id = request.form['trigger_id']

    dialog = {
    "token": "J1llSAeQAxNyw8yc37xuEsad",
    "trigger_id": trigger_id,
    "dialog": {
        "callback_id": "ryde-46e2b0",
        "title": "Request a Ride",
        "submit_label": "Request",
        "notify_on_cancel": True,
        "state": "Limo",
        "elements": [
            {
                "type": "text",
                "label": "Pickup Location",
                "name": "loc_origin"
            },
            {
                "type": "text",
                "label": "Dropoff Location",
                "name": "loc_destination"
            }
        ]
    }
    }

    print(dialog)
    requests.post(api_url, data=dialog)

    return make_response()

我希望在 slack 中编写斜杠命令后看到对话框 window。 我在印刷品中看到的内容:

ImmutableMultiDict([('token', 'J1llSAeQAxNyw8yc37xuEsad'), ('team_id', 'TKWQ5QP7Y'), ('team_domain', 'team-learningslack'), ('channel_id', 'CKH7RSZPC'), ('channel_name', 'slackflask'), ('user_id', 'UKN9KU7JM'), ('user_name', 'konrad.marzec1991'), ('command', '/musi'), ('text', ''), ('response_url', 'https://hooks.slack.com/commands/TKWQ5QP7Y/664885241506/ABjpMYmTWrnXpSBoGMpaJtOV'), ('trigger_id', '669947662833.676821839270.6c4bddd1418d3d4f2c8626f7c9accdf7')])

{'token': 'J1llSAeQAxNyw8yc37xuEsad', 'trigger_id': '669947662833.676821839270.6c4bddd1418d3d4f2c8626f7c9accdf7', 'dialog': {'callback_id': 'ryde-46e2b0', 'title': 'Request a Ride', 'submit_label': 'Request', 'notify_on_cancel': True, 'state': 'Limo', 'elements': [{'type': 'text', 'label': 'Pickup Location', 'name': 'loc_origin'}, {'type': 'text', 'label': 'Dropoff Location', 'name': 'loc_destination'}]}}
127.0.0.1 - - [26/Jun/2019 00:15:35] "POST /helpdesk HTTP/1.1" 200 -

您的代码中有 2 个问题:

  • 您需要在调用中使用访问令牌,而不是验证令牌 至 dialog.open
  • 您需要将对话框定义作为 JSON 发送,而不是作为数组形式

我进行了这些额外的更改 - 添加了使用定义为环境变量的松弛令牌的代码 - 使用 get() 方法从请求中访问表单参数 - 添加代码以显示来自 dialog.open

的 API 响应

这是您的代码的更正版本:

import os
import requests
from flask import Flask, json, request

app = Flask(__name__) #create the Flask app

@app.route('/helpdesk', methods=['POST'])
def helpdesk():

    api_url = 'https://slack.com/api/dialog.open'

    trigger_id = request.form.get('trigger_id')

    dialog = {
        "callback_id": "ryde-46e2b0",
        "title": "Request a Ride",
        "submit_label": "Request",
        "notify_on_cancel": True,
        "state": "Limo",
        "elements": [
            {
                "type": "text",
                "label": "Pickup Location",
                "name": "loc_origin"
            },
            {
                "type": "text",
                "label": "Dropoff Location",
                "name": "loc_destination"
            }
        ]
    }

    api_data = {
        "token": os.environ['SLACK_TOKEN'],
        "trigger_id": trigger_id,
        "dialog": json.dumps(dialog)
    }

    res = requests.post(api_url, data=api_data)
    print(res.content)

    return make_response()

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