Class: Lich::DragonRealms::SlackBot
- Inherits:
-
Object
- Object
- Lich::DragonRealms::SlackBot
- Defined in:
- documented/dragonrealms/commons/slackbot.rb
Overview
Represents a Slack bot for interacting with the Slack API.
This class handles authentication, user messaging, and error management.
Defined Under Namespace
Classes: ApiError, Error, NetworkError, ThrottlingError
Constant Summary collapse
- API_URL =
'https://slack.com/api/'- LNET_SCRIPT_NAME =
'lnet'- LICHBOTS =
%w[Quilsilgas].freeze
- TOKEN_REQUEST_TIMEOUT =
10- MAX_NETWORK_RETRIES =
5- NO_USER_PATTERN =
/\[server\]: "no user .*/.freeze
- LNET_CONNECTION_TIMEOUT =
30- LNET_ACTIVITY_TIMEOUT =
120- BASE_RETRY_DELAY_SECONDS =
30- MAX_RETRY_DELAY_SECONDS =
300- USERS_CACHE_SCRIPT =
'_slackbot_users_cache'- USERS_CACHE_TTL =
3600- MAX_THROTTLE_RETRIES =
10
Instance Attribute Summary collapse
-
#error_message ⇒ Object
readonly
Returns the value of attribute error_message.
Instance Method Summary collapse
-
#authed?(token) ⇒ Boolean
Checks if the provided token is authenticated with the Slack API.
-
#direct_message(username, message) ⇒ void
Sends a direct message to a specified user on Slack.
-
#initialize ⇒ SlackBot
constructor
A new instance of SlackBot.
-
#initialized? ⇒ Boolean
Checks if the SlackBot has been initialized successfully.
- #lnet_connected? ⇒ Boolean
-
#reconnect! ⇒ Boolean
Reconnects the SlackBot to the Slack API.
Constructor Details
#initialize ⇒ SlackBot
Returns a new instance of SlackBot.
52 53 54 55 56 57 58 59 60 61 |
# File 'documented/dragonrealms/commons/slackbot.rb', line 52 def initialize @initialized = false @error_message = nil ensure_slack_token unless authed?(UserVars.slack_token) return if @error_message fetch_users_list @initialized = true end |
Instance Attribute Details
#error_message ⇒ Object (readonly)
Returns the value of attribute error_message.
33 34 35 |
# File 'documented/dragonrealms/commons/slackbot.rb', line 33 def @error_message end |
Instance Method Details
#authed?(token) ⇒ Boolean
Checks if the provided token is authenticated with the Slack API.
99 100 101 102 103 104 105 |
# File 'documented/dragonrealms/commons/slackbot.rb', line 99 def authed?(token) return false unless token post('auth.test', { 'token' => token })['ok'] rescue ApiError, NetworkError false end |
#direct_message(username, message) ⇒ void
This method returns an undefined value.
Sends a direct message to a specified user on Slack.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'documented/dragonrealms/commons/slackbot.rb', line 111 def (username, ) if username.nil? || username.to_s.strip.empty? Lich::Messaging.msg('bold', 'SlackBot: Cannot send message - no username provided. Check your slackbot_username setting.') return nil end reconnect! unless initialized? unless initialized? Lich::Messaging.msg('bold', 'SlackBot: Cannot send message - not connected. Will retry on next attempt.') return nil end dm_channel = get_dm_channel(username) unless dm_channel Lich::Messaging.msg('bold', "SlackBot: Cannot send message - user '#{username}' not found in Slack workspace.") return nil end params = { 'token' => UserVars.slack_token, 'channel' => dm_channel, 'text' => "#{checkname}: #{}", 'as_user' => 'true' } post('chat.postMessage', params) rescue Error => e Lich.log "SlackBot: Failed to send Slack message to #{username}: #{e.}" Lich::Messaging.msg('bold', "SlackBot: Failed to send Slack message to #{username}: #{e.}") end |
#initialized? ⇒ Boolean
Checks if the SlackBot has been initialized successfully.
65 66 67 |
# File 'documented/dragonrealms/commons/slackbot.rb', line 65 def initialized? @initialized end |
#lnet_connected? ⇒ Boolean
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'documented/dragonrealms/commons/slackbot.rb', line 82 def lnet_connected? return false unless defined?(LNet) return false unless LNet.server return false if LNet.server.closed? if LNet.respond_to?(:last_recv) && LNet.last_recv return false if (Time.now - LNet.last_recv) > LNET_ACTIVITY_TIMEOUT end true rescue IOError, Errno::EBADF, Errno::EPIPE, NoMethodError false end |
#reconnect! ⇒ Boolean
Reconnects the SlackBot to the Slack API.
71 72 73 74 75 76 77 78 79 80 |
# File 'documented/dragonrealms/commons/slackbot.rb', line 71 def reconnect! @error_message = nil @initialized = false ensure_slack_token unless authed?(UserVars.slack_token) return false if @error_message fetch_users_list @initialized = true end |