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. |