Slack bot, register click on message button

Viewed 3768

I am trying to build a Slack bot in Python. I want my bot to send a message with interactive buttons on it, and then based on which the user clicks on, run a function in Python. I do not seem to find how to do this.

My code now look like this:

message = "message"
attachments = [{"text": "message",
                "attachment_type": "default",
                "actions": [
                    {
                        "name": "list",
                        "text": "message",
                        "type": "select",
                        "options": [
                        {
                            "name": "1",
                            "text": "1",
                            "type": "button",
                            "value": "1"
                        },
                        {
                            "name": "1",
                            "text": "1",
                            "type": "button",
                            "value": "2"
                        }
                    ]}]}]

sc.api_call("chat.postMessage",
             channel=channel,
             text=message,
             attachments=attachments)

So that gives me a message with two buttons. I, however, want to run a function based on the answer the user gives.

So say that if they click 1, function1() runs and when they click 2, function2() runs.

The Slack api documentation is quite confusing about how to do this, and the "listener" they provide rtm_read() does not pick on the user clicking on one of the buttons.

So if anyone could help me with this, it would be much appreciated.

1 Answers

When you click a button in a slack conversation it's basically applying a callback. The callback is sent somewhere that you define in the App's setting, then THAT service decides what to do next with the information that's given.

  1. First you need to create a new Slack App.

  2. After it's created click on the App to go to its Basic Information page.

  3. From there, on the left side under Features find "Interactive Components".

  4. Register the two URLs that will receive the POST data from clicking on a button.

  5. Interpret the data and proceed :)

From the Slack documentation you can find their walkthrough here.

You're going to need a running web server, something simple in Flask will work just fine.

from flask import Flask, request

app = Flask('SlackReceiver')


@app.route('/slack/message', methods=['POST'])
def incoming_slack_message():
    req = request.get_json()
    # .. do something with the req ..
    return 'action successful'


@app.route('/slack/options', methods=['POST', 'OPTIONS'])
def incoming_slack_options():
    # .. idk ..
    return 'ok'


if __name__ == '__main__':
    app.run('0.0.0.0', 8088, debug=False)

...

Lastly, according to the docs you need to host this application on a web server with an HTTPS valid certificate configured. Setting up a server is beyond the scope of this question, the easiest way to get free (valid) HTTPS certs is with Let's Encrypt and certbot.

Related