Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slack API: Unable to get Chat:Write:Bot to send message as custom username

Tags:

slack

I'm trying to integrate Slack with our application using their web API. I need to use the chat.postMessage endpoint with a custom username and setting as_user = false. I'm able to post messages but when I set as_user=false it doesn't work.

Example:

{
    "channel" : "1234689",
    "text" : "Hello, It's me.",
    "username": "DJDEPOLO",
    "as_user" : false
}

Every time I make that call I'm getting back an error saying I'm missing chat:write:bot. But I can not figure out how to get that scope. I've tried everything I could think of and went over their documentation several times.

I tried requesting the scope using the OAuth route and when I add chat:write:bot to the scopes I get an error saying

Invalid permissions requested

Example:

https://slack.com/oauth/v2/authorize?scope=chat:write:bot&client_id=1234&redire....

It appears that I need to use the user token to perform this action but when I request my access token I'm getting back a bot token.

Has anyone ever had to work with chat:write:bot or any scope that ends with :bot? Or am I missing something here?

like image 841
Thomas Depole Avatar asked Nov 07 '22 12:11

Thomas Depole


1 Answers

First, select your app at your apps for slack and navigate to 'OAuth & Permissions' page.

Then, click 'Update Scopes' in 'Scopes' section, scroll down, press 'Continue' and add chat:write to User Token Scopes. Then scroll down again and finish the process.

In order to get a user token, add user_scope param to your query instead of scope so that it looks like this https://slack.com/oauth/v2/authorize?user_scope=chat:write&redirect_uri=.... When you exchange the code for access token, you will receive something like this:

{
    "ok": true,
    "app_id": "A0KRD7HC3",
    "team": {
        "name": "Slack Softball Team",
        "id": "T9TK3CUKW"
    },
    "authed_user": {
        "id": "U1234",
        "scope": "chat:write",
        "access_token": "xoxp-1234",
        "token_type": "user"
    }
}

Pay attention to authed_user.access_token, as this is the token you need to send in your Authorization header.

Here is an example of POST body:

{
    "channel": "Your channel id",
    "as_user": true,
    "text": "hi there",
    "attachments": []
}

Hope it helps you.

like image 182
Maksym Lapshyn Avatar answered Nov 15 '22 06:11

Maksym Lapshyn