mos-eisley 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +21 -0
- data/README.md +8 -1
- data/bin/mos-eisley +1 -1
- data/lib/http-client/client.rb +9 -14
- data/lib/http-client/webapi.rb +31 -28
- data/lib/http-server/helper.rb +0 -4
- data/lib/http-server/server.rb +3 -3
- data/lib/mos-eisley/logger.rb +3 -10
- data/lib/mos-eisley/version.rb +1 -1
- data/lib/mos-eisley/version.rb.orig +9 -0
- data/lib/mos-eisley.rb +1 -0
- data/lib/s3po/s3po.rb +1 -1
- data/mos-eisley.gemspec +2 -2
- metadata +11 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7bcabaa68da8166ee15705358d036be3e4f4af58
|
4
|
+
data.tar.gz: d1030692b827fecccd2bd145031caa9780b4e4a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 00107717c275700bdcbe957c15d1e6777bb526510426083e18d41a46e5351e0d2eed67f2684d483738fa3f1641e26e6da9741543787eb5209921b524a268f2e8
|
7
|
+
data.tar.gz: def5281fe3b818226313eda18bbe308ed09c3debbdcdcb1af3d027231f4353a74c540b7c645a5c4b11c7f6af2a5576973b603a6e2743be9651b6a6964444a441
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 Ken J.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -58,6 +58,8 @@ end
|
|
58
58
|
|
59
59
|
Define handlers, also a Ruby file, and they'll be executed as incoming Slack events are processed. You can define as many handlers as you want. You'll store the file(s) in the directory you've identified in the configuration file above.
|
60
60
|
|
61
|
+
There are 3 types of handlers you can define: `:action`, `:command`, `:event`, which corresponds to the MosEisley endpoints accordingly.
|
62
|
+
|
61
63
|
```ruby
|
62
64
|
ME::Handler.add(:event, 'debug') do |e, h|
|
63
65
|
e.event.each { |k, v| puts "#{k}: #{v}" }
|
@@ -68,9 +70,14 @@ end
|
|
68
70
|
|
69
71
|
### Slack
|
70
72
|
|
73
|
+
Create an app in Slack to setup a bot. Following features can be setup.
|
71
74
|
|
75
|
+
- **Interactive Components** – Request URL should be set to MosEisley's `/action` endpoint.
|
76
|
+
- **Slash Commands** – Request URL should be set to MosEisley's `/command` endpoint.
|
77
|
+
- **OAuth & Permission** – This is where you get the Bot User OAuth Access Token you need to set in the configuration file.
|
78
|
+
- **Event Subscription** – Request URL should be set to MosEisley's `/event` endpoint. You'll likely Subscribe to Bot Events of `app_mention` or any of the `message.*` events.
|
72
79
|
|
73
|
-
## Usage
|
80
|
+
## CLI Usage
|
74
81
|
|
75
82
|
To see help:
|
76
83
|
|
data/bin/mos-eisley
CHANGED
@@ -11,7 +11,7 @@ Kajiki.run(opts) do |cmd|
|
|
11
11
|
MosEisley::Config.load_config(opts[:config]) if opts[:config]
|
12
12
|
MosEisley::Handler.autoload
|
13
13
|
require 'http-server/server'
|
14
|
-
MosEisley.logger.warn(
|
14
|
+
MosEisley.logger.warn("Mos Eisley server starting... (v#{MosEisley::Version})")
|
15
15
|
MosEisley::Server.run!({bind: opts[:address], port: opts[:port]})
|
16
16
|
end
|
17
17
|
end
|
data/lib/http-client/client.rb
CHANGED
@@ -1,57 +1,52 @@
|
|
1
1
|
require 'eventmachine'
|
2
2
|
require 'em-http'
|
3
3
|
|
4
|
-
|
5
4
|
module MosEisley
|
6
|
-
|
7
5
|
class HTTPClient
|
8
|
-
|
9
6
|
def self.post_form(url:, params: nil, head: nil, &block)
|
10
7
|
if EM.reactor_running?
|
11
8
|
MosEisley.logger.debug("POSTing form to #{url}")
|
12
9
|
MosEisley::HTTPClient.request(url: url, head: head, body: params, &block)
|
13
10
|
else
|
14
11
|
MosEisley.logger.debug('Starting reactor...')
|
15
|
-
EM.run
|
12
|
+
EM.run do
|
16
13
|
MosEisley.logger.debug("POSTing form to #{url}")
|
17
14
|
MosEisley::HTTPClient.request(url: url, head: head, body: params, stop: true, &block)
|
18
|
-
|
15
|
+
end
|
19
16
|
end
|
20
17
|
end
|
21
18
|
|
22
19
|
def self.post_json(url:, params: nil, body: nil, head: {}, &block)
|
23
|
-
head
|
20
|
+
head['Content-Type'] = 'application/json; charset=utf-8'
|
24
21
|
body = S3PO.json_with_object(params) if params
|
25
22
|
if EM.reactor_running?
|
26
23
|
MosEisley.logger.debug("POSTing JSON to: #{url}")
|
27
24
|
MosEisley::HTTPClient.request(url: url, head: head, body: body, &block)
|
28
25
|
else
|
29
26
|
MosEisley.logger.debug('Starting reactor...')
|
30
|
-
EM.run
|
27
|
+
EM.run do
|
31
28
|
MosEisley.logger.debug("POSTing JSON to #{url}")
|
32
29
|
MosEisley::HTTPClient.request(url: url, head: head, body: body, stop: true, &block)
|
33
|
-
|
30
|
+
end
|
34
31
|
end
|
35
32
|
end
|
36
33
|
|
37
34
|
def self.request(url:, head: nil, body:, stop: false, &block)
|
38
35
|
http = EM::HttpRequest.new(url).post(body: body, head: head)
|
39
|
-
http.errback
|
36
|
+
http.errback do
|
40
37
|
MosEisley.logger.error('HTTP error')
|
41
38
|
if stop
|
42
39
|
EM.stop
|
43
40
|
MosEisley.logger.debug('Stopped reactor.')
|
44
41
|
end
|
45
|
-
|
46
|
-
http.callback
|
42
|
+
end
|
43
|
+
http.callback do
|
47
44
|
block.call(http) if block_given?
|
48
45
|
if stop
|
49
46
|
EM.stop
|
50
47
|
MosEisley.logger.debug('Stopped reactor.')
|
51
48
|
end
|
52
|
-
|
49
|
+
end
|
53
50
|
end
|
54
|
-
|
55
51
|
end
|
56
|
-
|
57
52
|
end
|
data/lib/http-client/webapi.rb
CHANGED
@@ -1,45 +1,48 @@
|
|
1
1
|
module MosEisley
|
2
|
-
|
3
2
|
class WebAPI
|
4
|
-
|
5
|
-
BaseURL = 'https://slack.com/api/'
|
3
|
+
BASE_URL = 'https://slack.com/api/'.freeze
|
6
4
|
|
7
5
|
def self.auth_test
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
HTTPClient.post_form(url: url, head: head) do |h|
|
12
|
-
MosEisley.config.meta.merge!(S3PO.parse_json(h.response))
|
13
|
-
MosEisley.logger.info('Meta data updated by auth.test call.')
|
14
|
-
MosEisley.logger.debug("Meta data:\n#{MosEisley.config.meta}")
|
6
|
+
post_to_slack('auth.test', nil, true) do |r|
|
7
|
+
MosEisley.config.meta.merge!(r)
|
8
|
+
MosEisley.logger.info('Saved meta data.')
|
15
9
|
end
|
16
10
|
end
|
17
11
|
|
18
12
|
def self.post_message(msg)
|
19
|
-
|
20
|
-
url = BaseURL + m
|
21
|
-
head = {authorization: "Bearer #{MosEisley.config.bot_access_token}"}
|
22
|
-
HTTPClient.post_json(url: url, params: msg, head: head) do |h|
|
23
|
-
MosEisley.logger.info('POSTed chat.postMessage')
|
24
|
-
MosEisley.logger.debug("chat.postMessage echo:\n#{h.response}")
|
25
|
-
end
|
13
|
+
post_to_slack('chat.postMessage', msg)
|
26
14
|
end
|
27
15
|
|
28
16
|
def self.post_ephemeral(msg)
|
29
|
-
|
30
|
-
|
17
|
+
post_to_slack('chat.postEphemeral', msg)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def self.post_to_slack(method, data = nil, abort_on_err = false, &block)
|
23
|
+
url = BASE_URL + method
|
31
24
|
head = {authorization: "Bearer #{MosEisley.config.bot_access_token}"}
|
32
|
-
HTTPClient.post_json(url: url, params:
|
33
|
-
MosEisley.logger.info(
|
34
|
-
|
25
|
+
HTTPClient.post_json(url: url, params: data, head: head) do |h|
|
26
|
+
MosEisley.logger.info("POSTed #{method}.")
|
27
|
+
r = check_response(h.response)
|
28
|
+
abort('Aborting due to Slack error.') if r.nil? && abort_on_err
|
29
|
+
yield(r) if block_given?
|
35
30
|
end
|
36
31
|
end
|
37
32
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
33
|
+
def self.check_response(json)
|
34
|
+
r = S3PO.parse_json(json)
|
35
|
+
if r
|
36
|
+
if r[:ok]
|
37
|
+
MosEisley.logger.debug("Response from Slack:\n#{r}")
|
38
|
+
return r
|
39
|
+
else
|
40
|
+
MosEisley.logger.error("Slack error:\n#{r}")
|
41
|
+
end
|
42
|
+
else
|
43
|
+
MosEisley.logger.error('Could not parse response; not JSON?')
|
44
|
+
end
|
45
|
+
return nil
|
46
|
+
end
|
43
47
|
end
|
44
|
-
|
45
48
|
end
|
data/lib/http-server/helper.rb
CHANGED
@@ -1,9 +1,7 @@
|
|
1
1
|
require 's3po/s3po'
|
2
2
|
|
3
3
|
module MosEisley
|
4
|
-
|
5
4
|
module Helper
|
6
|
-
|
7
5
|
def logger
|
8
6
|
MosEisley.logger
|
9
7
|
end
|
@@ -30,7 +28,5 @@ module MosEisley
|
|
30
28
|
def json_with_object(obj, pretty: true, opts: nil)
|
31
29
|
MosEisley::S3PO.json_with_object(obj, pretty: pretty, opts: opts)
|
32
30
|
end
|
33
|
-
|
34
31
|
end
|
35
|
-
|
36
32
|
end
|
data/lib/http-server/server.rb
CHANGED
@@ -30,7 +30,7 @@ module MosEisley
|
|
30
30
|
halt 400 if event.nil?
|
31
31
|
logger.debug("Parsed JSON data:\n#{event}")
|
32
32
|
unless valid_token?(event[:token])
|
33
|
-
logger.
|
33
|
+
logger.warn("Invalid Slack Events token: #{event[:token]}")
|
34
34
|
halt 401
|
35
35
|
end
|
36
36
|
res = Handler.run(:action, S3PO.create_event(event, :action))
|
@@ -49,7 +49,7 @@ module MosEisley
|
|
49
49
|
logger.info('Incoming request received at /command.')
|
50
50
|
cmd = parse_command(params)
|
51
51
|
unless valid_token?(cmd[:token])
|
52
|
-
logger.
|
52
|
+
logger.warn("Invalid Slack Events token: #{cmd[:token]}")
|
53
53
|
halt 401
|
54
54
|
end
|
55
55
|
res = Handler.run(:command, cmd)
|
@@ -71,7 +71,7 @@ module MosEisley
|
|
71
71
|
logger.debug("Parsed JSON data:\n#{event}")
|
72
72
|
|
73
73
|
unless valid_token?(event[:token])
|
74
|
-
logger.
|
74
|
+
logger.warn("Invalid Slack Events token: #{event[:token]}")
|
75
75
|
halt 401
|
76
76
|
end
|
77
77
|
resp = {}
|
data/lib/mos-eisley/logger.rb
CHANGED
@@ -1,24 +1,17 @@
|
|
1
1
|
require 'logger'
|
2
2
|
|
3
|
-
|
4
3
|
module MosEisley
|
5
|
-
|
6
4
|
def self.logger=(logger)
|
7
5
|
@logger = logger
|
8
6
|
end
|
9
7
|
|
10
8
|
def self.logger
|
11
|
-
@logger ||= NullLogger.new
|
9
|
+
@logger ||= NullLogger.new
|
12
10
|
end
|
13
11
|
|
14
12
|
class NullLogger < Logger
|
13
|
+
def initialize(*args); end
|
15
14
|
|
16
|
-
def
|
17
|
-
end
|
18
|
-
|
19
|
-
def add(*args, &block)
|
20
|
-
end
|
21
|
-
|
15
|
+
def add(*args, &block); end
|
22
16
|
end
|
23
|
-
|
24
17
|
end
|
data/lib/mos-eisley/version.rb
CHANGED
data/lib/mos-eisley.rb
CHANGED
data/lib/s3po/s3po.rb
CHANGED
data/mos-eisley.gemspec
CHANGED
@@ -17,8 +17,8 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.require_paths = ['lib']
|
18
18
|
|
19
19
|
s.required_ruby_version = '>= 2.1'
|
20
|
+
s.add_runtime_dependency 'em-http-request', '~> 1.1'
|
20
21
|
s.add_runtime_dependency 'kajiki', '~> 1.1'
|
21
|
-
s.add_runtime_dependency 'thin', '~> 1.7'
|
22
22
|
s.add_runtime_dependency 'sinatra', '~> 2.0'
|
23
|
-
s.add_runtime_dependency '
|
23
|
+
s.add_runtime_dependency 'thin', '~> 1.7'
|
24
24
|
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mos-eisley
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ken J.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: em-http-request
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
@@ -25,19 +25,19 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: kajiki
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '1.
|
33
|
+
version: '1.1'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '1.
|
40
|
+
version: '1.1'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: sinatra
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -53,19 +53,19 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '2.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: thin
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '1.
|
61
|
+
version: '1.7'
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '1.
|
68
|
+
version: '1.7'
|
69
69
|
description: A Ruby based HTTP server for Slack app actions, commands, events, and
|
70
70
|
making web API calls.
|
71
71
|
email:
|
@@ -75,6 +75,7 @@ executables:
|
|
75
75
|
extensions: []
|
76
76
|
extra_rdoc_files: []
|
77
77
|
files:
|
78
|
+
- LICENSE
|
78
79
|
- README.md
|
79
80
|
- bin/mos-eisley
|
80
81
|
- lib/http-client/client.rb
|
@@ -86,6 +87,7 @@ files:
|
|
86
87
|
- lib/mos-eisley/handler.rb
|
87
88
|
- lib/mos-eisley/logger.rb
|
88
89
|
- lib/mos-eisley/version.rb
|
90
|
+
- lib/mos-eisley/version.rb.orig
|
89
91
|
- lib/s3po/action.rb
|
90
92
|
- lib/s3po/generic.rb
|
91
93
|
- lib/s3po/message.rb
|