简体   繁体   中英

How to update slack interactive message based on user response

I am new to slack API and I am trying to implement interactive messages using slash command with the help of flask and python. I am stuck at the last step where I want to update the original message instead of replacing it.

Every time I send a response on button click, the original message is replaced.

I am able to retrieve original_message from payload but not sure how to append a user response to that message. Here is my code:

@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)

Will really appreciate any help!

but not sure how to append a user response to that message

Something like this?

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

The following statement in

 def actions():  

resolved my query.

orig_message['attachments'][0]['text'] = statement + "\n"
return Response(response=json.dumps(orig_message), status=200, mimetype="application/json")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM