A ruby client for the Slack Web and Event APIs.
A ruby client for the Slack Web and Event APIs.
A Ruby client for the Slack Web and Events APIs. Comes with a handy command-line client, too. If you are not familiar with these concepts, you might want to watch this video.
You're reading the documentation for the next release of slack-ruby-client. Please see the documentation for the last stable release, v3.2.0 unless you're integrating with HEAD. See UPGRADING when upgrading from an older version.
Add to Gemfile.
gem 'slack-ruby-client'Run bundle install.
To integrate your bot with Slack, you must first create a new Slack App.
Once created, go to the app's Basic Info tab and grab the Client ID and Client Secret. You'll need these in order complete an OAuth Workflow. A working sample that starts a local web server and obtains a bot or a user token for your app using OAuth v2 is available in examples/oauth_v2.
Although OAuth is recommended, you can also generate an API token for your app and use it for some interactions.
Slack.configure do |config|
config.token = ENV['SLACK_API_TOKEN']
endThis sets a global default token. You can also pass a token into the initializer of both Slack::Web::Client and Slack::RealTime::Client or configure those separately via Slack::Web::Config.configure and Slack::RealTime::Config.configure. The instance token will be used over the client type token over the global default.
The following global settings are supported via Slack.configure.
| setting | description |
|---|---|
| token | Slack API token. |
| logger | An optional logger, defaults to ::Logger.new(STDOUT) at Logger::WARN level. |
The Slack Web API allows you to build applications that interact with Slack.
Here are some examples of how to use the web client with the Web API.
client = Slack::Web::Client.new
client.auth_testSend messages with chat_PostMessage.
client.chat_postMessage(channel: '#general', text: 'Hello World', as_user: true)See a fully working example in examples/hi_web.
List channels with conversations_list.
channels = client.conversations_list.channels
general_channel = channels.detect { |c| c.name == 'general' }Upload files with sequenced API calls.
This library provides a helper method files_upload_v2 that wraps the three separate API calls.
Upload a single file.
…Upload multiple files.
client.files_upload_v2(
files: [
{ filename: 'report.pdf', content: File.read('/users/me/report.pdf'), title: 'Monthly Report' },
{ filename: 'data.csv', content: File.read('/users/me/data.csv'), title: 'Raw Data' }
],
channels: ['#general'],
initial_comment: 'Here are the monthly results!'
)You can use a channel ID passed as channel_id, a single channel as channel, an array of channel IDs as channels, or a channel name or names (prefixed with #) in files_upload_v2. Lookup by name is not supported by the Slack API and this method called invokes conversations_list in order to locate the channel ID. This invocation can have a cost if you have many Slack channels and is only recommended when you intend to list channels anyway.
Note: This library includes a files_upload method that uses a deprecated endpoint files.upload that will no longer be supported on 3/11/2025.
client.files_upload(
channels: '#general',
as_user: true,
file: Faraday::Multipart::FilePart.new('/path/to/avatar.jpg', 'image/jpeg'),
title: 'My Avatar',
filename: 'avatar.jpg',
initial_comment: 'Attached a selfie.'
)You can use a channel ID or name (prefixed with #) in all functions that take a :channel argument. Lookup by name is not supported by the Slack API and the channels_id method called invokes conversations_list in order to locate the channel ID. This invocation can have a cost if you have many Slack channels. In this scenario, we encourage you to use channel id.
client.conversations_info(channel: 'C04KB5X4D') # calls conversations_infoclient.conversations_info(channel: '#general') # calls conversations_list followed by conversations_infoYou can use a user ID or name (prefixed with @) in all functions that take a :user argument. Lookup by name is not supported by the Slack API and the users_id method called invokes users_list in order to locate the user ID.
client.users_info(user: 'U092BDCLV') # calls users_infoclient.users_info(user: '@dblock') # calls users_list followed by users_infoConstructs an in-memory index of users and searches it. If you want to use this functionality, add the picky gem to your project's Gemfile.
client.users_search(user: 'dblock')Refer to the Slack Web API Method Reference for the list of all available functions.
The Web API expects certain arguments to be sent as JSON-encoded strings. With the client you can pass these args as ruby hashes or arrays and they will be converted automatically to JSON, or you can provide the JSON directly.
# As ruby objects
client.chat_postMessage(
channel: 'C123456',
text: 'Hello World',
blocks: [{type: 'section', text: {type: 'mrkdwn', text: 'Hello World'}}]
)
# As a JSON string
client.chat_postMessage(
channel: 'C123456',
text: 'Hello World',
blocks: JSON.dump([{type: 'section', text: {type: 'mrkdwn', text: 'Hello World'}}])
)
client.chat_postMessage(
channel: 'C123456',
text: 'Hello World',
blocks: '[{"type":"section","text":{"type":"mrkdwn","text":"Hello World"}}]'
)You can configure the Web client either globally or via the initializer.
Slack::Web::Client.configure do |config|
config.user_agent = 'Slack Ruby Client/3.0'
endclient = Slack::Web::Client.new(user_agent: 'Slack Ruby Client/3.0')The following settings are supported.
| setting | description |
|---|---|
| token | Slack API token. |
| user_agent | User-agent, defaults to Slack Ruby Client/version. |
| proxy | Optional HTTP proxy. |
| ca_path | Optional SSL certificates path. |
| ca_file | Optional SSL certificates file. |
| endpoint | Slack endpoint, default is https://slack.com/api. |
| logger | Optional Logger instance that logs HTTP requests. |
| timeout | Optional open/read timeout in seconds. |
| open_timeout | Optional connection open timeout in seconds. |
| default_page_size | Optional page size for paginated requests, default is 100. |
| conversations_id_page_size | Optional page size for conversations_list requests made when calculating conversation id from a conversation name, default is nil, which will use the default_page_size. |
| users_id_page_size | Optional page size for users_list requests made when calculating user id from a user name, default is nil, which will use the default_page_size. |
| default_max_retries | Optional number of retries for paginated requests, default is 100. |
| adapter | Optional HTTP adapter to use, defaults to Faraday.default_adapter. |
You can also pass request options, including timeout and open_timeout into individual calls.
client.conversations_list(request: { timeout: 180 })You can control what proxy options are used by modifying the http_proxy environment variable per Net::HTTP's documentation.
Note that Docker on OSX seems to incorrectly set the proxy, causing Faraday::ConnectionFailed, ERROR -- : Failed to open TCP connection to : (getaddrinfo: Name or service not known). You might need to manually unset http_proxy in that case, eg. http_proxy="" bundle exec ruby ./my_bot.rb.
No open issues yet, or sync has not completed.