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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f4e6f503ee04730d785137ffdeb1cf5d05b23cc2162b0cb01aec45d95472f36d
4
- data.tar.gz: 1eb9c637a1e67e990931887214f5af1d0b2c1d302dfd1a323f1b13565fdfa977
3
+ metadata.gz: 88a291f8028f19bacd30f4c7cd8fbcbb56fdfa3463dc916289fbf6f8447df94c
4
+ data.tar.gz: 68aa571fdf8b0b682551621601e1cfc37128823584596aa99a9ef5130d996c6c
5
5
  SHA512:
6
- metadata.gz: 43c76e8faec08b4efb1e54d9fd76b1c557b12ae0b438df483222929cbfbeb5af43de6ff87a8b135fb1bc1af55f977c19330a564e1b0cc68553438c40e6f4c771
7
- data.tar.gz: 0d18adae3b661b94d31b03b800dac99e2fb1c0df4685f621e75cbb197c7fa98b35e9f2eb6b1db40abd3998fcd95fa60c992e9ee3c547638cb0ab9ad15baa2281
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.3"
15
+ cc-report-collect-ruby: "3.2"
16
16
  matrix:
17
17
  parameters:
18
- ruby-version: ["3.2.3" , "3.2"]
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- slack_bot-events (0.0.1)
4
+ slack_bot-events (0.1.0)
5
5
  class_composer (~> 1.0)
6
6
  faraday
7
7
  faye-websocket
@@ -13,32 +13,70 @@ module SlackBot
13
13
  def start!
14
14
  EM.run do
15
15
  websocket.on :open do |event|
16
- p [:open]
16
+ event_tracer(:open)
17
17
  end
18
18
 
19
19
  websocket.on :message do |event|
20
- parsed_data = JSON.parse(event.data)
21
- if parsed_data["type"] == "events_api"
22
- puts parsed_data.to_json
23
- item = SlackBot::Events::Schemas::SocketPayload.new(parsed_data)
24
- s = {
25
- type: item.payload.event.type,
26
- tldr: item.payload.event.tldr,
27
- }
28
- puts s
29
- websocket.send("#{{ envelope_id: item.envelope_id }.to_json}")
30
- else
31
- puts event.data
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
- p [:close, event.code, event.reason]
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
- @listeners ||= {}
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
- @listeners[name] = handler
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
@@ -23,6 +23,10 @@ module SlackBot
23
23
  item.channel
24
24
  end
25
25
 
26
+ def message_ts
27
+ item.ts
28
+ end
29
+
26
30
  def tldr
27
31
  "type: #{type}; channel:#{channel}; reaction:#{reaction}"
28
32
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module SlackBot
4
4
  module Events
5
- VERSION = "0.0.3"
5
+ VERSION = "0.1.0"
6
6
  end
7
7
  end
@@ -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"
@@ -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.2")
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.3
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-16 00:00:00.000000000 Z
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.2'
162
+ version: '3.1'
162
163
  required_rubygems_version: !ruby/object:Gem::Requirement
163
164
  requirements:
164
165
  - - ">="