I'm trying to use the slack-ruby-client gem. Ive made a Slack app and have all the client id, secret, etc. But i cant work out what i am supposed to do with them. In that gem it has the following: how to send a message.
Slack.configure do |config|
config.token = SLACK_VERIFICATION_TOKEN
raise 'Missing SLACK_VERIFICATION_TOKEN' unless config.token
end
client = Slack::Web::Client.new
client.auth_test
client.chat_postMessage(channel: '#channel_name', text: 'Hello World', as_user: true)
but this complains:
Slack::Web::Api::Errors::InvalidAuth: invalid_auth
which makes sense as i haven't done the auth with the id and secret. But how do i actually do that?
-
Thanks - forgive my ignorance but that seems to be about running an oauth server whereas i just need a client to communicate with slack. I might be really missing the point though. Would you mind explaining?Max Williams– Max Williams2025年11月07日 14:59:01 +00:00Commented Nov 7, 2025 at 14:59
1 Answer 1
It turns out that the Invalid Auth wasn't about the API key/secret at all, and that i didn't need them. What i needed to do was to set up the Slack app properly within my company's Slack workspace. That app has permission to write messages and upload files, and i add it into the channels i want it to be able to post to. Then, it generates a token (starting with "xoxb-") and that's what i'm using for SLACK_VERIFICATION_TOKEN. That was the only constant that i needed.
So the error really was just that i hadn't set the app/bot up properly. I think the first time I did it was the top level app token but i really needed to token for the bot that it contains, or something along those lines, after the app had been added to my workspace.
This is how i send the attachment, after i got the basic messaging working:
#files_upload_v2 uses the ID of the channel, not the name: this can be seen in the url
#if you're on the web version: eg the #yustudio channel is https://app.slack.com/client/T02BU43AG/G01NASDQTVK4
#the channel id is the last part, "G01NASDQTVK4"
channel_id = "G01NASDQTVK4"
string = "Here is the attachment"
options = {:attachment => "/path/to/myfile.json"}
upload_options = {
filename: File.basename(options[:attachment]), # this is used for the file title, unless a :title option is provided
content: File.read(options[:attachment]), # the contents of the file
channels: [channel_id], # channel IDs to share the file to
title: string
}
if options[:comment].not_blank?
upload_options[:initial_comment] = options[:comment]
end
client.files_upload_v2(upload_options)