Search code examples
pythonslack-apislack

Slack API: Do Something when button is clicked


I am using Python and it's Slacker API to post messages to a slack channel and it's posting the messages nicely.

Now what I want to do is create a button that says, More Info and when it's clicked, I want to show a list of items. But when the button is clicked, slackbot says oh no, something weng wrong, Please try that again

Here is an example: link

Below is my json and the code

msg = "<!here> Hello guys! "
moreInfo = ['person', 'person2', 'person3']
message = [{
"title": "Lunch time has been decided",
"text": "You will also be joining",
"actions": [
    {
        "name": "buttonName",
        "text": "More Info",
        "type": "button",
        "value": moreInfo
    }]

}]
slack.chat.post_message('#teamChannel', msg, username='my_username', attachments=message)

And this is the what it looks like in Slack when I click on More info button. enter image description here

Any help is appreciated! Thanks :)


Solution

  • Do you have a button endpoint setup already? If not, you will see that error message.

    Or if you made the same mistake I did, you are using the incorrect token. This is non-obvious from the Slack docs. You can post a message with a custom integration bot token, including using attachments (i.e. interactive buttons). However, if you want to actually respond to a button press, you need to post the message with a full-fledged Slack app token (even if you don't intend on releasing your app into the wild). That means creating an endpoint for your oauth2 flow to add your app to a Slack team, and acquiring your bot token there.

    1. Create a Slack app if you haven't already.
    2. Create an oauth GET endpoint (e.g. /slack/authorize)
    3. Set your Slack app's "Redirect URI" to your auth endpoint
    4. Your endpoint should expect a code in its query parameters. Call the Slack API oauth.access with your app's client_id, client_secret, and that code to exchange for aforementioned app token. To post your interactive message from a bot, you will need the bot token (prefixed "xoxo-").