Class: Lich::DragonRealms::SlackBot

Inherits:
Object
  • Object
show all
Defined in:
documented/dragonrealms/commons/slackbot.rb

Overview

A bot for interacting with Slack API This class handles authentication, user management, and sending messages.

Examples:

Creating a SlackBot instance

bot = Lich::DragonRealms::SlackBot.new

Instance Method Summary collapse

Constructor Details

#initializeSlackBot

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

Examples:

bot.authed?("xoxb-1234567890")

Parameters:

  • token (String)

    The Slack token to verify

Returns:

  • (Boolean)

    Returns true if authenticated, false otherwise

Raises:

  • (StandardError)

    Raises an error if the request fails



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

Examples:

bot.direct_message("john_doe", "Hello, John!")

Parameters:

  • username (String)

    The username of the recipient

  • message (String)

    The message to send

Returns:

  • (Net::HTTPResponse)

    The response from the Slack API

Raises:

  • (StandardError)

    Raises an error if the request fails



102
103
104
105
106
107
# File 'documented/dragonrealms/commons/slackbot.rb', line 102

def direct_message(username, message)
  dm_channel = get_dm_channel(username)

  params = { 'token' => UserVars.slack_token, 'channel' => dm_channel, 'text' => "#{checkname}: #{message}", 'as_user' => true }
  post('chat.postMessage', params)
end

#find_tokenvoid

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.

Examples:

bot.find_token


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

Examples:

channel_id = bot.get_dm_channel("john_doe")

Parameters:

  • username (String)

    The username to find the DM channel for

Returns:

  • (String)

    The ID of the DM channel

Raises:

  • (StandardError)

    Raises an error if the user is not found



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

Examples:

response = bot.post("auth.test", {"token" => "xoxb-1234567890"})

Parameters:

  • method (String)

    The API method to call

  • params (Hash)

    The parameters to send with the request

Returns:

  • (Net::HTTPResponse)

    The response from the Slack API

Raises:

  • (StandardError)

    Raises an error if the request fails



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

Examples:

token = bot.request_token("Quilsilgas")

Parameters:

  • lichbot (String)

    The name of the lichbot to request the token from

Returns:

  • (String, false)

    Returns the token if found, false otherwise

Raises:

  • (Timeout::Error)

    Raises an error if the request times out



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