slack_socket_mode 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +28 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +68 -0
- data/LICENSE.txt +21 -0
- data/README.md +50 -0
- data/Rakefile +12 -0
- data/lib/slack_socket_mode/version.rb +5 -0
- data/lib/slack_socket_mode.rb +92 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8e4dec9d7acbb4f8ef652a7d3f103260e79e05938e3ab80b3cd80a17e47e4fea
|
4
|
+
data.tar.gz: 0a229bf97ad42d1882d6966923ec5ee8ce14095f89ed873ac9a84c934e0e26aa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c013795851fab1344fe9271e84955ffe45f98a0c2ea0bf51c9899656103d127ce56d30a05fde9b6d7f5b68ef6dadf597bddde371e2237ff370996c84fcd016a3
|
7
|
+
data.tar.gz: f6fc381f71ac83a4b510a98a29c607e3d87a43444ec655806dd236377198c8354e93fa39bbdbaed0ed0ce7f9c77ff9107b8e156a837eeb51ab607d15ef324f76
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.6
|
3
|
+
|
4
|
+
Layout/LineLength:
|
5
|
+
Max: 150
|
6
|
+
|
7
|
+
Metrics/AbcSize:
|
8
|
+
Max: 30
|
9
|
+
|
10
|
+
Metrics/ClassLength:
|
11
|
+
Max: 150
|
12
|
+
|
13
|
+
Metrics/CyclomaticComplexity:
|
14
|
+
Max: 10
|
15
|
+
|
16
|
+
Metrics/MethodLength:
|
17
|
+
Max: 30
|
18
|
+
|
19
|
+
Style/Documentation:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Style/StringLiterals:
|
23
|
+
Enabled: true
|
24
|
+
EnforcedStyle: double_quotes
|
25
|
+
|
26
|
+
Style/StringLiteralsInInterpolation:
|
27
|
+
Enabled: true
|
28
|
+
EnforcedStyle: double_quotes
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
slack_socket_mode (0.1.0)
|
5
|
+
faraday
|
6
|
+
websocket_client_lite
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
ast (2.4.2)
|
12
|
+
diff-lcs (1.5.0)
|
13
|
+
faraday (2.7.4)
|
14
|
+
faraday-net_http (>= 2.0, < 3.1)
|
15
|
+
ruby2_keywords (>= 0.0.4)
|
16
|
+
faraday-net_http (3.0.2)
|
17
|
+
json (2.6.3)
|
18
|
+
parallel (1.22.1)
|
19
|
+
parser (3.2.1.1)
|
20
|
+
ast (~> 2.4.1)
|
21
|
+
rainbow (3.1.1)
|
22
|
+
rake (13.0.6)
|
23
|
+
regexp_parser (2.7.0)
|
24
|
+
rexml (3.2.5)
|
25
|
+
rspec (3.12.0)
|
26
|
+
rspec-core (~> 3.12.0)
|
27
|
+
rspec-expectations (~> 3.12.0)
|
28
|
+
rspec-mocks (~> 3.12.0)
|
29
|
+
rspec-core (3.12.1)
|
30
|
+
rspec-support (~> 3.12.0)
|
31
|
+
rspec-expectations (3.12.2)
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
33
|
+
rspec-support (~> 3.12.0)
|
34
|
+
rspec-mocks (3.12.4)
|
35
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
36
|
+
rspec-support (~> 3.12.0)
|
37
|
+
rspec-support (3.12.0)
|
38
|
+
rubocop (1.48.1)
|
39
|
+
json (~> 2.3)
|
40
|
+
parallel (~> 1.10)
|
41
|
+
parser (>= 3.2.0.0)
|
42
|
+
rainbow (>= 2.2.2, < 4.0)
|
43
|
+
regexp_parser (>= 1.8, < 3.0)
|
44
|
+
rexml (>= 3.2.5, < 4.0)
|
45
|
+
rubocop-ast (>= 1.26.0, < 2.0)
|
46
|
+
ruby-progressbar (~> 1.7)
|
47
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
48
|
+
rubocop-ast (1.28.0)
|
49
|
+
parser (>= 3.2.1.0)
|
50
|
+
ruby-progressbar (1.13.0)
|
51
|
+
ruby2_keywords (0.0.5)
|
52
|
+
unicode-display_width (2.4.2)
|
53
|
+
websocket (1.2.9)
|
54
|
+
websocket_client_lite (0.1.1)
|
55
|
+
websocket
|
56
|
+
|
57
|
+
PLATFORMS
|
58
|
+
arm64-darwin-22
|
59
|
+
x86_64-darwin-22
|
60
|
+
|
61
|
+
DEPENDENCIES
|
62
|
+
rake (~> 13.0)
|
63
|
+
rspec (~> 3.0)
|
64
|
+
rubocop (~> 1.21)
|
65
|
+
slack_socket_mode!
|
66
|
+
|
67
|
+
BUNDLED WITH
|
68
|
+
2.4.9
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Ueda Satoshi
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# SlackSocketMode
|
2
|
+
|
3
|
+
Easy to build Slackbot.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
```sh
|
10
|
+
$ bundle add slack_socket_mode
|
11
|
+
```
|
12
|
+
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
14
|
+
|
15
|
+
```sh
|
16
|
+
$ gem install slack_socket_mode
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require 'slack_socket_mode'
|
23
|
+
|
24
|
+
app_token = '...'
|
25
|
+
logger = Logger.new($stdout)
|
26
|
+
client = SlackSocketMode.new(app_token, logger: logger)
|
27
|
+
return unless client.open(debug_reconnects: true)
|
28
|
+
|
29
|
+
client.each_payload do |payload|
|
30
|
+
text = payload.dig(:event, :text)
|
31
|
+
puts text
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
## Development
|
36
|
+
|
37
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
38
|
+
Then, run `rake spec` to run the tests.
|
39
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
40
|
+
|
41
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
42
|
+
To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/gunyoki/slack_socket_mode .
|
47
|
+
|
48
|
+
## License
|
49
|
+
|
50
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "slack_socket_mode/version"
|
4
|
+
require "cgi"
|
5
|
+
require "json"
|
6
|
+
require "faraday"
|
7
|
+
require "websocket_client_lite"
|
8
|
+
|
9
|
+
class SlackSocketMode
|
10
|
+
attr_accessor :slack_app_token, :logger, :websocket
|
11
|
+
|
12
|
+
def initialize(slack_app_token, logger: nil)
|
13
|
+
@slack_app_token = slack_app_token
|
14
|
+
@logger = logger || Logger.new(File::NULL)
|
15
|
+
end
|
16
|
+
|
17
|
+
def open(debug_reconnects: false)
|
18
|
+
ws_url = fetch_websocket_url(@slack_app_token)
|
19
|
+
ws_url = add_query_to_url(ws_url, debug_reconnects: true) if debug_reconnects
|
20
|
+
@websocket = WebsocketClientLite.new(ws_url, logger: @logger)
|
21
|
+
@websocket.handshake
|
22
|
+
end
|
23
|
+
|
24
|
+
def each_payload(&block)
|
25
|
+
@websocket.each_payload do |ws_payload|
|
26
|
+
data = JSON.parse(ws_payload, symbolize_names: true)
|
27
|
+
case data[:type].to_sym
|
28
|
+
when :hello
|
29
|
+
@logger.debug("[slackbot] hello")
|
30
|
+
when :events_api
|
31
|
+
@logger.debug("[slackbot] events_api")
|
32
|
+
if data[:envelope_id]
|
33
|
+
body = { envelope_id: data[:envelope_id] }.to_json
|
34
|
+
@websocket.send_text(body)
|
35
|
+
end
|
36
|
+
payload = data[:payload]
|
37
|
+
next if payload.dig(:event, :hidden)
|
38
|
+
|
39
|
+
block.yield(payload)
|
40
|
+
when :disconnect
|
41
|
+
@logger.debug("[slackbot] disconnect")
|
42
|
+
@websocket.close
|
43
|
+
else
|
44
|
+
@logger.error("[slackbot] Unknown type: #{data[:type]}")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def normalize_text(text)
|
50
|
+
unescape_slack_text(text).unicode_normalize(:nfkc).strip
|
51
|
+
end
|
52
|
+
|
53
|
+
def unescape_slack_text(text)
|
54
|
+
text = text.gsub(/<([?@#!]?)(.*?)>/) { |matched|
|
55
|
+
dt = matched[1]
|
56
|
+
_link, label = dt.split("|", 2)
|
57
|
+
label.to_s
|
58
|
+
}
|
59
|
+
CGI.unescape_html(text)
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def fetch_websocket_url(app_token)
|
65
|
+
api_client = Faraday.new("https://slack.com/api/") { |faraday|
|
66
|
+
faraday.request :authorization, "Bearer", app_token
|
67
|
+
faraday.request :json
|
68
|
+
faraday.response :json
|
69
|
+
faraday.response :raise_error
|
70
|
+
}
|
71
|
+
|
72
|
+
response = api_client.post("apps.connections.open")
|
73
|
+
body = response.body
|
74
|
+
|
75
|
+
if body["ok"]
|
76
|
+
body["url"]
|
77
|
+
else
|
78
|
+
@logger.error("Cannot generate an WebSocket URL: #{body['error']}")
|
79
|
+
nil
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def add_query_to_url(url, query)
|
84
|
+
uri = URI(url)
|
85
|
+
hash = URI.decode_www_form(uri.query.to_s).to_h
|
86
|
+
query.each do |key, value|
|
87
|
+
hash[key.to_s] = value
|
88
|
+
end
|
89
|
+
uri.query = URI.encode_www_form(hash)
|
90
|
+
uri.to_s
|
91
|
+
end
|
92
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slack_socket_mode
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ueda Satoshi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-03-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: websocket_client_lite
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: It works as a Slackbot by simply implementing a block that handles messages.
|
42
|
+
email:
|
43
|
+
- gunyoki@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".rspec"
|
49
|
+
- ".rubocop.yml"
|
50
|
+
- CHANGELOG.md
|
51
|
+
- Gemfile
|
52
|
+
- Gemfile.lock
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- lib/slack_socket_mode.rb
|
57
|
+
- lib/slack_socket_mode/version.rb
|
58
|
+
homepage: https://github.com/gunyoki/slack_socket_mode
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata:
|
62
|
+
homepage_uri: https://github.com/gunyoki/slack_socket_mode
|
63
|
+
source_code_uri: https://github.com/gunyoki/slack_socket_mode
|
64
|
+
changelog_uri: https://github.com/gunyoki/slack_socket_mode/blob/main/CHANGELOG.md
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 2.6.0
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubygems_version: 3.4.6
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Easy to build Slackbot
|
84
|
+
test_files: []
|