slack_bot-events 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/slack_bot/events/client.rb +18 -28
- data/lib/slack_bot/events/configuration.rb +6 -0
- data/lib/slack_bot/events/middleware/chain.rb +6 -3
- data/lib/slack_bot/events/middleware/event_tracer.rb +7 -3
- data/lib/slack_bot/events/middleware/message_handler.rb +52 -0
- data/lib/slack_bot/events/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 653594899c76b05d93054ceb20520d84a487a38451ecee7d561ab5b9e6590bf7
|
4
|
+
data.tar.gz: 3f722375d06dce4ca2a3f4151ed221948b9e1d5e6d87842cf966ff067c54c31b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb0fb66fded40a580f042bd45e9ba139cf3753d2960461265d07394a5c182a35c9f35d33105e7ca5536b6e279c44335baf6b8ba645e531186ffbf3c4d9790dcc
|
7
|
+
data.tar.gz: 02f478db61cabfce5f8e66b7303f47f9ed548d08dafded39d364e579f68ebf448f17ddf851e9717bfea0e755ab41e0821d450ab4b4e3353823a3b161e9ad0dea
|
data/Gemfile.lock
CHANGED
@@ -17,10 +17,10 @@ module SlackBot
|
|
17
17
|
end
|
18
18
|
|
19
19
|
websocket.on :message do |socket_event|
|
20
|
-
process_message(socket_event: socket_event) do |parsed_data:, schema: nil|
|
20
|
+
process_message(socket_event: socket_event) do |listener:, parsed_data:, schema: nil|
|
21
21
|
case parsed_data["type"]
|
22
22
|
when "events_api"
|
23
|
-
events_api(schema: schema, parsed_data: parsed_data)
|
23
|
+
events_api(handler: listener[:handler], schema: schema, parsed_data: parsed_data)
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
@@ -37,8 +37,11 @@ module SlackBot
|
|
37
37
|
|
38
38
|
def process_message(socket_event:)
|
39
39
|
schema_data = Schematize.call(data: socket_event.data)
|
40
|
-
|
41
|
-
|
40
|
+
|
41
|
+
listener = find_listener(schema: schema_data[:schema]) # Events.config.listeners[schema_data[:schema]&.type.to_sym]
|
42
|
+
|
43
|
+
SlackBot::Events.message_middleware.invoke_message(websocket: websocket, listener: listener, type: :message, socket_event: socket_event, **schema_data) do
|
44
|
+
yield(listener: listener, **schema_data) if block_given?
|
42
45
|
end
|
43
46
|
end
|
44
47
|
|
@@ -48,33 +51,22 @@ module SlackBot
|
|
48
51
|
end
|
49
52
|
end
|
50
53
|
|
51
|
-
def events_api(schema:, parsed_data:)
|
52
|
-
if
|
53
|
-
Events.logger.info { schema.tldr }
|
54
|
-
end
|
54
|
+
def events_api(handler:, schema:, parsed_data:)
|
55
|
+
return if listener.nil?
|
55
56
|
|
56
|
-
|
57
|
-
if object
|
58
|
-
safe_handler(type: schema.type.to_sym, object: object, schema: schema, parsed_data: parsed_data)
|
59
|
-
end
|
57
|
+
Events.logger.info { schema.tldr } if Events.config.print_tldr
|
60
58
|
|
61
|
-
|
59
|
+
# This gets rescued in the MessageHandler middleware
|
60
|
+
# on_success and on_failure happens there as well
|
61
|
+
handler.call(schema: schema, raw_data: parsed_data)
|
62
62
|
end
|
63
63
|
|
64
64
|
private
|
65
65
|
|
66
|
-
def
|
67
|
-
|
68
|
-
|
69
|
-
|
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
|
66
|
+
def find_listener(schema:)
|
67
|
+
return nil if schema.nil?
|
68
|
+
|
69
|
+
Events.config.listeners[schema.type.to_sym]
|
78
70
|
end
|
79
71
|
|
80
72
|
def websocket
|
@@ -93,9 +85,7 @@ module SlackBot
|
|
93
85
|
end
|
94
86
|
|
95
87
|
def faraday_headers
|
96
|
-
{
|
97
|
-
'Authorization' => "Bearer #{SlackBot::Events.config.client_socket_token}"
|
98
|
-
}
|
88
|
+
{ 'Authorization' => "Bearer #{SlackBot::Events.config.client_socket_token}" }
|
99
89
|
end
|
100
90
|
end
|
101
91
|
end
|
@@ -26,6 +26,12 @@ module SlackBot
|
|
26
26
|
add_composer :close_middleware, allowed: Middleware::Chain, default: Middleware::Chain.new(type: :close)
|
27
27
|
add_composer :envelope_acknowledge, allowed: Symbol, default: DEFAULT_ACKNOWLEDGE, validator: ->(val) { ALLOWED_ACKNOWLEDGE.include?(val) }, invalid_message: ->(_) { "Must by a Symbol in #{ALLOWED_ACKNOWLEDGE}" }
|
28
28
|
|
29
|
+
ALLOWED_ACKNOWLEDGE.each do |ack|
|
30
|
+
define_method :"acknowledge_#{ack}?" do
|
31
|
+
envelope_acknowledge == ack
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
29
35
|
def register_listener(name:, handler:, on_success: nil, on_failure: nil)
|
30
36
|
if event_handler = listeners[name.to_sym]
|
31
37
|
logger.warn "`#{name}` already exists as listener event. Reseting with new input"
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "slack_bot/events/middleware/event_tracer"
|
4
|
+
require "slack_bot/events/middleware/message_handler"
|
4
5
|
|
5
6
|
####################
|
6
7
|
#
|
@@ -18,7 +19,7 @@ module SlackBot
|
|
18
19
|
attr_reader :type
|
19
20
|
|
20
21
|
DEFAULT_ENTRIES = {
|
21
|
-
message: [Middleware::EventTracer],
|
22
|
+
message: [Middleware::EventTracer, Middleware::MessageHandler],
|
22
23
|
open: [Middleware::EventTracer],
|
23
24
|
close: [Middleware::EventTracer],
|
24
25
|
}
|
@@ -85,19 +86,21 @@ module SlackBot
|
|
85
86
|
@entries = nil
|
86
87
|
end
|
87
88
|
|
88
|
-
def invoke_message(type:, socket_event:, parsed_data:, schema: nil)
|
89
|
+
def invoke_message(type:, socket_event:, parsed_data:, websocket:, listener:, schema: nil)
|
89
90
|
return yield if empty?
|
90
91
|
|
91
92
|
chain = retrieve
|
92
93
|
traverse_chain = proc do
|
93
94
|
if chain.empty?
|
94
|
-
yield
|
95
|
+
yield
|
95
96
|
else
|
96
97
|
params = {
|
97
98
|
parsed_data: parsed_data,
|
98
99
|
schema: schema,
|
99
100
|
socket_event: socket_event,
|
101
|
+
listener: listener,
|
100
102
|
type: type,
|
103
|
+
websocket: websocket,
|
101
104
|
}
|
102
105
|
chain.shift.call(**params, &traverse_chain)
|
103
106
|
end
|
@@ -4,14 +4,18 @@ module SlackBot
|
|
4
4
|
module Events
|
5
5
|
module Middleware
|
6
6
|
class EventTracer
|
7
|
-
def call(type:, socket_event:, schema: nil, parsed_data: nil)
|
7
|
+
def call(type:, socket_event:, schema: nil, parsed_data: nil, **params)
|
8
8
|
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
9
9
|
temp_type = type.dup.to_s
|
10
10
|
case type
|
11
11
|
when :close
|
12
12
|
additional_info = "code: #{socket_event.code} reason:#{socket_event.reason}"
|
13
13
|
when :message
|
14
|
-
|
14
|
+
accurate_type = parsed_data.dig("payload","event","subtype") || parsed_data.dig("payload","event","type")
|
15
|
+
p_type = [
|
16
|
+
parsed_data.dig("type"),
|
17
|
+
accurate_type
|
18
|
+
].compact.join(":")
|
15
19
|
case p_type
|
16
20
|
when "app_rate_limited"
|
17
21
|
# https://api.slack.com/apis/rate-limits#events
|
@@ -32,7 +36,7 @@ module SlackBot
|
|
32
36
|
yield
|
33
37
|
|
34
38
|
elapsed_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
|
35
|
-
Events.logger.info { "[Event Finished] [#{(elapsed_time * 1000).round(2)}ms]
|
39
|
+
Events.logger.info { "[Event Finished] #{temp_type} [#{(elapsed_time * 1000).round(2)}ms]" }
|
36
40
|
end
|
37
41
|
end
|
38
42
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SlackBot
|
4
|
+
module Events
|
5
|
+
module Middleware
|
6
|
+
class MessageHandler
|
7
|
+
def call(schema:, websocket:, listener:, **params)
|
8
|
+
if SlackBot::Events.config.acknowledge_on_receive?
|
9
|
+
acknowledge!(websocket: websocket, schema: schema)
|
10
|
+
end
|
11
|
+
|
12
|
+
yield
|
13
|
+
|
14
|
+
listener[:on_success]&.call(schema) if listener
|
15
|
+
|
16
|
+
if SlackBot::Events.config.acknowledge_on_success? || SlackBot::Events.config.acknowledge_on_complete?
|
17
|
+
acknowledge!(websocket: websocket, schema: schema)
|
18
|
+
end
|
19
|
+
rescue StandardError => error
|
20
|
+
puts error.message
|
21
|
+
puts error.backtrace
|
22
|
+
Events.logger.error do
|
23
|
+
"#{listener[:handler]} returned #{error.class} => #{error.message}. #{error.backtrace[0...10]}"
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
listener[:on_failure]&.call(schema, error) if listener
|
28
|
+
rescue StandardError => on_failure_error
|
29
|
+
Events.logger.error("Failure occured during on_failure block. #{on_failure_error}")
|
30
|
+
end
|
31
|
+
|
32
|
+
if SlackBot::Events.config.acknowledge_on_complete?
|
33
|
+
acknowledge!(websocket: websocket, schema: schema)
|
34
|
+
elsif SlackBot::Events.config.acknowledge_on_success?
|
35
|
+
Events.logger.debug do
|
36
|
+
"Envelope acknowledgment skipped. Ackowledgment on success only. Slack may send a duplicate message"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def acknowledge!(websocket:, schema:)
|
44
|
+
return if schema.nil?
|
45
|
+
|
46
|
+
websocket.send("#{{ envelope_id: schema.envelope_id }.to_json}")
|
47
|
+
Events.logger.debug { "Envelope acknowledgment completed [#{SlackBot::Events.config.envelope_acknowledge}]" }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
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.
|
4
|
+
version: 0.4.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-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -136,6 +136,7 @@ files:
|
|
136
136
|
- lib/slack_bot/events/configuration.rb
|
137
137
|
- lib/slack_bot/events/middleware/chain.rb
|
138
138
|
- lib/slack_bot/events/middleware/event_tracer.rb
|
139
|
+
- lib/slack_bot/events/middleware/message_handler.rb
|
139
140
|
- lib/slack_bot/events/schemas/authorization.rb
|
140
141
|
- lib/slack_bot/events/schemas/data_payload.rb
|
141
142
|
- lib/slack_bot/events/schemas/socket_payload.rb
|