Class: Waylon::Slack::Message

Inherits:
Object
  • Object
show all
Includes:
Message
Defined in:
lib/waylon/slack/message.rb

Overview

A representation of Slack messages for Waylon

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, data = {}) ⇒ Message

Returns a new instance of Message.



11
12
13
14
# File 'lib/waylon/slack/message.rb', line 11

def initialize(id, data = {})
  @id = id
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/waylon/slack/message.rb', line 9

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/waylon/slack/message.rb', line 9

def id
  @id
end

Instance Method Details

#authorUser

The User that authored this Message

Returns:



18
19
20
# File 'lib/waylon/slack/message.rb', line 18

def author
  User.new(data["user"])
end

#channelChannel

The Channel where this Message was sent

Returns:



24
25
26
# File 'lib/waylon/slack/message.rb', line 24

def channel
  Channel.new(data["channel"])
end

#mentions_bot?Boolean

Does the message text mention the bot?

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/waylon/slack/message.rb', line 30

def mentions_bot?
  me = User.whoami
  reg = /(,\s+)?\s*@#{me.id},?\s*/
  ::Slack::Messages::Formatting.unescape(data["text"]) =~ reg ? true : false
end

#part_of_thread?Boolean

Is this Message a reply in a thread?

Returns:

  • (Boolean)


38
39
40
# File 'lib/waylon/slack/message.rb', line 38

def part_of_thread?
  thread_ts && thread_ts != ts
end

#private_message?Boolean Also known as: private?

Is this a private Message / direct Message?

Returns:

  • (Boolean)


44
45
46
# File 'lib/waylon/slack/message.rb', line 44

def private_message?
  channel.private?
end

#react(reaction) ⇒ void

This method returns an undefined value.

Uses the Sense’s Web Client to add a reaction to a Message

Parameters:

  • reaction (String, Symbol)

    The reaction to add (not wrapped in “:”)



53
54
55
# File 'lib/waylon/slack/message.rb', line 53

def react(reaction)
  sense.client.reactions_add(channel: channel.id, name: reaction, timestamp: ts)
end

#senseClass

Easy access to the Sense class

Returns:

  • (Class)


59
60
61
# File 'lib/waylon/slack/message.rb', line 59

def sense
  ::Waylon::Senses::Slack
end

#textString Also known as: body

The unescaped contents of the Message

Returns:

  • (String)


65
66
67
68
69
# File 'lib/waylon/slack/message.rb', line 65

def text
  me = User.whoami
  reg = /(,\s+)?\s*@#{me.id},?\s*/
  ::Slack::Messages::Formatting.unescape(data["text"]).gsub(reg, "")
end

#thread_parentString

The TS value of the parent of this Message’s thread, or its own TS if it is the parent

Returns:

  • (String)


75
76
77
# File 'lib/waylon/slack/message.rb', line 75

def thread_parent
  thread_ts || ts
end

#thread_tsString?

The TS value of the parent of this Message’s thread, if it exists

Returns:

  • (String, nil)


81
82
83
# File 'lib/waylon/slack/message.rb', line 81

def thread_ts
  data["thread_ts"].dup
end

#to_bot?Boolean

Does this Message either directly mention or is it directly to this bot?

Returns:

  • (Boolean)


87
88
89
# File 'lib/waylon/slack/message.rb', line 87

def to_bot?
  data["type"] == "app_mention" || private_message?
end

#tsString

This Message’s own TS value (which should not be used for threading if it itself is a thread reply)

Returns:

  • (String)


93
94
95
# File 'lib/waylon/slack/message.rb', line 93

def ts
  data["ts"].dup
end