slack-api
slack-cr
A Crystal shard for making Slack Web API calls. Most (but not all) API calls have been implemented. If there are calls missing for what you want to do, either send a request to the contributors or (ideally) submit a pull request.
Installation
Add this to your application's shard.yml
:
dependencies:
slack-api:
github: crystal-bits/slack-api
Usage
In order to make any Slack API call you need to set up a Slack app, and create an OAuth user token and optionally a Bot user token. These tokens are described here. The user token needs to be set in environment variable SLACK_OAUTH_TOKEN
. If you are using the Bot user token (for example to send messages as the Bot user rather than the user that installed the app), you need to set it in environment variable SLACK_BOT_TOKEN
.
From there it is pretty straight forward. A class has been defined for each of the supported Slack API calls. The instance variables of the class correspond to the parameters defined for the Slack API method being called. You simply instantiate the class and call the #submit
method on that object.
For example:
require "slack-api"
req = Slack::Request::APITest.new
resp = req.submit
The response object will contain all the fields as defined in the Slack API docs.
For examples of specific calls, refer to spec tests.
Checking OAuth scopes
As you will be aware, access to individual Slack API methods is controlled by OAuth scope. This shard can optionally check whether or not the current OAuth token has been granted the required OAuth scope prior to making the call. In order for this checking to occur simply do the following:
Slack::Request.check_scope = true
Throttling (aka Rate Limits)
Slack imposes rate limits on most of the API requests. This shard handles any response from Slack as per Slack docs and resubmits requests (after sleeping for the directed duration) automatically as necessary.
Logging
The shard has been set up to log various events in case you're interested. I mainly used it during development but you may find it useful. There are two options for logging. Either you provide your own Log
instance as follows:
Slack::Request.logger = my_logger
or have the shard create a 'default' logger for you:
Slack::Request.set_default_logger
The following messages will be logged. The examples are from the default logger:
Submitting a Slack API http request
- Level =
Log::Severity::INFO
I|14:33:03.369|00.019301|00.019301|Submitting api.test
Dump request parameters before POST
- Level =
Log::Severity::DEBUG
D|14:33:17.714|00.333320|00.000037|params: {"exclude_archived":true,"limit":20}
Response from Slack API http request
- Level =
Log::Severity::DEBUG
D|14:33:18.036|00.654655|00.321335|Got response: 200
Rate Limit response
- Level =
Log::Severity::WARN
W|14:33:18.997|01.615490|00.309931|chat.postMessage : Got TOO_MANY_REQUESTS response. Waiting for 5 seconds before retry.
Class to API mapping
Here is a list of the supported Slack API method calls and the corresponding slack-api
class.
Channels Methods
- channels.archive :
Slack::Request::ChannelsArchive
- channels.create :
Slack::Request::ChannelsCreate
- channels.history :
Slack::Request::ChannelsHistory
- channels.info :
Slack::Request::ChannelsInfo
- channels.invite :
Slack::Request::ChannelsInvite
- channels.join :
Slack::Request::ChannelsJoin
- channels.kick :
Slack::Request::ChannelsKick
- channels.leave :
Slack::Request::ChannelsLeave
- channels.list :
Slack::Request::ChannelsList
- channels.mark :
Slack::Request::ChannelsMark
- channels.rename :
Slack::Request::ChannelsRename
- channels.replies :
Slack::Request::ChannelsReplies
- channels.setPurpose :
Slack::Request::ChannelsSetPurpose
- channels.setTopic :
Slack::Request::ChannelsSetTopic
- channels.unarchive :
Slack::Request::ChannelsUnarchive
Chat Methods
- chat.delete :
Slack::Request::ChatDelete
- chat.getPermalink :
Slack::Request::ChatGetPermalink
- chat.meMessage :
Slack::Request::ChatMeMessage
- chat.postEphemeral :
Slack::Request::ChatPostEphemeral
- chat.postMessage :
Slack::Request::ChatPostMessage
- chat.update :
Slack::Request::ChatUpdate
Conversations Methods
- conversations.archive :
Slack::Request::ConversationsArchive
- conversations.close :
Slack::Request::ConversationsClose
- conversations.create :
Slack::Request::ConversationsCreate
- conversations.history :
Slack::Request::ConversationsHistory
- conversations.info :
Slack::Request::ConversationsInfo
- conversations.invite :
Slack::Request::ConversationsInvite
- conversations.join :
Slack::Request::ConversationsJoin
- conversations.kick :
Slack::Request::ConversationsKick
- conversations.leave :
Slack::Request::ConversationsLeave
- conversations.list :
Slack::Request::ConversationsList
- conversations.members :
Slack::Request::ConversationsMembers
- conversations.open :
Slack::Request::ConversationsOpen
- conversations.rename :
Slack::Request::ConversationsRename
- conversations.replies :
Slack::Request::ConversationsReplies
- conversations.setPurpose :
Slack::Request::ConversationsSetPurpose
- conversations.setTopic :
Slack::Request::ConversationsSetTopic
- conversations.unarchive :
Slack::Request::ConversationsUnarchive
Groups Methods
- groups.archive :
Slack::Request::GroupsArchive
- groups.createChild :
Slack::Request::GroupsCreateChild
- groups.create :
Slack::Request::GroupsCreate
- groups.history :
Slack::Request::GroupsHistory
- groups.info :
Slack::Request::GroupsInfo
- groups.invite :
Slack::Request::GroupsInvite
- groups.kick :
Slack::Request::GroupsKick
- groups.leave :
Slack::Request::GroupsLeave
- groups.list :
Slack::Request::GroupsList
- groups.mark :
Slack::Request::GroupsMark
- groups.open :
Slack::Request::GroupsOpen
- groups.rename :
Slack::Request::GroupsRename
- groups.replies :
Slack::Request::GroupsReplies
- groups.setPurpose :
Slack::Request::GroupsSetPurpose
- groups.setTopic :
Slack::Request::GroupsSetTopic
- groups.unarchive :
Slack::Request::GroupsUnarchive
IM Methods
- im.close :
Slack::Request::IMClose
- im.history :
Slack::Request::IMHistory
- im.list :
Slack::Request::IMList
- im.mark :
Slack::Request::IMMark
- im.open :
Slack::Request::IMOpen
- im.replies :
Slack::Request::IMReplies
MPIM Methods
- mpim.close :
Slack::Request::MPIMClose
- mpim.history :
Slack::Request::MPIMHistory
- mpim.list :
Slack::Request::MPIMList
- mpim.mark :
Slack::Request::MPIMMark
- mpim.open :
Slack::Request::MPIMOpen
- mpim.replies :
Slack::Request::MPIMReplies
Misc Methods
- api.test :
Slack::Request::APITest
Pins Methods
- pins.add :
Slack::Request::PinsAdd
- pins.list :
Slack::Request::PinsList
- pins.remove :
Slack::Request::PinsRemove
User Groups Methods
- usergroups.create :
Slack::Request::UserGroupsCreate
- usergroups.disable :
Slack::Request::UserGroupsDisable
- usergroups.enable :
Slack::Request::UserGroupsEnable
- usergroups.list :
Slack::Request::UserGroupsList
- usergroups.update :
Slack::Request::UserGroupsUpdate
- usergroups.users.list :
Slack::Request::UserGroupsUsersList
- usergroups.users.update :
Slack::Request::UserGroupsUsersUpdate
User Methods
- users.conversations :
Slack::Request::UsersConversations
- users.getPresence :
Slack::Request::UsersGetPresence
- users.info :
Slack::Request::UsersInfo
- users.list :
Slack::Request::UsersList
- users.lookupByEmail :
Slack::Request::UsersLookupByEmail
- users.profile.get :
Slack::Request::UsersProfileGet
Contributing
- Fork it (https://github.com/crystal-bits/slack-api/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- stbaldwin Steve Baldwin - creator, maintainer
- skaragianis Steffan Karagianis - maintainer
slack-api
- 0
- 0
- 0
- 1
- 0
- over 4 years ago
- November 2, 2018
MIT License
Mon, 25 Nov 2024 00:50:17 GMT