slack_bot-events 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +2 -2
- data/Gemfile.lock +1 -1
- data/lib/slack_bot/events/client.rb +52 -14
- data/lib/slack_bot/events/configuration.rb +43 -5
- data/lib/slack_bot/events/schemas/socket_payload.rb +11 -0
- data/lib/slack_bot/events/schemas/type/message.rb +17 -0
- data/lib/slack_bot/events/schemas/type/reaction_modified.rb +4 -0
- data/lib/slack_bot/events/version.rb +1 -1
- data/lib/slack_bot/events.rb +6 -2
- data/lib/slack_bot-events.rb +1 -0
- data/slack_bot-events.gemspec +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 88a291f8028f19bacd30f4c7cd8fbcbb56fdfa3463dc916289fbf6f8447df94c
|
4
|
+
data.tar.gz: 68aa571fdf8b0b682551621601e1cfc37128823584596aa99a9ef5130d996c6c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5488211f2ebbbdc4c5960fa03d49d859e41fce30432fb42438951ab90f6f9ccb1b410cae8fcec3dcbe86d4c2c17e5ea520eb2ec7f73eebf95192ef5092572463
|
7
|
+
data.tar.gz: 34d761af5cbc44e718dd44cd5ed9612e7270bbbb7f92ea88df68aceab85e23352df83fecd3b8f227155cb3e03127354dcf7bc387a2339b057890f6aab245b2f8
|
data/.circleci/config.yml
CHANGED
@@ -12,10 +12,10 @@ workflows:
|
|
12
12
|
- cst/enforce-gem-version-bump
|
13
13
|
- cst/rspec-ruby:
|
14
14
|
rspec-system-args: "SIMPLE_COV_RUN=true"
|
15
|
-
cc-report-collect-ruby: "3.2
|
15
|
+
cc-report-collect-ruby: "3.2"
|
16
16
|
matrix:
|
17
17
|
parameters:
|
18
|
-
ruby-version: ["3.
|
18
|
+
ruby-version: ["3.1" , "3.2", "3.3"]
|
19
19
|
alias: required-matrix-tests
|
20
20
|
name: test-ruby<< matrix.ruby-version >>
|
21
21
|
- cst/publish-gem:
|
data/Gemfile.lock
CHANGED
@@ -13,32 +13,70 @@ module SlackBot
|
|
13
13
|
def start!
|
14
14
|
EM.run do
|
15
15
|
websocket.on :open do |event|
|
16
|
-
|
16
|
+
event_tracer(:open)
|
17
17
|
end
|
18
18
|
|
19
19
|
websocket.on :message do |event|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
20
|
+
event_tracer(:message) do
|
21
|
+
parsed_data = JSON.parse(event.data)
|
22
|
+
case parsed_data["type"]
|
23
|
+
when "events_api"
|
24
|
+
events_api(parsed_data)
|
25
|
+
else "hello"
|
26
|
+
params = {
|
27
|
+
num_connections: parsed_data["num_connections"],
|
28
|
+
debug_info_host: parsed_data["debug_info"]["host"],
|
29
|
+
debug_info_connect_time: parsed_data["debug_info"]["approximate_connection_time"],
|
30
|
+
}
|
31
|
+
event_tracer("message:hello", **params)
|
32
|
+
end
|
32
33
|
end
|
33
34
|
end
|
34
35
|
|
35
36
|
websocket.on :close do |event|
|
36
|
-
|
37
|
+
event_tracer(:close, code: event.code, reason: event.reason)
|
37
38
|
@websocket = nil
|
38
39
|
end
|
39
40
|
end
|
40
41
|
end
|
41
42
|
|
43
|
+
def events_api(parsed_data)
|
44
|
+
schematized = SlackBot::Events::Schemas::SocketPayload.new(parsed_data)
|
45
|
+
Events.logger.info(schematized.tldr) if Events.config.print_tldr
|
46
|
+
object = Events.config.listeners[schematized.type.to_sym]
|
47
|
+
if object
|
48
|
+
safe_handler(type: schematized.type.to_sym, object: object, schema: schematized, parsed_data: parsed_data)
|
49
|
+
end
|
50
|
+
websocket.send("#{{ envelope_id: schematized.envelope_id }.to_json}")
|
51
|
+
end
|
52
|
+
|
53
|
+
def event_tracer(type, **params)
|
54
|
+
stringify = params.map { |k,v| "#{k}:#{v}" }.join("; ")
|
55
|
+
Events.logger.info "[Event Received] #{type} #{stringify}"
|
56
|
+
if block_given?
|
57
|
+
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
58
|
+
yield
|
59
|
+
elapsed_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
|
60
|
+
Events.logger.info "[Event completed] [#{elapsed_time.round(2)}s] #{type} #{stringify}"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def safe_handler(type:, object:, schema:, parsed_data:)
|
67
|
+
Events.logger.info "Received Handler for #{type}"
|
68
|
+
object[:handler].call(schema: schema, raw_data: parsed_data)
|
69
|
+
object[:on_success]&.call(schema)
|
70
|
+
rescue => error
|
71
|
+
Events.logger.error("#{object[:handler]} returned #{error.class} => #{error.message}. Safely returning to websocket. #{error.backtrace[0...10]}")
|
72
|
+
|
73
|
+
begin
|
74
|
+
object[:on_failure]&.call(schema, error)
|
75
|
+
rescue => on_failure_error
|
76
|
+
Events.logger.error("Failure occured during on_failure block. #{on_failure_error}")
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
42
80
|
def websocket
|
43
81
|
@websocket ||= Faye::WebSocket::Client.new(socket_endpoint)
|
44
82
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "class_composer"
|
4
|
+
require "logger"
|
4
5
|
|
5
6
|
module SlackBot
|
6
7
|
module Events
|
@@ -11,22 +12,59 @@ module SlackBot
|
|
11
12
|
add_composer :client_signing_secret, allowed: String, default: ENV["SLACK_SIGNING_SECRET"]
|
12
13
|
add_composer :client_socket_token, allowed: String, default: ENV["SLACK_SOCKET_TOKEN"]
|
13
14
|
add_composer :client_verification_token, allowed: String, default: ENV["SLACK_VERIFICATION_TOKEN"]
|
15
|
+
add_composer :print_tldr, allowed: [true.class, false.class], default: true
|
14
16
|
|
15
|
-
def register_listener(name:, handler:)
|
16
|
-
|
17
|
-
|
18
|
-
if @listeners.has_key(name.to_sym)
|
17
|
+
def register_listener(name:, handler:, on_success: nil, on_failure: nil)
|
18
|
+
if event_handler = listeners[name.to_sym]
|
19
|
+
logger.warn "`#{name}` already exists as listener event. Reseting with new input"
|
19
20
|
end
|
20
21
|
|
21
|
-
|
22
|
+
validate_listener!(handler: handler, on_success: on_success, on_failure: on_failure)
|
22
23
|
|
24
|
+
listeners[name.to_sym] = { handler: handler, name: name, on_success: on_success, on_failure: on_failure }
|
23
25
|
true
|
24
26
|
end
|
25
27
|
|
26
28
|
def remove_listener(name:)
|
29
|
+
listeners ||= {}
|
30
|
+
end
|
31
|
+
|
32
|
+
def logger
|
33
|
+
@logger ||= Logger.new(STDOUT)
|
34
|
+
end
|
35
|
+
|
36
|
+
def logger=(log)
|
37
|
+
@logger = log
|
38
|
+
end
|
39
|
+
|
40
|
+
def listeners
|
27
41
|
@listeners ||= {}
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def validate_listener!(handler:, on_success:, on_failure:)
|
47
|
+
unless retreive_method(object: handler, meth_name: :call)
|
48
|
+
raise ArgumentError, "When present, handler argument needs to respond_to :call with schema: and raw_data: kwargs"
|
49
|
+
end
|
28
50
|
|
51
|
+
if on_success
|
52
|
+
unless retreive_method(object: on_success, meth_name: :call)
|
53
|
+
raise ArgumentError, "When present, on_success argument needs to respond_to :call with 1 arguments passed in"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
if on_failure
|
58
|
+
unless retreive_method(object: on_failure, meth_name: :call)
|
59
|
+
raise ArgumentError, "When present, on_failure argument needs to respond_to :call with 2 arguments passed in"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
29
63
|
|
64
|
+
def retreive_method(object:, meth_name:)
|
65
|
+
object.method(meth_name.to_sym)
|
66
|
+
rescue NameError
|
67
|
+
nil
|
30
68
|
end
|
31
69
|
end
|
32
70
|
end
|
@@ -19,6 +19,17 @@ module SlackBot
|
|
19
19
|
add_field name: :accepts_response_payload, type: JsonSchematize::Boolean
|
20
20
|
add_field name: :retry_attempt, type: Integer
|
21
21
|
add_field name: :retry_reason, type: String
|
22
|
+
|
23
|
+
|
24
|
+
def type
|
25
|
+
payload.event.type
|
26
|
+
end
|
27
|
+
|
28
|
+
def tldr
|
29
|
+
retry_language = "#{retry_attempt}"
|
30
|
+
retry_language += ":#{retry_reason}" if retry_attempt > 0
|
31
|
+
"tldr:#{payload.event.tldr}; retry:#{retry_language}"
|
32
|
+
end
|
22
33
|
end
|
23
34
|
end
|
24
35
|
end
|
@@ -21,6 +21,9 @@ module SlackBot
|
|
21
21
|
|
22
22
|
add_field name: :blocks, type: SlackBot::Events::Schemas::Type::Block, array_of_types: true
|
23
23
|
|
24
|
+
add_field name: :thread_ts, type: String, required: false
|
25
|
+
add_field name: :parent_user_id, type: String, required: false
|
26
|
+
|
24
27
|
add_field name: :channel, type: String
|
25
28
|
add_field name: :event_ts, type: String
|
26
29
|
add_field name: :channel_type, type: String
|
@@ -28,6 +31,20 @@ module SlackBot
|
|
28
31
|
def tldr
|
29
32
|
"type: #{type}; channel:#{channel}; raw_text:#{text}"
|
30
33
|
end
|
34
|
+
|
35
|
+
def thread_ts
|
36
|
+
return_nil?(@thread_ts) ? nil : @thread_ts
|
37
|
+
end
|
38
|
+
|
39
|
+
def parent_user_id
|
40
|
+
return_nil?(@parent_user_id) ? nil : @parent_user_id
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def return_nil?(val)
|
46
|
+
JsonSchematize::EmptyValue === val || val.nil?
|
47
|
+
end
|
31
48
|
end
|
32
49
|
end
|
33
50
|
end
|
data/lib/slack_bot/events.rb
CHANGED
@@ -29,12 +29,16 @@ module SlackBot
|
|
29
29
|
raise Error, "Expected configuration to be a SlackBot::Events::Configuration"
|
30
30
|
end
|
31
31
|
|
32
|
-
def self.register_listener(name:, handler:)
|
33
|
-
config.register_listener(name: name, handler: handler)
|
32
|
+
def self.register_listener(name:, handler:, on_success: nil, on_failure: nil)
|
33
|
+
config.register_listener(name: name, handler: handler, on_success: on_success, on_failure: on_failure)
|
34
34
|
end
|
35
35
|
|
36
36
|
def self.remove_listener(name:)
|
37
37
|
config.remove_listener(name: name)
|
38
38
|
end
|
39
|
+
|
40
|
+
def self.logger
|
41
|
+
config.logger
|
42
|
+
end
|
39
43
|
end
|
40
44
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "slack_bot/events"
|
data/slack_bot-events.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.description = "Describe the gem here"
|
20
20
|
spec.homepage = "https://github.com/matt-taylor/slack_bot-events"
|
21
21
|
|
22
|
-
spec.required_ruby_version = Gem::Requirement.new(">= 3.
|
22
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 3.1")
|
23
23
|
|
24
24
|
spec.metadata = {
|
25
25
|
"github_repo" => spec.homepage,
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slack_bot-events
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Taylor
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-06-
|
11
|
+
date: 2024-06-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -130,6 +130,7 @@ files:
|
|
130
130
|
- bin/setup
|
131
131
|
- bin/start
|
132
132
|
- docker-compose.yml
|
133
|
+
- lib/slack_bot-events.rb
|
133
134
|
- lib/slack_bot/events.rb
|
134
135
|
- lib/slack_bot/events/client.rb
|
135
136
|
- lib/slack_bot/events/configuration.rb
|
@@ -158,7 +159,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
158
159
|
requirements:
|
159
160
|
- - ">="
|
160
161
|
- !ruby/object:Gem::Version
|
161
|
-
version: '3.
|
162
|
+
version: '3.1'
|
162
163
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
163
164
|
requirements:
|
164
165
|
- - ">="
|