Search code examples
ruby-on-railsslack

Slack webhook in Rails


I am opening a Rails console to try and use HTTParty to send a message to a slack channel using web hooks.

It all works when I use Postman but it doesnt work when I try HTTParty. In Postman I simply POST to the url with form-data with a key of payload and a value of:

{"channel" => "#some_channel", "username" => "webhookbot", "text" => "Testing message", "icon_emoji" => ":ghost:"}

In the rails console I am trying:

HTTParty.post(webhook_url, body: {"channel" => "#some_channel", "username" => "webhookbot", "text" => "Testing message", "icon_emoji" => ":ghost:"}, :headers => { 'Content-Type' => 'application/x-www-form-urlencoded'})

I get this error message:

Errno::ECONNREFUSED: Connection refused - connect(2) for "hooks.slack.com" port 443

Is this a proxy issue?


Solution

  • Try this:

    HTTParty.post(webhook_url,
      body: {
        "channel" => "#some_channel",
        "username" => "webhookbot",
        "text" => "Testing message",
        "icon_emoji" => ":ghost:"
      }.to_json,
      headers: { 
        'Content-Type' => 'application/json'
      }
    )