Class: SlackSocketMode
- Inherits:
-
Object
- Object
- SlackSocketMode
- Defined in:
- lib/slack_socket_mode.rb,
lib/slack_socket_mode/version.rb
Constant Summary collapse
- VERSION =
"0.1.1"
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#slack_app_token ⇒ Object
Returns the value of attribute slack_app_token.
-
#websocket ⇒ Object
Returns the value of attribute websocket.
Instance Method Summary collapse
- #each_payload(&block) ⇒ Object
-
#initialize(slack_app_token, logger: nil) ⇒ SlackSocketMode
constructor
A new instance of SlackSocketMode.
- #normalize_text(text) ⇒ Object
- #open(debug_reconnects: false) ⇒ Object
- #unescape_slack_text(text) ⇒ Object
Constructor Details
#initialize(slack_app_token, logger: nil) ⇒ SlackSocketMode
Returns a new instance of SlackSocketMode.
12 13 14 15 |
# File 'lib/slack_socket_mode.rb', line 12 def initialize(slack_app_token, logger: nil) @slack_app_token = slack_app_token @logger = logger || Logger.new(File::NULL) end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
10 11 12 |
# File 'lib/slack_socket_mode.rb', line 10 def logger @logger end |
#slack_app_token ⇒ Object
Returns the value of attribute slack_app_token.
10 11 12 |
# File 'lib/slack_socket_mode.rb', line 10 def slack_app_token @slack_app_token end |
#websocket ⇒ Object
Returns the value of attribute websocket.
10 11 12 |
# File 'lib/slack_socket_mode.rb', line 10 def websocket @websocket end |
Instance Method Details
#each_payload(&block) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/slack_socket_mode.rb', line 28 def each_payload(&block) @websocket.each_payload do |ws_payload| data = JSON.parse(ws_payload, symbolize_names: true) case data[:type].to_sym when :hello @logger.debug("[slackbot] hello") when :events_api @logger.debug("[slackbot] events_api") if data[:envelope_id] body = { envelope_id: data[:envelope_id] }.to_json @websocket.send_text(body) end payload = data[:payload] next if payload.dig(:event, :hidden) block.yield(payload) when :disconnect @logger.debug("[slackbot] disconnect") @websocket.close else @logger.error("[slackbot] Unknown type: #{data[:type]}") end end end |
#normalize_text(text) ⇒ Object
53 54 55 |
# File 'lib/slack_socket_mode.rb', line 53 def normalize_text(text) unescape_slack_text(text).unicode_normalize(:nfkc).strip end |
#open(debug_reconnects: false) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/slack_socket_mode.rb', line 17 def open(debug_reconnects: false) ws_url = fetch_websocket_url(@slack_app_token) return false unless ws_url ws_url = add_query_to_url(ws_url, debug_reconnects: true) if debug_reconnects @logger.debug("[slackbot] WebSocket URL: #{ws_url}") @websocket = WebsocketClientLite.new(ws_url, logger: @logger) @websocket.handshake end |
#unescape_slack_text(text) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/slack_socket_mode.rb', line 57 def unescape_slack_text(text) text = text.gsub(/<([?@#!]?)(.*?)>/) { |matched| dt = matched[1] _link, label = dt.split("|", 2) label.to_s } CGI.unescape_html(text) end |