Class: Waylon::Slack::User
- Inherits:
-
Object
- Object
- Waylon::Slack::User
- Includes:
- User
- Defined in:
- lib/waylon/slack/user.rb
Overview
A representation of Slack users for Waylon
Class Method Summary collapse
-
.find_by_email(email) ⇒ User
Find a Slack User based on their email address.
-
.find_by_handle(handle) ⇒ User
Find a Slack User based on their IM handle.
-
.from_mention(mention_string) ⇒ User
Find a Slack User based on the text provided by Slack from a mention of that User.
-
.from_response(response) ⇒ User
Provide a Slask User based on a Web API response.
-
.sense ⇒ Class
Allows easy access to the Sense class.
-
.whoami ⇒ User
A convenient way use the Slack API to figure out the bot’s own info.
Instance Method Summary collapse
-
#bot? ⇒ Boolean
Is this user a bot?.
-
#data ⇒ Hash
Provides lazy, cached access to the User’s internal details.
-
#dm(text: nil, attachments: nil, blocks: nil, thread: nil) ⇒ void
Posts a direct (private) message to a user.
-
#email ⇒ String
The User’s email address.
-
#handle ⇒ String
The User’s username/chat handle.
-
#initialize(id = nil, data: {}) ⇒ User
constructor
A new instance of User.
-
#profile ⇒ Slack::Messages::Message
The User’s profile information.
-
#sense ⇒ Class
Easy access to the Sense class.
-
#status ⇒ String
The User’s current Status (comes from cache so can be outdated).
-
#team ⇒ String
The User’s Slack team ID.
-
#tz ⇒ String
The User’s time zone.
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
Not recommended as it requires an additional scope on the OAuth token
Find a Slack User based on their email address
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
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
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
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 |
.sense ⇒ Class
Allows easy access to the Sense class
43 44 45 |
# File 'lib/waylon/slack/user.rb', line 43 def self.sense ::Waylon::Senses::Slack end |
.whoami ⇒ User
A convenient way use the Slack API to figure out the bot’s own info
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?
76 77 78 |
# File 'lib/waylon/slack/user.rb', line 76 def bot? data["is_bot"] end |
#data ⇒ Hash
Provides lazy, cached access to the User’s internal details
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
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) = { channel: id } # Sends a message to the user's ID [:text] = text if text [:attachments] = if [:blocks] = blocks if blocks [:thread_ts] = thread if thread sense.client.chat_postMessage() end |
#email ⇒ String
The User’s email address
97 98 99 |
# File 'lib/waylon/slack/user.rb', line 97 def email profile["email"] end |
#handle ⇒ String
The User’s username/chat handle
103 104 105 |
# File 'lib/waylon/slack/user.rb', line 103 def handle bot? ? data["real_name"] : data["name"] end |
#profile ⇒ Slack::Messages::Message
The User’s profile information
109 110 111 |
# File 'lib/waylon/slack/user.rb', line 109 def profile data["profile"] end |
#sense ⇒ Class
Easy access to the Sense class
115 116 117 |
# File 'lib/waylon/slack/user.rb', line 115 def sense self.class.sense end |
#status ⇒ String
The User’s current Status (comes from cache so can be outdated)
121 122 123 |
# File 'lib/waylon/slack/user.rb', line 121 def status profile["status_text"] end |
#team ⇒ String
The User’s Slack team ID
127 128 129 |
# File 'lib/waylon/slack/user.rb', line 127 def team data["team_id"] end |
#tz ⇒ String
The User’s time zone
133 134 135 |
# File 'lib/waylon/slack/user.rb', line 133 def tz data["tz"] end |