Tuesday 15 August 2023

Handle button click events in Interactive messages using Slack API with Python Flask

I am trying to build a Slack app where when a message is sent with a command, an ephemeral message with two buttons Confirm and Cancel is shown. If the user clicks on Confirm, the message should be sent and if they click on Cancel, it should be deleted.

I have written endpoint to handle the events and I have added it under Interactivity. I receive the payload in my endpoint but I have two issues here:

  1. Since this is an ephemeral message, the payload does not have the original message and do not know how to send it when the Confirm button is clicked. In place of "confirm worked", I want to send the original message.
 payload = request.form["payload"]
 payload = json.loads(payload)
 if payload['type'] == 'interactive_message':
   action = payload['actions'][0]['name']

   if action == 'confirm':
      client.chat_postMessage(channel="#channelname", text="confirm worked")
  1. In the event handler, when I send the message using the method chat_postMessage I see the error "Oh no, something went wrong. Please try that again." I understand I should get the access token using OAuth2.0 for sending this message by calling the oauth.access. I wrote this endpoint but I am confused on how to use it? Should I call this before the client.chat_postMessage above? If yes, what is the code I should send as a part of the request?
@app.route('/slack/authorize/', methods=['POST'])
def get_auth_token():
    payload = request.form["payload"]
    payload = json.loads(payload)
    code = payload["code"]
    result = client.oauth_v2_access(CLIENT_ID, SIGNING_SECRET, code, "URL/slack/authorize/")
    return jsonify(result), 200

I have also read in the documentation that I need to send an acknowledgement response within 3 seconds. Could you please help me connect all these pieces? Thank you in advance.



from Handle button click events in Interactive messages using Slack API with Python Flask

No comments:

Post a Comment