Manipulate Presence
Learn to use Slack API calls to manipulate presence.
We’ll cover the following endpoints from Slack’s Users API in this lesson.
users.setPresence
: This endpoint manually sets user .presence This is a flag with the value of either “auto” or “away.” When it is set to “away,” the user is shown as non-active. users.getPresence
: This endpoint gets user presence information.
Retrieve a user token
Even though our application can set its presence with our current token, this isn’t that useful because no user can see the application’s “activity” status. It’ll be a lot more useful if a user could manipulate their presence, and to do that, we’ll need a
- Go to Slack’s API website and click on your application.
- Go to the “OAuth & Permissions” tab on the left-hand sidebar.
- Now scroll down and click the “Add an OAuth Scope” for the “User Token Scopes.”
- Give the application the following scopes:
users:read
andusers:write
. - Click “reinstall your app” on the yellow notification.
- Click the “Allow” button and copy the “User OAuth Token.”
- Paste it in the code widget below.
Set the user’s presence
To set the user’s presence, we access the https://slack.com/api/users.setPresence
endpoint. This endpoint allows us to set presence manually.
Request parameters
Some important query parameters for the users.setPresence
endpoint are as follows:
Parameter | Type | Category | Description |
| token | required | Authentication tokens carry the required scopes that govern the usage of different Slack applications and APIs. We usually pass these tokens as an HTTP Authorization header or as a POST parameter. |
| string | required | This can be set to either |
Let’s call the users.setPresence
endpoint. Click the “Run” button to set the presence as “away” for yourself.
import fetch from "node-fetch";const url = new URL("https://slack.com/api/users.setPresence");const headerParameters = {Authorization: "Bearer {{USER_TOKEN}}","Content-Type": "application/json; charset=UTF-8",};const bodyParameters = JSON.stringify({presence: "away",});const options = {method: "POST",headers: headerParameters,body: bodyParameters,};async function setPresence() {try {const response = await fetch(url, options);printResponse(response);} catch (error) {printError(error);}}setPresence();
On line 11, we set the presence
as away
and call the users.setPresence
endpoint on line 22.
Response fields
The response from this endpoint only contains the ok
field indicating whether or not the request was successful.
Note: After running the code above, you should notice yourself as being set as “away” in the Slack application.
Get the user’s presence
To get the user’s presence, we access the https://slack.com/api/users.getPresence
endpoint.
Request parameters
Some important query parameters for the users.getPresence
endpoint are as follows:
Parameter | Type | Category | Description |
| token | required | Authentication tokens carry the required scopes that govern the usage of different Slack applications and APIs. We usually pass these tokens as an HTTP Authorization header or as a POST parameter. |
| string | optional | This is the user ID of the user whose presence will be retrieved. |
Let’s call the users.getPresence
endpoint. Click the “Run” button to get the current presence of a user.
import fetch from "node-fetch";const url = new URL("https://slack.com/api/users.getPresence");const headerParameters = {Authorization: "Bearer {{USER_TOKEN}}","Content-Type": "application/json; charset=UTF-8",};const options = {method: "GET",headers: headerParameters,};async function getPresence() {try {const response = await fetch(url, options);printResponse(response);} catch (error) {printError(error);}}getPresence();
On line 17, we call the user.getPresence
endpoint to get the presence we set in the previous widget.
Response fields
Some of the important fields included in a successful response from this endpoint are as follows:
Parameter | Type | Description |
| boolean | A boolean value indicating whether the request was successful or not. |
| string | The authorized user's current presence status. It can either be |
| boolean | Whether the authorized user is currently online or not. |
| boolean | Whether the user's presence was automatically set to |
| boolean | Whether the user's presence was manually set to |
| string | A timestamp of when the user was last active. |