user2019182
user2019182

Reputation: 315

Post Request To Slack Endpoint Failing

I have making a post request in Ruby to a slack endpoint and its failing, here is my request, not sure what I'm missing:

#!/usr/bin/env ruby
#Notification Script Test

def send_slack_message
    slack_rooms = [ '#test_channel_notify' ]
    slack_token_file = (File.join(ENV['HOME'], '.slack_api_token'))
    slack_api_token = (File.open(@slack_token_file).readlines)[0].chomp
    msg = 'This is a test message send'
    slack_url = "https://slack.com/api/chat.postMessage"
    %x{curl -k -X POST -d"token=#{slack_api_token}\&channel=#{slack_rooms}\&text=#{msg}" '#{slack_url}'} 
end

send_slack_message

I am getting the following error, not sure what I'm missing:

./cap2.rb:7:in `initialize': no implicit conversion of nil into String (TypeError)
from ./cap2.rb:7:in `open'
from ./cap2.rb:7:in `send_slack_message'
from ./cap2.rb:13:in `<main>'

I am a ruby novice so I may be missing everything would love some help!

Upvotes: 0

Views: 351

Answers (3)

msanford
msanford

Reputation: 12247

While your original problem has been solved, I will provide an alternative solution which follows the more common practice of storing secret credentials as environment variables, not as files (which can, for example, more easily exfiltrated by accident, committed to source control, pasted to a presentation, etc).

Set SLACK_TOKEN in your environment, and then use:

#!/usr/bin/env ruby
#Notification Script Test

def send_slack_message
    slack_rooms = [ '#test_channel_notify' ]
    slack_api_token = ENV['SLACK_TOKEN']
    msg = 'This is a test message send'
    slack_url = "https://slack.com/api/chat.postMessage"
    %x{curl -k -X POST -d"token=#{slack_api_token}\&channel=#{slack_rooms}\&text=#{msg}" '#{slack_url}'} 
end

send_slack_message

Upvotes: 0

Shiyason
Shiyason

Reputation: 781

The slack API wants to receive the payload in this format: 'payload={"json": "data"}' Using Net::HTTP you can make a POST request like this:

require 'net/http'
require 'uri'

def payload(message, channel)
  { channel: channel, username: 'your-username', text: message, icon_emoji: ':robot_face:' }
end

msg = 'This is a test message send'
body = payload(msg, '#test_channel_notify').to_json

url = URI("https://slack.com/api/chat.postMessage")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(url)
request.body = "payload=#{body}"
http.request(request)

Upvotes: 1

yoones
yoones

Reputation: 2464

The error says that you can't give nil to File.open. Make sure @slack_token_file exists and is not nil.

Upvotes: 2

Related Questions