2am: Slack: Post as Yourself from the Terminal
As we reach 2am in our '24 Hours in the Terminal' exploration, we've got a new exciting challenge: interacting with Slack. We've seen how bots send automated messages, but have you ever thought of sending messages as a user? Yes, right from the terminal! It's not just bots that can have all the fun.
What does this article replace?
This article offers a terminal-based solution to interacting with Slack, thus replacing the need for the standard Slack GUI client.
How long will it take to set up?
This setup should take you about 30 minutes to 1 hour, depending on your familiarity with terminal commands and Slack APIs.
4 out of 5 stars
Difficulty Rating:
Being able to manipulate Slack directly from the command line scores a solid 4/5 on the difficulty level scale. It's a great step towards mastering terminal-based communications.
Step 1: Understanding the language of tokens
First things first, let's talk about Slack API and tokens. Tokens are like secret keys, providing you with the ability to access and interact with Slack's API. When you create a Slack app and install it in your workspace, you are handed your personal token.
Bots usually have bot tokens, starting with xoxb-
. But for our little experiment, we require a user token, which is going to begin with xoxp-
. Remember, this token is linked to you, as the installer of the app.
Obtaining your Slack user token
To get your Slack user token, follow the steps below:
-
Visit the Slack API website and sign in to your Slack account.
-
Click on 'Your Apps' in the top right corner.
-
If you already have an app, click on it. If not, click 'Create New App', give it a name, select the workspace you want it in, and click 'Create App'.
-
In the app settings, click on 'OAuth & Permissions' in the left-hand side menu.
-
Scroll down to the section named 'Scopes'. Here, you need to add the necessary scopes to use the API methods. To send messages, you need the chat:write:user scope for user tokens. Click on 'Add an OAuth Scope', type in chat:write:user, and hit 'Enter'.
-
After adding the scopes, scroll back up to the top of the 'OAuth & Permissions' page. Here, you'll see a button that says 'Install App to Workspace' - click it.
-
Slack will ask for confirmation. After confirming, you'll be redirected to a page that contains your tokens. The 'OAuth Tokens for Your Workspace' section contains your 'User OAuth Token', which will start with xoxp-.
Remember to keep your tokens safe! Your user token is tied to your workspace permissions and can grant access to data, so don't share it or publish it anywhere.
Now that you have your Slack user token, you can continue to the next step - sending messages as a user.
Step 2: Sending messages as a user
Now that we understand tokens, let's look at how to send messages as a user. This can be done using the chat.postMessage
API method, in combination with your user token and the as_user
parameter.
In your terminal, input the following command. Ensure to replace 'your-token'
with your user token and 'C061EG9SL'
with the ID of the channel you want to post in.
curl -X POST -H 'Authorization: Bearer your-token' -H 'Content-type: application/json' --data '{
"channel":"C061EG9SL",
"text":"Hello, world!",
"as_user": true
}' https://slack.com/api/chat.postMessage
Step 3: Keep in mind the limitations
While the ability to send messages as a user offers certain benefits, it's important to remember that Slack, starting from February 21, 2020, has been transitioning away from chat:write:user
scope and as_user
parameter for newly created apps. For these, chat:write
scope is recommended, and the messages sent via the API will be attributed to the app, not the user.
Also, remember to safeguard your user token. It's tied to your workspace permissions and can grant access to data, so keep it under wraps!
Step 4: Responsible communication
Although posting as a user can be handy for specific scenarios, it's crucial to maintain a transparent and respectful communication atmosphere within your Slack workspace. Be mindful of your interactions and use this feature responsibly.
Extra: To send more sophisticated messages or to interact more deeply with the Slack API, you can explore Slack's official API documentation. Whether you want to send attachments, handle interactive components like buttons, or even build out full-featured apps, the Slack API can provide the tools you need.
We've now unlocked a new dimension of terminal-based communication: interacting with Slack as a user. Stay tuned for more insights as we continue our journey through '24 Hours in the Terminal'.