如何根据用户响应更新松弛的交互式消息

时间:2018-12-05 03:00:38

标签: python flask slack

我是Slack API的新手,我正尝试在flask和python的帮助下使用slash命令实现交互式消息。 我陷入了最后一步,我想更新原始消息而不是替换它。

每次单击按钮发送答复时,原始消息都会被替换。

我能够从有效负载中检索original_message,但不确定如何将用户响应附加到该消息。这是我的代码:

@application.route("/summarize", methods=['POST'])    
def hello():
    attach_json = {
    "response_type": "in_channel",
    "text": "Interested in outing?",
    "attachments": [
        {
            "text": "Response",
            "fallback": "You are unable to choose any option",
            "callback_id": "confirmation",
            "color": "#3AA3E3",
            "attachment_type": "default",
            "actions": [
                {
                    "name": "confirm_btn",
                    "text": "Yes",
                    "type": "button",
                    "value": "yes"
                },
                {
                    "name": "confirm_btn",
                    "text": "No",
                    "type": "button",
                    "value": "No"
                },
                {
                    "name": "confirm_btn",
                    "text": "Can't decide",
                    "type": "button",
                    "value": "unavailable"
                }
            ]
        }
    ]
}
    print(request.get_data())
    #return Response("test", status=200)
    return Response(response=json.dumps(attach_json), status=200, mimetype="application/json")

@application.route("/actions", methods=['POST'])
def actions():
    statement = None
    slack_req = json.loads(request.form.get('payload'))
    response = '{"text": "Hi, <@' + slack_req["user"]["id"] + '>"}'

    user = slack_req["user"]["name"]
    user_response = slack_req["actions"][0]["value"]
    orig_message = slack_req["original_message"]

    if user_response == "yes":
        statement = user + "chose " + user_response


    print(slack_req)
    return Response(response=statement, status=200, mimetype="application/json")




if __name__ == "__main__":
    application.run(debug=True)

将非常感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

  

但不确定如何将用户回复附加到该消息

像这样吗?

print("user {} chose {} for original_message='{}'".format(user, user_response, original_message))

答案 1 :(得分:0)

中的以下语句
{"jsonrpc":"1.0","id":"1","method":"createrawtransaction","params":["[{\"txid\":\"1a43a1f27c5837d5319a45217aa948a4d39c1d89faf497ce59de5bd570a64a26\",\"vout\":1}]","[{\"2NAZpRsvj9BstxxCDkKoe1FVjmPPxdmvqKj\":0.01}]"]}

解决了我的查询。

var selectElement = document.getElementById("sel");
for(i = 0; i < names.length; i++) {
    opt = new Option(names[i]); 
    selectElement.options.add(opt);
}