Class: Lich::DragonRealms::SlackBot
- Inherits:
-
Object
- Object
- Lich::DragonRealms::SlackBot
- Defined in:
- documented/dragonrealms/commons/slackbot.rb
Overview
A bot for interacting with Slack API This class handles authentication, user management, and sending messages.
Instance Method Summary collapse
-
#authed?(token) ⇒ Boolean
Checks if the provided token is authenticated with Slack.
-
#direct_message(username, message) ⇒ Net::HTTPResponse
Sends a direct message to a specified user.
-
#find_token ⇒ void
Attempts to find a valid Slack token from known lichbots Searches through predefined lichbots and updates the UserVars with a valid token if found.
-
#get_dm_channel(username) ⇒ String
Retrieves the direct message channel ID for a given username.
-
#initialize ⇒ SlackBot
constructor
Initializes a new SlackBot instance Sets up the API URL and retrieves the user list.
-
#post(method, params) ⇒ Net::HTTPResponse
Sends a POST request to the Slack API.
-
#request_token(lichbot) ⇒ String, false
Requests a Slack token from a specified lichbot.
Constructor Details
#initialize ⇒ SlackBot
Initializes a new SlackBot instance Sets up the API URL and retrieves the user list.
11 12 13 14 15 16 17 18 19 |
# File 'documented/dragonrealms/commons/slackbot.rb', line 11 def initialize @api_url = 'https://slack.com/api/' @lnet = (Script.running + Script.hidden).find { |val| val.name == 'lnet' } find_token unless authed?(UserVars.slack_token) params = { 'token' => UserVars.slack_token } res = post('users.list', params) @users_list = JSON.parse(res.body) end |
Instance Method Details
#authed?(token) ⇒ Boolean
Checks if the provided token is authenticated with Slack
27 28 29 30 31 32 |
# File 'documented/dragonrealms/commons/slackbot.rb', line 27 def authed?(token) params = { 'token' => token } res = post('auth.test', params) body = JSON.parse(res.body) body['ok'] end |
#direct_message(username, message) ⇒ Net::HTTPResponse
Sends a direct message to a specified user
102 103 104 105 106 107 |
# File 'documented/dragonrealms/commons/slackbot.rb', line 102 def (username, ) dm_channel = get_dm_channel(username) params = { 'token' => UserVars.slack_token, 'channel' => dm_channel, 'text' => "#{checkname}: #{}", 'as_user' => true } post('chat.postMessage', params) end |
#find_token ⇒ void
This method returns an undefined value.
Attempts to find a valid Slack token from known lichbots Searches through predefined lichbots and updates the UserVars with a valid token if found.
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'documented/dragonrealms/commons/slackbot.rb', line 64 def find_token lichbots = %w[Quilsilgas] echo 'Looking for a token...' return if lichbots.any? do |bot| token = request_token(bot) authed = authed?(token) if token UserVars.slack_token = token if authed authed end echo 'Unable to locate a token :[' exit end |
#get_dm_channel(username) ⇒ String
Retrieves the direct message channel ID for a given username
115 116 117 118 |
# File 'documented/dragonrealms/commons/slackbot.rb', line 115 def get_dm_channel(username) user = @users_list['members'].find { |u| u['name'] == username } user['id'] end |
#post(method, params) ⇒ Net::HTTPResponse
Sends a POST request to the Slack API
85 86 87 88 89 90 91 92 93 |
# File 'documented/dragonrealms/commons/slackbot.rb', line 85 def post(method, params) uri = URI.parse("#{@api_url}#{method}") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE req = Net::HTTP::Post.new(uri.path) req.set_form_data(params) http.request(req) end |
#request_token(lichbot) ⇒ String, false
Requests a Slack token from a specified lichbot
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'documented/dragonrealms/commons/slackbot.rb', line 40 def request_token(lichbot) ttl = 10 send_time = Time.now @lnet.unique_buffer.push("chat to #{lichbot} RequestSlackToken") loop do line = get pause 0.05 return false if Time.now - send_time > ttl case line when /\[Private\]-.*:#{lichbot}: "slack_token: (.*)"/ msg = Regexp.last_match(1) return msg != 'Not Found' ? msg : false when /\[server\]: "no user .*/ return false end end end |