2

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.

Priyanka
  • 136
  • 1
  • 2
  • 13

1 Answers1

1

Accessing the Original Message: For each interactive message, you must include a distinctive identifier (such as a timestamp or message ID) in order to be able to access the original message when processing a button click. When you send the interactive message, this identifier needs to be included in the payload. Later, Slack will add this identifier in the payload when the user presses the buttons. If necessary, you may then use this identification to get the original message back.

Sending Messages: You require an access token in order to send messages from your Slack application. The proper method for obtaining this token is OAuth2.0. You might not need to utilize the oauth.access function for delivering messages as the client.chat_postMessage method in the code you provided appears to be doing it currently. In the client instance, confirm that the token you're using has the required scopes for posting messages.

Acknowledgment Response: You must send an acknowledgment response in 3 seconds after receiving an interactive event from Slack. A 200 status code and an empty body are required for this response. This is a confirmation to Slack that you have received the event and are handling it. This is especially crucial for situations where user-facing actions, like button clicks, may be triggered.

from flask import Flask, request, jsonify
from slack_sdk import WebClient
import json

app = Flask(__name__)

# Initialize Slack client with your bot token
client = WebClient(token="your_token")

@app.route('/slack/events', methods=['POST'])
def get_auth_token():
    payload = json.loads(request.form.get('payload'))
    if payload['type'] == 'interactive_message':
        action = payload['actions'][0]['name']
        response_url = payload['response_url']  # Use this URL to send additional messages
        
        if action == 'confirm':
            original_message = payload['original_message']['text']
            # Send the original message using response_url
            client.chat_postMessage(channel=payload['channel']['id'], text=original_message)
        elif action == 'cancel':
            # Delete the original message using response_url
            delete_response = {"text": "Message deleted.", "replace_original": True}
            client.chat_postMessage(channel=payload['channel']['id'], text=delete_response)
    
    # Send acknowledgment response
    return '', 200

if __name__ == '__main__':
    app.run()

Ensure that your action_id values for the buttons match the ones you're checking in your code.

f05135
  • 56
  • 7