4
votes

I have created a Slack app with an incoming webhook and slash command. The OAUTH process works as expected and I am able to successfully deploy the app, retrieve and store the app token.

I have created and sent a message (with interactive buttons) into Slack, via the app's incoming webhook. The problem is that the buttons do not operate, and generate an error message within the Slack channel.

Slack Error Message

Having read as much of the Slack docs as I can find, I note that bots sending in messages to Slack are required to include the app token in the message. I read the webhook docs in detail however and could not find a similar requirement. The webhook guide just mentions the JSON format needed but nothing re the app token. Am I missing something? The guide clearly states that webhook messages can include interactive buttons, yet the button just doesn't work. I'm creating the response as follows:

    $actions = [
        [
            "name"  => "save",
            "text"  => "Save",
            "type"  => "button",
            "value" => "save"
        ]
    ];

    $attachments = [
        [
            "fallback"      => "fallback message",
            "title"         => "Attachment 1",
            "text"          => "foobar attachment",
            "color"         => "#0066ff",
            "callback_id"   => "btn_action",
            "actions"       => $actions
        ]
    ];

    $payload = [
        "channel"       => "#test",
        "response_type" => "ephemeral",
        "icon_emoji"    => ":rocket:",
        "username"      => "Test User",
        "attachments"   => $attachments
    ];

    $data = 'payload=' . json_encode($payload);

    $ch = curl_init($webhook);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);

Would love any advice about how to resolve! Cheers, Andrew.

1

1 Answers

4
votes

I had this same problem and it was due to using a slack token that was different from the one generated when I authorized my app. I expect that slack can only identify the app if it has bot token that is unique to that app. Unfortunately, it was a non-trivial process to get this new bot access token. I'll outline it below:

  1. Select your app and the 'bot' checkbox in the "Add the Slack button" section of https://api.slack.com/docs/slack-button.

  2. Paste the url it generates in the 'a' element into your browser and then authorize yourself.

  3. Collect the code token that it adds to the url when it tries to redirect back to your application.

  4. Use the code token to fetch the bot_access_token for your app. I already had a python app using slack for oath-based login so I just modified that, but with python slackclient the code would look like this:

    SlackClient("your api token").api_call( 'oauth.access', client_id="your app client id", client_secret="your app client secret", code="the code token")

I think it works this way because 3rd-party integrations just store the bot_access_token per team and use it later. In my case, I just wanted to stuff the bot access token an environment variable for my app, which will never be in the public slack app directory. I'd love to know about an easier way to get a bot access token, so please let me know if you find one. Hope this works for you, Andrew.