7

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 :)

Parth Bhoiwala
  • 1,282
  • 3
  • 19
  • 44
  • Hi @Parth, could you please explain why you wrote a Python code to accomplish this task? And where are you running this script? – Agent 0 Sep 23 '19 at 18:50
  • 1
    @Kourosh I was using [Slacker](https://github.com/os/slacker) which is a Python wrapper for Slack API. And I had the script deployed on one of our company's local servers. – Parth Bhoiwala Sep 23 '19 at 19:49
  • 1
    Interesting, but I am using Elasticsearch to query my data and if I get any failures, then I post slack notification in my channel, so I don't think running a script would work in my case. Thanks for your response though. – Agent 0 Sep 23 '19 at 21:48

1 Answers1

7

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-").
Keller
  • 17,051
  • 8
  • 55
  • 72
  • 6
    Thanks, you're correct, I had to create an endpoint. The Slack API documentation is kinda unorganized, it was hard to figure it out. – Parth Bhoiwala Sep 08 '16 at 18:01
  • I have a question regarding that. According to the api doc, the response to a user button action replaces the original message. – ramin Mar 22 '17 at 01:17
  • @ramin You are not required to replace the original message, but you can by appending the original message's message_ts (timestamp) to the chat.update API. – Keller Mar 22 '17 at 13:58
  • 3
    @ParthBhoiwala Yes it's a sh*t documentation on Slack's part! – deepdive Dec 01 '17 at 04:23
  • Hi, what is the code that needs to be sent? The user has already logged in right? So, why would they be redirected to Authorize the app again? There is an access token under App Configuration tokens. That doesn't work either. Could you please clarify how to get the right access token. – Priyanka Aug 09 '23 at 17:32