The Slack::Web::Client allows you to interact with the Slack Web API. You can initialize it with a specific token to override the global configuration, or use the global configuration if no token is provided.
Common Tasks
Test Authentication
Verify your token is working:
client = Slack::Web::Client.new
client.auth_test
Send Messages
Use chat_postMessage to send text to a channel:
client.chat_postMessage(channel: '#general', text: 'Hello World', as_user: true)
List Channels
Retrieve a list of channels using conversations_list:
channels = client.conversations_list.channels
general_channel = channels.detect { |c| c.name == 'general' }
Upload Files
Use the files_upload_v2 helper to handle the multi-step upload process required by the Slack API. This method supports uploading a single file or an array of files.
Single file upload:
client.files_upload_v2(
filename: 'results.pdf',
content: File.read('/users/me/results.pdf'),
channels: ['C000000001'],
initial_comment: 'Sharing results'
)
Multiple files upload:
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!'
)
Note: Avoid using the files_upload method as it uses a deprecated endpoint that will be unsupported after 2025-03-11.