slack-bot-api icon indicating copy to clipboard operation
slack-bot-api copied to clipboard

Give an example with buttons and response handling

Open michaelkruglos opened this issue 6 years ago • 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.

michaelkruglos avatar Sep 11 '19 18:09 michaelkruglos

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

madhuracharya avatar Oct 04 '19 04:10 madhuracharya

Follow the tutorial of this link https://api.slack.com/tutorials/intro-to-message-buttons

leomozzer avatar Oct 30 '19 20:10 leomozzer

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

chiumax avatar Jan 05 '20 19:01 chiumax

@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 avatar Jan 22 '20 09:01 prashant1k99

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

aliehsan90 avatar May 07 '20 14:05 aliehsan90

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

prashant1k99 avatar May 09 '20 03:05 prashant1k99