Class: SlackSocketModeBot
- Inherits:
-
Object
- Object
- SlackSocketModeBot
- Defined in:
- lib/slack_socket_mode_bot.rb,
lib/slack_socket_mode_bot/version.rb,
sig/slack_socket_mode.rbs
Defined Under Namespace
Classes: Error, SimpleWebSocket
Constant Summary collapse
- API_BASE =
"https://slack.com/api/"- RETRY_AFTER_CAP =
Max seconds to sleep on a 429 Retry-After; #call may run in the event loop.
60- VERSION =
"0.9.2"
Instance Method Summary collapse
-
#call(method, data, token: @token) ⇒ Object
: (String method, untyped data, ?token: String) -> untyped.
-
#initialize(token:, app_token: nil, num_of_connections: 4, debug: false, logger: nil) {|arg0| ... } ⇒ SlackSocketModeBot
constructor
: (token: String, ?app_token: String, ?num_of_connections: Integer, ?debug: boolean, ?logger: Logger) { (untyped) -> untyped } -> void.
-
#run ⇒ void
: -> bot.
-
#step ⇒ [ Array[IO], Array[IO] ]
: -> [Array[IO], Array[IO]].
Constructor Details
#initialize(token:, app_token: nil, num_of_connections: 4, debug: false, logger: nil) {|arg0| ... } ⇒ SlackSocketModeBot
: (token: String, ?app_token: String, ?num_of_connections: Integer, ?debug: boolean, ?logger: Logger) { (untyped) -> untyped } -> void
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/slack_socket_mode_bot.rb', line 31 def initialize(token:, app_token: nil, num_of_connections: 4, debug: false, logger: nil, &callback) @token = token @app_token = app_token @conns = [] @debug = debug @logger = logger @events = {} @callback = callback # No app token: Web API calls only, no Socket Mode connections. @num_of_connections = app_token ? num_of_connections : 0 replenish_connections end |
Instance Method Details
#call(method, data, token: @token) ⇒ Object
: (String method, untyped data, ?token: String) -> untyped
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/slack_socket_mode_bot.rb', line 45 def call(method, data, token: @token) url = URI(API_BASE + method) body = JSON.generate(data) headers = { "Content-type" => "application/json; charset=utf-8", "Authorization" => "Bearer " + token, } retries = 0 begin res = Net::HTTP.post(url, body, headers) case res when Net::HTTPSuccess json = JSON.parse(res.body, symbolize_names: true) raise Error, json[:error] unless json[:ok] json when Net::HTTPTooManyRequests # Rejected, not processed: safe to retry after Retry-After seconds. raise Retry, Integer(res["retry-after"] || retries + 1) else # 5xx etc.: may already be processed, so don't retry; just don't crash. raise Error, "HTTP #{ res.code } #{ res. }" end rescue Socket::ResolutionError, Net::OpenTimeout # Never sent (DNS / connect failure): safe to retry. retries += 1 raise if retries >= 3 sleep 1 retry rescue Retry => e # Don't block the event loop on an absurdly long wait. retries += 1 raise Error, "rate limited (retry-after: #{ e.wait }s)" if retries >= 3 || e.wait > RETRY_AFTER_CAP sleep e.wait retry end end |
#run ⇒ void
This method returns an undefined value.
: -> bot
176 177 178 179 180 181 |
# File 'lib/slack_socket_mode_bot.rb', line 176 def run while true read_ios, write_ios = step IO.select(read_ios, write_ios) end end |
#step ⇒ [ Array[IO], Array[IO] ]
: -> [Array[IO], Array[IO]]
168 169 170 171 172 173 |
# File 'lib/slack_socket_mode_bot.rb', line 168 def step read_ios, write_ios = [], [] @conns.select! {|ws| ws.step(read_ios, write_ios) } replenish_connections return read_ios, write_ios end |