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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With