Slack bot, register click message button

I am trying to create 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, runs the function in Python. I don't seem to know how to do this.

Now my code is as follows:

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 this gives me a message with two buttons. However, I want to run a function based on the response that the user gives.

So say that if they press 1, function1 () starts, and when they press 2, function2 () starts.

The Slack api documentation is rather confusing on how to do this, and the “listener” they provide rtm_read () does not select the user clicking on one of the buttons.

, - , .

+4
1

, . -, , THAT , , .

  • Slack.

  • , .

  • " ".

  • URL-, POST .

  • :)

Slack .

-, - Flask .

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)

...

, - HTTPS. , () HTTPS - Let Encrypt certbot.

+4

Source: https://habr.com/ru/post/1687050/


All Articles