slack_cli 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ad43e690e15ffd38a98ac42584ecd59381891be4
4
- data.tar.gz: 2f9aae7204c3e1846474774b3502c8b84f78afcf
3
+ metadata.gz: 0aadf0d88328d026de5bb4e52e0c48f47c203473
4
+ data.tar.gz: 20b4357c0850cc350098f6a78919c8be68a7053e
5
5
  SHA512:
6
- metadata.gz: 15983cc24d35d068dcd1545cab55563747007626bb148a896928c30eb2a72d7667b89d69705192e0b4fbac3129b9bdc61df90fe7b8faf625f19e7c85752a1d43
7
- data.tar.gz: ab5bce079b5faebe93edf000f28a2b6af4569ad73da0711c02f01e730de2f3fd3fcbc2a9facb56c9826bfab1860a453d592a8b6f1b6c9c8a59f0d1b87c52613f
6
+ metadata.gz: 33abb67c37822e408229677ef62c91e47116a23e8ffe8d2142632ef6c47eca5187e7d3c76f06e02545af266ce2e84876905bdfdc11c9fcadc700a97243a6016a
7
+ data.tar.gz: 61b3091f6abae5650c519924fc16e89e7923ad2103e0d3c2b9f3da491fe792101b71a4ff92e90b8a5f75bfa81245110538091c9950471e290466ddfca5b93e44
@@ -1,11 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- slack_cli (0.3.2)
4
+ slack_cli (0.4.0)
5
5
  dotenv (~> 2.2)
6
+ eventmachine (~> 1.2)
6
7
  puma (~> 3.11)
7
8
  rack (~> 2.0)
8
9
  thor (~> 0.20)
10
+ websocket-eventmachine-client (~> 1.2)
9
11
 
10
12
  GEM
11
13
  remote: https://rubygems.org/
@@ -13,6 +15,7 @@ GEM
13
15
  coderay (1.1.2)
14
16
  diff-lcs (1.3)
15
17
  dotenv (2.2.1)
18
+ eventmachine (1.2.5)
16
19
  method_source (0.9.0)
17
20
  pry (0.11.3)
18
21
  coderay (~> 1.1.0)
@@ -34,6 +37,14 @@ GEM
34
37
  rspec-support (~> 3.7.0)
35
38
  rspec-support (3.7.1)
36
39
  thor (0.20.0)
40
+ websocket (1.2.5)
41
+ websocket-eventmachine-base (1.2.0)
42
+ eventmachine (~> 1.0)
43
+ websocket (~> 1.0)
44
+ websocket-native (~> 1.0)
45
+ websocket-eventmachine-client (1.2.0)
46
+ websocket-eventmachine-base (~> 1.0)
47
+ websocket-native (1.0.0)
37
48
 
38
49
  PLATFORMS
39
50
  ruby
data/exe/slack CHANGED
@@ -23,13 +23,29 @@ class CLI < Thor
23
23
  method_option :channel, required: true, aliases: '-c', desc: 'Channel to post the message'
24
24
  def post(*text)
25
25
  handle_class_options
26
- SlackCLI.clear_cache unless options[:cache]
27
26
  SlackCLI.post(channel: options[:channel], text: text.join(' '))
28
27
  end
29
28
 
29
+ desc 'rtm', 'Real Time Message Connect'
30
+ method_option :type, default: ['message'], type: 'array', desc: 'List of message types to retrieve'
31
+ def rtm
32
+ handle_class_options
33
+ types = Set.new(options[:type])
34
+
35
+ $stderr.puts({types: types.to_a}.to_json)
36
+ EM.run {
37
+ SlackCLI.rtm_connect do |msg|
38
+ if types.include?(msg['type'])
39
+ puts msg.to_json
40
+ end
41
+ end
42
+ }
43
+ end
44
+
30
45
  private
31
46
 
32
47
  def handle_class_options
48
+ SlackCLI.clear_cache unless options[:cache]
33
49
  $DEBUG = options[:debug]
34
50
  end
35
51
  end
@@ -4,6 +4,8 @@ require 'dotenv/load' # must come first
4
4
  require 'cgi'
5
5
  require 'digest/md5'
6
6
  require 'erb'
7
+ require 'eventmachine'
8
+ require 'websocket/eventmachine/client'
7
9
  require 'fileutils'
8
10
  require 'json'
9
11
  require 'logger'
@@ -48,7 +50,7 @@ module SlackCLI
48
50
  end
49
51
 
50
52
  def authorize
51
- puts "Go to \e[1;32mhttp://localhost:65187/\e[0m to authenticate Slack CLI."
53
+ $stderr.puts "Go to \e[1;32mhttp://localhost:65187/\e[0m to authenticate Slack CLI."
52
54
  t = Thread.new do
53
55
  Rack::Handler::WEBrick.run(cli_app, webrick_options) do |server|
54
56
  cli_app.webbrick = server
@@ -56,10 +58,10 @@ module SlackCLI
56
58
  end
57
59
  t.join
58
60
  if File.file?(cli_app.authorization_path)
59
- puts "\e[32mSuccessfully authenticated.\e[0m"
61
+ $stderr.puts "\e[32mSuccessfully authenticated.\e[0m"
60
62
  else
61
- puts 'There was a problem authorizing the token!'
62
- puts log_string_io.read
63
+ $stderr.puts 'There was a problem authorizing the token!'
64
+ $stderr.puts log_string_io.read
63
65
  end
64
66
  end
65
67
 
@@ -87,7 +89,7 @@ module SlackCLI
87
89
 
88
90
  def slack_get(path:, refresh: true)
89
91
  url = "https://slack.com/api/#{path}"
90
- cache_key = Digest::MD5.base64digest(url).gsub('=', '')
92
+ cache_key = Digest::MD5.hexdigest(url).gsub('=', '')
91
93
  full_cache_path = File.join(cache_path, cache_key)
92
94
  save = false
93
95
  data = if refresh == true || !File.file?(full_cache_path)
@@ -103,21 +105,18 @@ module SlackCLI
103
105
 
104
106
  json = JSON.parse(data)
105
107
  if (error = json['error'])
106
- raise Error, "[#{error['code']}] #{error['text']}"
108
+ raise Error, json.inspect
107
109
  end
108
110
  File.open(full_cache_path, 'w'){|f| f << data} if save
109
111
  json
110
112
  end
111
113
 
112
- # http.use_ssl = true
113
- # http.verify_mode = OpenSSL::SSL::VERIFY_PEER
114
- # http.set_debug_output($stdout) if $DEBUG
115
114
  def slack_post(path:, json:)
116
115
  uri = URI.parse("https://slack.com/api/#{path}")
117
116
  http = Net::HTTP.new(uri.host, uri.port)
118
117
  http.use_ssl = true
119
118
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
120
- http.set_debug_output($stdout) if $DEBUG
119
+ http.set_debug_output($stderr) if $DEBUG
121
120
  header = {
122
121
  'Authorization' => "Bearer #{access_token}",
123
122
  'Content-Type' => 'application/json'
@@ -153,10 +152,33 @@ module SlackCLI
153
152
  if place
154
153
  slack_post(
155
154
  path: 'chat.postMessage',
156
- json: { channel: place['id'], text: text }
155
+ json: { channel: place['id'], text: text, as_user: true}
157
156
  )
158
157
  else
159
158
  raise "No channel or user found for `#{channel}`"
160
159
  end
161
160
  end
161
+
162
+ def rtm_connect(**params, &block)
163
+ resp = slack_get(path: 'rtm.connect', refresh: true)
164
+ $stderr.puts resp.to_json
165
+ ws = WebSocket::EventMachine::Client.connect(uri: resp['url'])
166
+ ws.onopen do
167
+ $stderr.puts({status: 'connected'}.to_json)
168
+ # ws.send(connect_payload.to_json)
169
+ end
170
+
171
+ ws.onmessage do |msg, type|
172
+ if block
173
+ block.call(JSON.parse(msg))
174
+ else
175
+ puts msg
176
+ end
177
+ end
178
+
179
+ ws.onclose do |code, reason|
180
+ $stderr.puts({status: 'disconnected', code: code, reason: reason}.to_json)
181
+ rtm_connect
182
+ end
183
+ end
162
184
  end
@@ -80,9 +80,5 @@ module SlackCLI
80
80
  def client_secret
81
81
  ENV['SLACK_SECRET'] || raise('SLACK_SECRET not found in ENV!')
82
82
  end
83
-
84
- def scope
85
- ENV['SLACK_SCOPE'] || raise('SLACK_SCOPE not found in ENV!')
86
- end
87
83
  end
88
84
  end
@@ -1,3 +1,3 @@
1
1
  module SlackCLI
2
- VERSION = '0.3.2'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -28,4 +28,6 @@ Gem::Specification.new do |spec|
28
28
  spec.add_dependency 'puma', '~> 3.11'
29
29
  spec.add_dependency 'rack', '~> 2.0'
30
30
  spec.add_dependency 'thor', '~> 0.20'
31
+ spec.add_dependency 'eventmachine', '~> 1.2'
32
+ spec.add_dependency 'websocket-eventmachine-client', '~> 1.2'
31
33
  end
@@ -42,7 +42,7 @@
42
42
  <div class="center">
43
43
  <h1>Slack CLI</h1>
44
44
  <img class="center" style="margin-top: -1em; width: 5em; height: 5em;" src="https://slack-files2.s3-us-west-2.amazonaws.com/avatars/2018-02-28/322540051491_005c180801b07413d867_512.png"></img>
45
- <a href="https://slack.com/oauth/authorize?client_id=322607959140.322080377537&scope=bot,chat:write:user,channels:read,users:read"><img alt="Add to Slack" height="40" width="139" src="https://platform.slack-edge.com/img/add_to_slack.png" srcset="https://platform.slack-edge.com/img/add_to_slack.png 1x, https://platform.slack-edge.com/img/add_to_slack@2x.png 2x" /></a>
45
+ <a href="https://slack.com/oauth/authorize?client_id=322607959140.322080377537&scope=client"><img alt="Add to Slack" height="40" width="139" src="https://platform.slack-edge.com/img/add_to_slack.png" srcset="https://platform.slack-edge.com/img/add_to_slack.png 1x, https://platform.slack-edge.com/img/add_to_slack@2x.png 2x" /></a>
46
46
  </div>
47
47
  </body>
48
48
  </html>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Pierce
@@ -108,6 +108,34 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0.20'
111
+ - !ruby/object:Gem::Dependency
112
+ name: eventmachine
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.2'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.2'
125
+ - !ruby/object:Gem::Dependency
126
+ name: websocket-eventmachine-client
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.2'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.2'
111
139
  description:
112
140
  email:
113
141
  - ddrscott@gmail.com