Class: Waylon::Slack::User

Inherits:
Object
  • Object
show all
Includes:
User
Defined in:
lib/waylon/slack/user.rb

Overview

A representation of Slack users for Waylon

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil, data: {}) ⇒ User

Returns a new instance of User.



54
55
56
57
58
59
# File 'lib/waylon/slack/user.rb', line 54

def initialize(id = nil, data: {})
  raise "Must provide ID or details" unless id || !data.empty?

  @id = id || data["id"]
  @data = data
end

Class Method Details

.find_by_email(email) ⇒ User

Note:

Not recommended as it requires an additional scope on the OAuth token

Find a Slack User based on their email address

Parameters:

  • email (String)

Returns:



21
22
23
# File 'lib/waylon/slack/user.rb', line 21

def self.find_by_email(email)
  from_response(sense.client.users_lookupByEmail(email))
end

.find_by_handle(handle) ⇒ User

Find a Slack User based on their IM handle

Parameters:

  • handle (String)

Returns:



12
13
14
15
# File 'lib/waylon/slack/user.rb', line 12

def self.find_by_handle(handle)
  real_handle = handle.start_with?("@") ? handle : "@#{handle}"
  from_response(sense.client.users_info(user: real_handle))
end

.from_mention(mention_string) ⇒ User

Find a Slack User based on the text provided by Slack from a mention of that User

Parameters:

  • mention_string (String)

Returns:



28
29
30
# File 'lib/waylon/slack/user.rb', line 28

def self.from_mention(mention_string)
  from_response(sense.client.users_info(user: mention_string[1..]))
end

.from_response(response) ⇒ User

Provide a Slask User based on a Web API response

Parameters:

  • response (Hash)

    The response from a Web API request

Returns:



35
36
37
38
39
# File 'lib/waylon/slack/user.rb', line 35

def self.from_response(response)
  raise "Failed Request" unless response && response["ok"]

  new(data: response["user"])
end

.senseClass

Allows easy access to the Sense class

Returns:

  • (Class)


43
44
45
# File 'lib/waylon/slack/user.rb', line 43

def self.sense
  ::Waylon::Senses::Slack
end

.whoamiUser

A convenient way use the Slack API to figure out the bot’s own info

Returns:



49
50
51
52
# File 'lib/waylon/slack/user.rb', line 49

def self.whoami
  response = sense.cache("whoami") { sense.client.auth_test }
  new(response["user_id"])
end

Instance Method Details

#bot?Boolean

Is this user a bot?

Returns:

  • (Boolean)


76
77
78
# File 'lib/waylon/slack/user.rb', line 76

def bot?
  data["is_bot"]
end

#dataHash

Provides lazy, cached access to the User’s internal details

Returns:

  • (Hash)


63
64
65
66
67
68
69
70
71
72
# File 'lib/waylon/slack/user.rb', line 63

def data
  if !@data || @data.empty?
    sense.cache("users.#{id}") do
      raw_data = sense.client.users_info(user: id)
      @data = raw_data["user"]
    end
  else
    @data
  end
end

#dm(text: nil, attachments: nil, blocks: nil, thread: nil) ⇒ void

This method returns an undefined value.

Posts a direct (private) message to a user

Parameters:

  • text (String) (defaults to: nil)

    Message text or fallback text for blocks

  • attachments (Array<Hash>) (defaults to: nil)

    Old-style message attachments

  • blocks (Array<Hash>) (defaults to: nil)

    New-style block method of sending complex messages

  • thread (Integer) (defaults to: nil)

    The message timestamp for the thread id



86
87
88
89
90
91
92
93
# File 'lib/waylon/slack/user.rb', line 86

def dm(text: nil, attachments: nil, blocks: nil, thread: nil)
  options = { channel: id } # Sends a message to the user's ID
  options[:text] = text if text
  options[:attachments] = attachments if attachments
  options[:blocks] = blocks if blocks
  options[:thread_ts] = thread if thread
  sense.client.chat_postMessage(options)
end

#emailString

The User’s email address

Returns:

  • (String)


97
98
99
# File 'lib/waylon/slack/user.rb', line 97

def email
  profile["email"]
end

#handleString

The User’s username/chat handle

Returns:

  • (String)


103
104
105
# File 'lib/waylon/slack/user.rb', line 103

def handle
  bot? ? data["real_name"] : data["name"]
end

#profileSlack::Messages::Message

The User’s profile information

Returns:

  • (Slack::Messages::Message)


109
110
111
# File 'lib/waylon/slack/user.rb', line 109

def profile
  data["profile"]
end

#senseClass

Easy access to the Sense class

Returns:

  • (Class)


115
116
117
# File 'lib/waylon/slack/user.rb', line 115

def sense
  self.class.sense
end

#statusString

The User’s current Status (comes from cache so can be outdated)

Returns:

  • (String)


121
122
123
# File 'lib/waylon/slack/user.rb', line 121

def status
  profile["status_text"]
end

#teamString

The User’s Slack team ID

Returns:

  • (String)


127
128
129
# File 'lib/waylon/slack/user.rb', line 127

def team
  data["team_id"]
end

#tzString

The User’s time zone

Returns:

  • (String)


133
134
135
# File 'lib/waylon/slack/user.rb', line 133

def tz
  data["tz"]
end