Class: Ruboty::SlackEvents::Api::SocketClient
- Inherits:
-
Object
- Object
- Ruboty::SlackEvents::Api::SocketClient
- Defined in:
- lib/ruboty/slack_events/api/socket_client.rb
Instance Attribute Summary collapse
-
#slack_client ⇒ Object
readonly
: Slack::Web::Client.
Instance Method Summary collapse
-
#connect(&callback) ⇒ Object
: Async::Task.
-
#initialize(slack_client:, auto_reconnect: false) ⇒ SocketClient
constructor
A new instance of SocketClient.
Constructor Details
#initialize(slack_client:, auto_reconnect: false) ⇒ SocketClient
Returns a new instance of SocketClient.
17 18 19 20 |
# File 'lib/ruboty/slack_events/api/socket_client.rb', line 17 def initialize(slack_client:, auto_reconnect: false) @slack_client = slack_client @auto_reconnect = auto_reconnect end |
Instance Attribute Details
#slack_client ⇒ Object (readonly)
: Slack::Web::Client
14 15 16 |
# File 'lib/ruboty/slack_events/api/socket_client.rb', line 14 def slack_client @slack_client end |
Instance Method Details
#connect(&callback) ⇒ Object
: Async::Task
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ruboty/slack_events/api/socket_client.rb', line 22 def connect(&callback) #: Async::Task res = slack_client.apps_connections_open #: Slack::Messages::Message raise res.error unless res.ok url = res.url endpoint = Async::HTTP::Endpoint.parse(url) Async do Async::WebSocket::Client.connect(endpoint) do |connection| while (payload = connection.read) #: Protocol::WebSocket::TextMessage response = payload.to_h = Slack::Messages::Message.new(response) if .envelope_id # https://api.slack.com/apis/socket-mode#acknowledge = Protocol::WebSocket::TextMessage.generate({ envelope_id: .envelope_id }) .send(connection) connection.flush end begin callback&.call() rescue StandardError => e SlackEvents::Logger.error("UnhandledError on socket API callback") { e. } end end end end end |