lita-slack 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 756bfd5bc0c402ad707500b2d06fe4131ae61414
4
+ data.tar.gz: ba2490c9f35bfbe7dda51dffb5d18da5c18b5bba
5
+ SHA512:
6
+ metadata.gz: 99dd9b57499029f4feb8bd8624ed6c6f710c100f0b11cdcbb52ed9752516d6652a7e3b92e3ce3278299c604fb74b6a672bede4ea81b036af18605411dc3b58b6
7
+ data.tar.gz: bc4639ebffb94030a59873e206a7708945a5b4b304a6d2d53be946233f0c3254eb2c1d6937b7a2f777ae88ece3af6b7aab7a0b10492f44b60eebcaf414d77e96
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2013 kenjij
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # lita-slack
2
+
3
+ **lita-slack** is an adapter for [Lita](https://github.com/jimmycuadra/lita) that allows you to use the robot with [Slack](https://slack.com/). As **lita-slack** is one way, robot messages to Slack, it depends on [lita-slack-handler](https://github.com/kenjij/lita-slack-handler) gem to receive messages from Slack.
4
+
5
+ ## Installation
6
+
7
+ Add **lita-slack** and **lita-slack-handler** to your Lita instance's Gemfile:
8
+
9
+ ``` ruby
10
+ gem "lita-slack"
11
+ gem "lita-slack-handler"
12
+ ```
13
+
14
+ ## Configuration
15
+
16
+ **First, you need to make sure your Slack team has [Incoming WebHooks](https://my.slack.com/services/new/incoming-webhook) integration setup. For configuration regarding lita-slack-handler, see its [README](https://github.com/kenjij/lita-slack-handler).**
17
+
18
+ Then, define the following attributes:
19
+
20
+ ### Required attributes
21
+
22
+ * `incoming_token` (String) – Slack integration token.
23
+ * `team_domain` (String) – Slack team domain; subdomain of slack.com.
24
+
25
+ ### Optional attributes
26
+
27
+ * `incoming_url` (String) – Default: https://<team_domain>.slack.com/services/hooks/incoming-webhook
28
+ * `username` (String) – Display name of the robot; default: whatever is set in Slack integration
29
+ * `add_mention` (Bool) – Always prefix message with mention of the user which it's directed to; this triggers a notification.
30
+
31
+ ### Example lita_config.rb
32
+
33
+ ``` ruby
34
+ Lita.configure do |config|
35
+ config.robot.name = "Lita"
36
+ config.robot.mention_name = "@lita"
37
+ # Select the Slack adapter
38
+ config.robot.adapter = :slack
39
+ # lita-slack adapter config
40
+ config.adapter.incoming_token = "aN1NvAlIdDuMmYt0k3n"
41
+ config.adapter.team_domain = "example"
42
+ config.adapter.username = "lita"
43
+ # Some more handlers and other config
44
+ # .....
45
+ end
46
+ ```
47
+
48
+ ## License
49
+
50
+ [MIT](http://opensource.org/licenses/MIT)
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/lib/lita-slack.rb ADDED
@@ -0,0 +1 @@
1
+ require "lita/adapters/slack"
@@ -0,0 +1,86 @@
1
+ require 'lita'
2
+ require 'faraday'
3
+ require 'json'
4
+
5
+ module Lita
6
+ module Adapters
7
+ class Slack < Adapter
8
+ # Required Lita config keys (via lita_config.rb)
9
+ require_configs :incoming_token, :team_domain
10
+
11
+ def initialize(robot)
12
+ log.debug 'Slack::initialize started'
13
+ super
14
+ # Set config default values
15
+ config.incoming_url ||= "https://#{config.team_domain}.slack.com/services/hooks/incoming-webhook"
16
+ config.username ||= nil
17
+ config.add_mention ||= false
18
+ log.debug 'Slack::initialize ending'
19
+ end
20
+
21
+ # Adapter main run loop
22
+ def run
23
+ log.debug 'Slack::run started'
24
+ sleep
25
+ rescue Interrupt
26
+ shut_down
27
+ end
28
+
29
+ def send_messages(target, strings)
30
+ log.debug 'Slack::send_messages started'
31
+ status = http_post prepare_payload(target, strings)
32
+ log.error "Slack::send_messages failed to send (#{status})" if status != 200
33
+ log.debug 'Slack::send_messages ending'
34
+ end
35
+
36
+ def set_topic(target, topic)
37
+ # Slack currently provides no method
38
+ log.info 'Slack::set_topic no implementation'
39
+ end
40
+
41
+ def shut_down
42
+ end
43
+
44
+ def prepare_payload(target, strings)
45
+ if not defined?(target.room)
46
+ channel_id = nil
47
+ log.warn "Slack::prepare_payload proceeding without channel designation"
48
+ else
49
+ channel_id = target.room
50
+ end
51
+ payload = {'channel' => channel_id, 'username' => config.username}
52
+ payload['text'] = strings.join('\n')
53
+ if config.add_mention and defined?(target.user.id)
54
+ payload['text'] = payload['text'].prepend("<@#{target.user.id}> ")
55
+ end
56
+ return payload
57
+ end
58
+
59
+ def http_post(payload)
60
+ res = Faraday.post do |req|
61
+ log.debug "Slack::http_post sending payload to #{config.incoming_url}; length: #{payload.to_json.size}"
62
+ req.url config.incoming_url, :token => config.incoming_token
63
+ req.headers['Content-Type'] = 'application/json'
64
+ req.body = payload.to_json
65
+ end
66
+ log.info "Slack::http_post sent payload with response status #{res.status}"
67
+ log.debug "Slack::http_post response body: #{res.body}"
68
+ return res.status
69
+ end
70
+
71
+ #
72
+ # Accessor shortcuts
73
+ #
74
+ def config
75
+ Lita.config.adapter
76
+ end
77
+
78
+ def log
79
+ Lita.logger
80
+ end
81
+ end
82
+
83
+ # Register Slack adapter to Lita
84
+ Lita.register_adapter(:slack, Slack)
85
+ end
86
+ end
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "lita-slack"
3
+ spec.version = "0.1.0"
4
+ spec.authors = ["Ken J."]
5
+ spec.email = ["kenjij@gmail.com"]
6
+ spec.description = %q{Lita adapter for Slack}
7
+ spec.summary = %q{Lita adapter for Slack using Sinatra. Requires lita-slack-handler gem.}
8
+ spec.homepage = "https://github.com/kenjij/lita-slack"
9
+ spec.license = "MIT"
10
+ spec.metadata = { "lita_plugin_type" => "adapter" }
11
+
12
+ spec.files = `git ls-files`.split($/)
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ["lib"]
16
+
17
+ spec.add_runtime_dependency "lita", "~> 2.7"
18
+ spec.add_runtime_dependency "lita-slack-handler", "~> 0.2"
19
+ spec.add_runtime_dependency "faraday", ">= 0.8.7"
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", ">= 2.14"
24
+ end
@@ -0,0 +1,53 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Adapters::Slack do
4
+ before do
5
+ Lita.configure do |config|
6
+ config.adapter.incoming_token = 'aN1NvAlIdDuMmYt0k3n'
7
+ config.adapter.team_domain = 'example'
8
+ config.adapter.username = 'lita'
9
+ config.adapter.add_mention = true
10
+ end
11
+ end
12
+
13
+ subject { described_class.new(robot) }
14
+ let(:robot) { double("Lita::Robot") }
15
+
16
+ it "registers with Lita" do
17
+ expect(Lita.adapters[:slack]).to eql(described_class)
18
+ end
19
+
20
+ it "fails without valid config: incoming_token and team_domain" do
21
+ Lita.clear_config
22
+ expect(Lita.logger).to receive(:fatal).with(/incoming_token, team_domain/)
23
+ expect { subject }.to raise_error(SystemExit)
24
+ end
25
+
26
+ describe "#send_messages" do
27
+ it "sends JSON payload via HTTP POST to Slack channel" do
28
+ target = double("Lita::Source", room: "CR00M1D")
29
+ payload = {'channel' => target.room, 'username' => Lita.config.adapter.username, 'text' => 'Hello!'}
30
+ expect(subject).to receive(:http_post).with(payload)
31
+ subject.send_messages(target, ["Hello!"])
32
+ end
33
+
34
+ it "sends message with mention if user info is provided" do
35
+ user = double("Lita::User", id: "UM3NT10N")
36
+ target = double("Lita::Source", room: "CR00M1D", user: user)
37
+ text = "<@#{user.id}> Hello!"
38
+ payload = {'channel' => target.room, 'username' => Lita.config.adapter.username, 'text' => text}
39
+ expect(subject).to receive(:http_post).with(payload)
40
+ subject.send_messages(target, ["Hello!"])
41
+ end
42
+
43
+ it "proceeds but logs WARN when directed to an user without channel(room) info" do
44
+ user = double("Lita::User", id: "UM3NT10N")
45
+ target = double("Lita::Source", user: user)
46
+ text = "<@#{user.id}> Hello!"
47
+ payload = {'channel' => nil, 'username' => Lita.config.adapter.username, 'text' => text}
48
+ expect(subject).to receive(:http_post).with(payload)
49
+ expect(Lita.logger).to receive(:warn).with(/without channel/)
50
+ subject.send_messages(target, ["Hello!"])
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,2 @@
1
+ require "lita-slack"
2
+ require "lita/rspec"
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lita-slack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ken J.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lita
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '2.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: lita-slack-handler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.8.7
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.8.7
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '2.14'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '2.14'
97
+ description: Lita adapter for Slack
98
+ email:
99
+ - kenjij@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - Gemfile
105
+ - LICENSE
106
+ - README.md
107
+ - Rakefile
108
+ - lib/lita-slack.rb
109
+ - lib/lita/adapters/slack.rb
110
+ - lita-slack.gemspec
111
+ - spec/lita/adapters/slack_spec.rb
112
+ - spec/spec_helper.rb
113
+ homepage: https://github.com/kenjij/lita-slack
114
+ licenses:
115
+ - MIT
116
+ metadata:
117
+ lita_plugin_type: adapter
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.0.7
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: Lita adapter for Slack using Sinatra. Requires lita-slack-handler gem.
138
+ test_files:
139
+ - spec/lita/adapters/slack_spec.rb
140
+ - spec/spec_helper.rb