Permalink
Browse files

switch to new webhook_url format from slack for notifier initialization

  • Loading branch information...
stevenosloan committed Oct 22, 2014
1 parent a020507 commit 04006c8d2aeba4bf9086ff2524bae88919a4d424
Showing with 49 additions and 76 deletions.
  1. +1 −2 .env-example
  2. +2 −0 changelog.md
  3. +26 −0 docs/upgrate-from-0.6.1.md
  4. +5 −18 lib/slack-notifier.rb
  5. +4 −15 readme.md
  6. +1 −1 spec/integration/ping_integration_test.rb
  7. +10 −40 spec/lib/slack-notifier_spec.rb
@@ -1,2 +1 @@
SLACK_TEAM: team_name
SLACK_TOKEN: token
SLACK_WEBHOOK_URL: "https://example.com"
@@ -1,3 +1,5 @@
- [BREAKING!] To follow changes with slack, client is now initialized with a webhook url instead of team & token. For help upgrading read the [upgrade from 0.6.1 guide](docs/upgrade-from-0.6.1.md)

# 0.6.1
- fix bug in link_formatter to allow multiple links in a message

@@ -0,0 +1,26 @@
Recently slack changed the way incoming webhooks are handled. Instead of taking a team name and token, they now provide a unique (obfuscated) webhook url.

To upgrade the slack-notifier gem, you'll need to find your webhook url. In slack:
- go to you're configured integrations (https://team-name.slack.com/services)
- select **Incoming Webhooks**
- select the webhook that uses the slack-notifier gem
- find the webhook url under the heading **Integration Settings**

You'll then change the way you initialize your notifier

From:
```ruby
notifier = Slack::Notifier.new 'team', 'token'
```

To:
```ruby
notifier = Slack::Notifier.new 'WEBHOOK_URL'
```

Defaults & attachemnts will continue to work like they have

```ruby
notifier = Slack::Notifier.new 'WEBHOOK_URL', icon_emoji: ":ghost:"
notifier.ping "I'm feeling spooky"
```
@@ -7,14 +7,11 @@

module Slack
class Notifier
attr_reader :team, :token, :http_client,
:hook_name, :default_payload

def initialize team, token, options={} # hook_name=default_hook_name, default_payload={}
@team = team
@token = token
@http_client = options.delete(:http_client) || DefaultHTTPClient
@hook_name = options.delete(:hook_name) || default_hook_name
attr_reader :endpoint, :http_client, :default_payload

def initialize webhook_url, options={}
@endpoint = URI.parse webhook_url
@http_client = options.delete(:http_client) || DefaultHTTPClient
@default_payload = options
end

@@ -42,15 +39,5 @@ def username= username
default_payload[:username] = username
end

private

def default_hook_name
'incoming-webhook'
end

def endpoint
URI.parse "https://#{team}.slack.com/services/hooks/#{hook_name}?token=#{token}"
end

end
end
@@ -8,7 +8,7 @@ A simple wrapper to send notifications to [Slack](https://slack.com/) webhooks.
```ruby
require 'slack-notifier'
notifier = Slack::Notifier.new "yourteam", "yourtokenXX"
notifier = Slack::Notifier.new "WEBHOOK_URL"
notifier.ping "Hello World"
# => if your webhook is setup, will message "Hello World"
# => to the default channel you set in slack
@@ -20,8 +20,8 @@ notifier.ping "Hello World"
On initialization you can set default payloads by passing an options hash.

```ruby
notifier = Slack::Notifier.new "yourteam", "yourtokenXX",
channel: '#default', username: 'notifier'
notifier = Slack::Notifier.new "WEBHOOK_URL", channel: '#default',
username: 'notifier'
notifier.ping "Hello default"
# => will message "Hello default"
@@ -47,17 +47,6 @@ notifier.ping "Hello random", channel: "#random"
```


### Custom hook name

When Slack integrates an app with their website, they replace `incoming-webhook` with the service name.
This allows you to use this library to integrate a non-DIY service.

```ruby
notifier = Slack::Notifier.new "yourteam", "yourtokenXX", hook_name: "custom_hook_name"
# => Messages will be posted to https://yourteam.slack.com/services/hooks/custom_hook_name
```


## Links

Slack requires links to be formatted a certain way, so slack-notifier will look through your message and attempt to convert any html or markdown links to slack's format before posting.
@@ -107,7 +96,7 @@ module Client
end
end
notifier = Slack::Notifier.new 'yourteam', 'yourtoken', http_client: Client
notifier = Slack::Notifier.new 'WEBHOOK_URL', http_client: Client
```


@@ -1,5 +1,5 @@
require_relative '../../lib/slack-notifier'

notifier = Slack::Notifier.new ENV['SLACK_TEAM'], ENV['SLACK_TOKEN'], username: 'notifier'
notifier = Slack::Notifier.new ENV['SLACK_WEBHOOK_URL'], username: 'notifier'
puts "testing with ruby #{RUBY_VERSION}"
notifier.ping "hello from notifier test script on ruby: #{RUBY_VERSION}"
@@ -1,38 +1,23 @@
require 'spec_helper'

describe Slack::Notifier do
subject { described_class.new 'team', 'token' }
subject { described_class.new 'http://example.com' }

describe "#initialize" do
it "sets the given team" do
expect( subject.team ).to eq 'team'
end

it "sets the given token" do
expect( subject.token ).to eq 'token'
end

it "sets the optional service hook name" do
subject = described_class.new 'team', 'token', hook_name: 'custom_hook_name'
expect( subject.hook_name ).to eq 'custom_hook_name'
it "sets the given hook_url to the endpoint URI" do
expect( subject.endpoint ).to eq URI.parse 'http://example.com'
end

it "sets the default_payload options" do
subject = described_class.new 'team', 'token', channel: 'foo'
subject = described_class.new 'http://example.com', channel: 'foo'
expect( subject.channel ).to eq 'foo'
end

it "sets a custom http client" do
client = double("CustomClient")
subject = described_class.new 'team', 'token', http_client: client
subject = described_class.new 'http://example.com', http_client: client
expect( subject.http_client ).to eq client
end

it "can set service hook & default_payload options" do
subject = described_class.new 'team', 'token', hook_name: 'hook_name', channel: 'foo'
expect( subject.channel ).to eq 'foo'
expect( subject.hook_name ).to eq 'hook_name'
end
end

describe "#ping" do
@@ -44,7 +29,7 @@
expect( Slack::Notifier::LinkFormatter ).to receive(:format)
.with("the message")

described_class.new('team','token').ping "the message", channel: 'foo'
described_class.new('http://example.com').ping "the message", channel: 'foo'
end

context "with a default channel set" do
@@ -84,44 +69,29 @@
it "posts with the correct endpoint & data" do
@endpoint_double = instance_double "URI::HTTP"
allow( URI ).to receive(:parse)
.with("https://team.slack.com/services/hooks/incoming-webhook?token=token")
.with("http://example.com")
.and_return(@endpoint_double)

expect( Slack::Notifier::DefaultHTTPClient ).to receive(:post)
.with @endpoint_double,
payload: '{"text":"the message","channel":"channel"}'

described_class.new("team","token").ping "the message", channel: "channel"
end
end

context "with custom webhook name" do
it "posts with the correct endpoint & data" do
@endpoint_double = instance_double "URI::HTTP"
allow( URI ).to receive(:parse)
.with("https://team.slack.com/services/hooks/custom_hook_name?token=token")
.and_return(@endpoint_double)

expect( Slack::Notifier::DefaultHTTPClient ).to receive(:post)
.with @endpoint_double,
payload: '{"text":"the message","channel":"channel"}'

described_class.new("team","token", hook_name: "custom_hook_name").ping "the message", channel: "channel"
described_class.new("http://example.com").ping "the message", channel: "channel"
end
end

context "with a custom http_client set" do
it "uses it" do
endpoint_double = instance_double "URI::HTTP"
allow( URI ).to receive(:parse)
.with("https://team.slack.com/services/hooks/incoming-webhook?token=token")
.with("http://example.com")
.and_return(endpoint_double)
client = double("CustomClient")
expect( client ).to receive(:post)
.with endpoint_double,
payload: '{"text":"the message"}'

described_class.new('team','token',http_client: client).ping "the message"
described_class.new('http://example.com',http_client: client).ping "the message"
end
end
end

0 comments on commit 04006c8

Please sign in to comment.