0

编写了一个烧瓶应用程序,它可以完成一些工作(最多可以持续 3 小时),然后通过 slack 传入的 webhook 向 slack 发送响应。应用程序正在通过 slack slash 命令调用,我必须在 3000 毫秒内响应,否则会引发超时错误。工作完成后使用 webhook 发送响应,因为我无法response_url在 30 分钟后发送响应。(松弛斜线命令

from flask import Flask, request, Response, jsonify
import webbrowser
import os
import time
import json
import requests
...
some functions here
...
app = Flask(__name__)

@app.route('/tests', methods=['POST'])
def test(): 
    if "text" in request.form:
        if not request.form["text"]:
            return slackCommandResponse("No parameters provided. Please run /run-tests <url> <test_ID>")
        else:
            params = request.form["text"]
            user_id = request.form["user_id"]
            payload = {'token': ''+token+'', 'user': user_id }
            user_req = requests.get('https://slack.com/api/users.info', params=payload)
            user_data = user_req.json()
            real_name = user_data['user']['real_name']
            user_email = user_data['user']['profile']['email']
            if len(params.split()) > 2:
                return slackCommandResponse("More parameters are provided than needed. Please run /run-tests <url> <test_ID>")
            else:
                baseUrl = params.split(" ")[0]
                if not len(params.split()) > 1:
# here is the response need to be send
                    return slackCommandResponse("No test ID provided. Running all tests. You will be notified upon progress.")
# code part starting from here need to be executed after response above
                    for file in files(path):
                        file_full_path = os.path.join(path, file)
                        ID = file[:-4]
                        url=baseUrl+"&respondentid="+ID
                        apiUrl=apibase+"&respondentid="+ID
                        test_answer = 5
                        webbrowser.get(chrome_path).open(url)
                        openFile(file_full_path)
                        time.sleep(5)
                        checkAppIsRunning("AmcEngine.exe")
                        responseContent = requests.get(apiUrl)
                        respjson = responseContent.json()
                        test_result = (respjson["questions"][0]["qResponseData"]["basketContent"][0]["quantity"])
                        if test_result == test_answer:
                            slackResponse("Test "+ID+" passed")
                        else:
                            slackResponse("Test "+ID+" failed")
                else:

                    return slackCommandResponse("Cannot run single test yet")


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)

主要问题是当我在 3000 毫秒内发送 200 响应时,我的代码的其余部分不起作用。试图使用这个,但它不起作用。不确定问题出在,因为我第一次尝试做这样的事情。任何帮助表示赞赏。

4

1 回答 1

0

将此评论用于我的任务并且它有效。也许这会为某人节省时间。干杯!

于 2018-07-09T21:07:35.867 回答