Class SlackService
java.lang.Object
edu.ucsb.cs156.frontiers.services.SlackService
Service for interacting with the Slack Web API.
A Slack bot token (xoxb-...) is already associated with a specific workspace, so
the token alone is sufficient to identify where API calls go.
Note that the Slack Web API typically returns HTTP 200 even for failures; the ok
field of the response indicates success, and error contains an error code (e.g.
invalid_auth, missing_scope).
Tokens are secrets; they must never be logged.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceWaits for the given number of milliseconds; replaceable so that tests do not really wait. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringstatic final intUpper bound on the number of pages fetched for any paginated method; see MAX_USER_PAGES.static final intHow many times a rate limited (HTTP 429) call is tried again, after waiting.static final longLongest wait, in seconds, before trying a rate limited call again.static final intUpper bound on the number of pages of users fetched (200 users per page), so that a misbehaving cursor can never cause an endless loop.static final Stringstatic final StringError code used when Slack kept rate limiting a call, even after waiting as it asked.static final StringWhat to tell the instructor when removing a member fails withRESTRICTED_ACTION.static final StringSlack's error code when a workspace preference forbids the action: forconversations.kick, when the workspace does not let this bot remove members from public channels (by default only Workspace Owners and Admins may).static final Stringstatic final StringError code used when Slack could not be reached or returned an unusable response.static final String -
Constructor Summary
ConstructorsConstructorDescriptionSlackService(org.springframework.boot.web.client.RestTemplateBuilder builder) -
Method Summary
Modifier and TypeMethodDescriptionCalls the Slackauth.testmethod to check whether a bot token is valid.createPrivateChannel(String token, String name) Calls the Slackconversations.createmethod to create a private channel; the bot becomes a member of the channel it creates.createPublicChannel(String token, String name) Calls the Slackconversations.createmethod to create a public channel; the bot becomes a member of the channel it creates.voidinviteToChannel(String token, String channelId, List<String> userIds) Calls the Slackconversations.invitemethod to add users to a channel that the bot is a member of.voidjoinChannel(String token, String channelId) Calls the Slackconversations.joinmethod so that the bot is a member of a public channel, which it has to be to invite or remove members.listChannelMembers(String token, String channelId) Calls the Slackconversations.membersmethod (following pagination).listPrivateChannels(String token) Calls the Slackconversations.listmethod (following pagination) to get the private channels that the bot is a member of, including archived ones; Slack does not tell a bot about any other private channels.listPublicChannels(String token) Calls the Slackconversations.listmethod (following pagination) to get all public channels of the workspace, including archived ones (the name of an archived channel is still taken).Calls the Slackusers.listmethod (following pagination) to get all members of the workspace the token belongs to.voidremoveFromChannel(String token, String channelId, String userId) Calls the Slackconversations.kickmethod to remove a user from a channel that the bot is a member of.voidrenameChannel(String token, String channelId, String name) Calls the Slackconversations.renamemethod to rename a channel that the bot created.voidsetSleeper(SlackService.Sleeper sleeper)
-
Field Details
-
AUTH_TEST_ENDPOINT
- See Also:
-
USERS_LIST_ENDPOINT
- See Also:
-
MAX_USER_PAGES
public static final int MAX_USER_PAGESUpper bound on the number of pages of users fetched (200 users per page), so that a misbehaving cursor can never cause an endless loop.- See Also:
-
SLACK_UNREACHABLE
Error code used when Slack could not be reached or returned an unusable response.- See Also:
-
SLACK_API
- See Also:
-
CONVERSATIONS_LIST_ENDPOINT
- See Also:
-
PRIVATE_CONVERSATIONS_LIST_ENDPOINT
- See Also:
-
MAX_PAGES
public static final int MAX_PAGESUpper bound on the number of pages fetched for any paginated method; see MAX_USER_PAGES.- See Also:
-
RATE_LIMITED
Error code used when Slack kept rate limiting a call, even after waiting as it asked.- See Also:
-
RESTRICTED_ACTION
Slack's error code when a workspace preference forbids the action: forconversations.kick, when the workspace does not let this bot remove members from public channels (by default only Workspace Owners and Admins may).- See Also:
-
REMOVAL_RESTRICTED_ADVICE
What to tell the instructor when removing a member fails withRESTRICTED_ACTION.- See Also:
-
MAX_RATE_LIMIT_RETRIES
public static final int MAX_RATE_LIMIT_RETRIESHow many times a rate limited (HTTP 429) call is tried again, after waiting.- See Also:
-
MAX_RETRY_AFTER_SECONDS
public static final long MAX_RETRY_AFTER_SECONDSLongest wait, in seconds, before trying a rate limited call again.- See Also:
-
-
Constructor Details
-
SlackService
public SlackService(org.springframework.boot.web.client.RestTemplateBuilder builder)
-
-
Method Details
-
setSleeper
-
authTest
Calls the Slackauth.testmethod to check whether a bot token is valid.- Parameters:
token- the (plaintext) Slack bot token- Returns:
- the response from Slack; if
okis true,teamandteamIdidentify the workspace the token belongs to. If Slack cannot be reached, a response withokfalse and errorSLACK_UNREACHABLEis returned.
-
listUsers
Calls the Slackusers.listmethod (following pagination) to get all members of the workspace the token belongs to. This includes bots, deactivated accounts, and users who have been invited but have not yet signed in; seeSlackUser.isActivePerson().Requires the
users:readscope; emails are only included if the token also has theusers:read.emailscope.- Parameters:
token- the (plaintext) Slack bot token- Returns:
- all members of the workspace
- Throws:
SlackApiException- with the Slack error code (e.g.missing_scope) if Slack reports an error, orSLACK_UNREACHABLEif Slack cannot be reached
-
listPublicChannels
Calls the Slackconversations.listmethod (following pagination) to get all public channels of the workspace, including archived ones (the name of an archived channel is still taken). Requires thechannels:readscope.- Parameters:
token- the (plaintext) Slack bot token- Returns:
- the public channels of the workspace
- Throws:
SlackApiException- with the Slack error code if the call fails
-
listPrivateChannels
Calls the Slackconversations.listmethod (following pagination) to get the private channels that the bot is a member of, including archived ones; Slack does not tell a bot about any other private channels. Requires thegroups:readscope.- Parameters:
token- the (plaintext) Slack bot token- Returns:
- the private channels of the workspace that the bot is a member of
- Throws:
SlackApiException- with the Slack error code if the call fails
-
createPublicChannel
Calls the Slackconversations.createmethod to create a public channel; the bot becomes a member of the channel it creates. Requires thechannels:managescope.- Parameters:
token- the (plaintext) Slack bot tokenname- name of the channel (lowercase letters, digits, hyphens and underscores)- Returns:
- the new channel
- Throws:
SlackApiException- with the Slack error code (e.g.name_taken,invalid_name_specials) if the call fails
-
createPrivateChannel
Calls the Slackconversations.createmethod to create a private channel; the bot becomes a member of the channel it creates. Requires thegroups:writescope.- Parameters:
token- the (plaintext) Slack bot tokenname- name of the channel (lowercase letters, digits, hyphens and underscores)- Returns:
- the new channel
- Throws:
SlackApiException- with the Slack error code (e.g.name_taken) if the call fails
-
renameChannel
Calls the Slackconversations.renamemethod to rename a channel that the bot created. Requires thechannels:managescope for a public channel, and thegroups:writescope for a private one.- Parameters:
token- the (plaintext) Slack bot tokenchannelId- id of the channelname- the new name of the channel- Throws:
SlackApiException- with the Slack error code (e.g.name_taken,not_authorized) if the call fails
-
joinChannel
Calls the Slackconversations.joinmethod so that the bot is a member of a public channel, which it has to be to invite or remove members. Joining a channel the bot is already in is not an error. Requires thechannels:joinscope.- Parameters:
token- the (plaintext) Slack bot tokenchannelId- id of the channel- Throws:
SlackApiException- with the Slack error code if the call fails
-
listChannelMembers
Calls the Slackconversations.membersmethod (following pagination). Requires thechannels:readscope for a public channel, and thegroups:readscope for a private one.- Parameters:
token- the (plaintext) Slack bot tokenchannelId- id of the channel- Returns:
- the Slack user ids of the members of the channel
- Throws:
SlackApiException- with the Slack error code if the call fails
-
inviteToChannel
Calls the Slackconversations.invitemethod to add users to a channel that the bot is a member of. Slack adds either all of the users or none of them. Requires thechannels:managescope for a public channel, and thegroups:writescope for a private one.- Parameters:
token- the (plaintext) Slack bot tokenchannelId- id of the channeluserIds- Slack user ids; at most 1000- Throws:
SlackApiException- with the Slack error code (e.g.already_in_channel) if the call fails
-
removeFromChannel
Calls the Slackconversations.kickmethod to remove a user from a channel that the bot is a member of. Requires thechannels:managescope for a public channel, and thegroups:writescope for a private one. Whether the bot is allowed to remove members is also a workspace setting; if it is not, Slack answersRESTRICTED_ACTION, and for public channelsREMOVAL_RESTRICTED_ADVICEsays how to fix that.- Parameters:
token- the (plaintext) Slack bot tokenchannelId- id of the channeluserId- Slack user id- Throws:
SlackApiException- with the Slack error code (e.g.restricted_action) if the call fails
-