Class: Waylon::Slack::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/waylon/slack/channel.rb

Overview

A representation of Slack channels for Waylon

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Channel.



24
25
26
27
28
29
30
# File 'lib/waylon/slack/channel.rb', line 24

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

  @id = id || data["id"]
  # @data should never be accessed directly... always use the wrapper instance method
  @data = data
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/waylon/slack/channel.rb', line 7

def id
  @id
end

Class Method Details

.from_name(name) ⇒ Channel

Allows finding a channel based on its channel name

Returns:

Raises:

  • (Slack::Web::Api::Errors::ChannelNotFound)

    When the channel doesn’t exist



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

def self.from_name(name)
  name = "##{name}" unless name.start_with?("#")
  raw = sense.client.conversations_info(channel: name)
  new(raw["channel"]["id"], data: raw["channel"])
end

.senseClass

Provides direct access to the Sense class

Returns:

  • (Class)


20
21
22
# File 'lib/waylon/slack/channel.rb', line 20

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

Instance Method Details

#archived?Boolean

Is channel archived (meaning no further messages are possible)?

Returns:

  • (Boolean)


34
35
36
# File 'lib/waylon/slack/channel.rb', line 34

def archived?
  data["is_archived"].dup
end

#dataHash

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

Returns:

  • (Hash)


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/waylon/slack/channel.rb', line 40

def data
  if !@data || @data.empty?
    # Only cache channel info for 5 min
    sense.cache("channels.#{id}", expires: 300) do
      raw_data = sense.client.conversations_info(channel: id)
      @data = raw_data["channel"]
    end
  else
    @data
  end
end

#general?Boolean

Is this the “main” channel for this team?

Returns:

  • (Boolean)


54
55
56
# File 'lib/waylon/slack/channel.rb', line 54

def general?
  data["is_general"].dup
end

#member?Boolean

Is this bot a member of the channel?

Returns:

  • (Boolean)


74
75
76
# File 'lib/waylon/slack/channel.rb', line 74

def member?
  data["is_member"]
end

#membersArray<User>

Lists channel members

Returns:

  • (Array<User>)

    channel members



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/waylon/slack/channel.rb', line 60

def members
  # Only cache channel member ids for 5 min
  ids = sense.cache("channels.#{id}.member_ids", expires: 300) do
    member_ids = []
    sense.client.conversations_members(channel: id) do |raw|
      member_ids += raw["members"]
    end
    member_ids.sort.uniq
  end
  ids.map { |m| User.new(m) }
end

#nameString

The proper channel name

Returns:

  • (String)


80
81
82
# File 'lib/waylon/slack/channel.rb', line 80

def name
  "##{data["name"]}"
end

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

This method returns an undefined value.

Posts a message to a channel

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



90
91
92
93
94
95
96
97
# File 'lib/waylon/slack/channel.rb', line 90

def post(text: nil, attachments: nil, blocks: nil, thread: nil)
  options = { channel: 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

#private?Boolean

Is this a private channel? (meaning a direct message, NOT private in the Slack sense)

Returns:

  • (Boolean)


101
102
103
# File 'lib/waylon/slack/channel.rb', line 101

def private?
  data["is_im"].dup
end

#senseClass

An instance-level helper to access the class-level method

Returns:

  • (Class)


107
108
109
# File 'lib/waylon/slack/channel.rb', line 107

def sense
  self.class.sense
end

#topicString

Provides access to the Channel’s topic

Returns:

  • (String)


113
114
115
# File 'lib/waylon/slack/channel.rb', line 113

def topic
  data.dig("topic", "value").dup
end