Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

slack-ruby-client

> 编程语言
Open source

A ruby client for the Slack Web and Event APIs.

1.3K stars0 likes1 views
WebsiteGitHub

About

A ruby client for the Slack Web and Event APIs.

Slack Ruby Client

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.

Table of Contents

  • Useful to Me?
  • Stable Release
  • Installation
  • Usage
    • Create a New Bot Integration
    • OAuth Code Grant
    • Using an API Token
    • Global Settings
    • Web Client
      • Web Client Examples
        • Test Auth
        • Send Messages
        • List Channels
        • Upload a File
        • Get Channel Info
        • Get User Info
        • Search for a User
        • Other
      • JSON Arguments
      • Web Client Options
      • Pagination Support
      • Character Encoding
      • Error Handling
        • Slack Errors
        • Rate Limiting
        • Other Errors
    • Events API
      • Configuring Slack::Events
      • Verifying the Request Signature
    • Message Handling
      • Formatting Messages
        • Date and Time Formatting
        • Channel ID Formatting
        • User ID Formatting
        • Group ID Formatting
        • URL Formatting
        • Markdown Formatting
      • Parsing Messages
        • Unescaping Message Content
        • Escaping Message Content
    • Command-Line Client
      • Authenticate with Slack
      • Send a Message
      • Get Channel Info
      • List Users
  • Sponsorship and Enterprise Support
  • History
  • Security
  • Contributing
  • Copyright and License

Useful to Me?

  • This library lets you send messages to Slack via the Web API and facilitates integration with the Events API.
  • To write a complete bot for Slack you need more than this library, and it's much easier to start with slack-ruby-bot-server-events.
  • To respond to slash commands, interactive components, or events at the lowest level, implement a web application using your favorite web framework, and use this library to call the Slack Web API, and to verify that events are coming from Slack.

Stable Release

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.

Installation

Add to Gemfile.

gem 'slack-ruby-client'

Run bundle install.

Usage

Create a New Bot Integration

To integrate your bot with Slack, you must first create a new Slack App.

OAuth Code Grant

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.

Using an API Token

Although OAuth is recommended, you can also generate an API token for your app and use it for some interactions.

ruby
Slack.configure do |config|
  config.token = ENV['SLACK_API_TOKEN']
end

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

Global Settings

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.

Web Client

The Slack Web API allows you to build applications that interact with Slack.

Web Client Examples

Here are some examples of how to use the web client with the Web API.

Test Auth
ruby
client = Slack::Web::Client.new
client.auth_test
Send Messages

Send messages with chat_PostMessage.

ruby
client.chat_postMessage(channel: '#general', text: 'Hello World', as_user: true)

See a fully working example in examples/hi_web.

List Channels

List channels with conversations_list.

ruby
channels = client.conversations_list.channels

general_channel = channels.detect { |c| c.name == 'general' }
Upload a File

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.

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

ruby
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.'
)
Get Channel Info

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.

ruby
client.conversations_info(channel: 'C04KB5X4D') # calls conversations_info
ruby
client.conversations_info(channel: '#general') # calls conversations_list followed by conversations_info
Get User Info

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

ruby
client.users_info(user: 'U092BDCLV') # calls users_info
ruby
client.users_info(user: '@dblock') # calls users_list followed by users_info
Search for a User

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

ruby
client.users_search(user: 'dblock')
Other

Refer to the Slack Web API Method Reference for the list of all available functions.

JSON Arguments

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.

ruby
# 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"}}]'
)

Web Client Options

You can configure the Web client either globally or via the initializer.

ruby
Slack::Web::Client.configure do |config|
  config.user_agent = 'Slack Ruby Client/3.0'
end
ruby
client = 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.

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

Pagination

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyhacktoberfestslackslack-apislack-bot

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言