mishk0/slack-bot-api

Give an example with buttons and response handling

Opened this issue · 6 comments

Please, give an example of how to implement a slack bot, which proposes a few options by displaying buttons, and handles the button clicks.

The RTM API doesn't support interactive messages. You will have to perform these using web API.
Please refer to this page for more information
https://api.slack.com/messaging/interactivity/enabling#responding_to_interactions

Here, just add the attachment directly to params for postMessageToChannel.

Example:

    const params = {
        icon_emoji: ':smiley:',
        "text": "Would you like to play a game?",
        "attachments": [
            {
                "text": "Choose a game to play",
                "fallback": "You are unable to choose a game",
                "callback_id": "wopr_game",
                "color": "#3AA3E3",
                "attachment_type": "default",
                "actions": [
                    {
                        "name": "game",
                        "text": "Chess",
                        "type": "button",
                        "value": "chess"
                    },
                    {
                        "name": "game",
                        "text": "Falken's Maze",
                        "type": "button",
                        "value": "maze"
                    },
                    {
                        "name": "game",
                        "text": "Thermonuclear War",
                        "style": "danger",
                        "type": "button",
                        "value": "war",
                        "confirm": {
                            "title": "Are you sure?",
                            "text": "Wouldn't you prefer a good game of chess?",
                            "ok_text": "Yes",
                            "dismiss_text": "No"
                        }
                    }
                ]
            }
        ]

    };
    app.postMessageToChannel(
        'general',
        'What\'s up laddies!', params
    )

Screenshot:
image

@dumblole How can I listen to the response of the button clicked by the user and take actions regarding that in interactive messages.
In simple words, which method is suitable for listening to response of the interactive blocks.

So as per your example if the user clicks Chess button, I want it to send to the server and based on that I want to respond with another message.

Such as responding with another interactive message of opening a modal.

@prashant1k99 Did you found a solution for listening to the response?

@aliehsan90 not yet, if you find it then please do tell us.