Class: Waylon::Senses::Slack

Inherits:
Waylon::Sense
  • Object
show all
Defined in:
lib/waylon/senses/slack.rb

Overview

The Waylon Sense for interacting with Slack via the Events API

Class Method Summary collapse

Class Method Details

.clientSlack::Web::Client

Provides easy access to the Slack Web Client for interacting with the Slack API

Returns:

  • (Slack::Web::Client)


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

def self.client
  @client ||= ::Slack::Web::Client.new
end

.mention(user) ⇒ String

“At-mention” for Slack.

Parameters:

  • user (Waylon::User)

    The User to mention

Returns:

  • (String)


32
33
34
# File 'lib/waylon/senses/slack.rb', line 32

def self.mention(user)
  "<@#{user.handle}>"
end

.message_classClass

Required by the Waylon framework, this provides the Sense’s own Message class

Returns:

  • (Class)


114
115
116
# File 'lib/waylon/senses/slack.rb', line 114

def self.message_class
  Waylon::Slack::Message
end

.message_from_request(request) ⇒ Waylon::Message

Takes an incoming request from a webhook and converts it to a usable Waylon Message

Parameters:

  • request (Hash, Waylon::Message)

Returns:

  • (Waylon::Message)


18
19
20
21
22
23
24
25
26
27
# File 'lib/waylon/senses/slack.rb', line 18

def self.message_from_request(request)
  return request if request.is_a?(message_class)

  if request["type"] == "event_callback" && %w[app_mention message].include?(request.dig("event", "type"))
    # These are typical chat messages
    message_class.new(request["event_id"], request["event"])
  elsif request["type"] == "event_callback"
    log("Support for events of type #{request.dig("event", "type")} not yet implemented")
  end
end

.private_reply(request, text) ⇒ void

This method returns an undefined value.

Provides a simple means to privately reply to the author of a Message

Parameters:

  • request (Hash, Waylon::Message)
  • text (String)

    Reply contents



40
41
42
43
# File 'lib/waylon/senses/slack.rb', line 40

def self.private_reply(request, text)
  message = message_from_request(request)
  message.author.dm(text:)
end

.private_reply_with_blocks(request, blocks) ⇒ void

This method returns an undefined value.

Provides a simple means to privately reply to the author of a Message using Blocks

Parameters:

  • request (Hash, Waylon::Message)
  • blocks (Array)

    Blocks data to reply with



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

def self.private_reply_with_blocks(request, blocks)
  message = message_from_request(request)
  message.author.dm(blocks:)
end

.react(request, reaction) ⇒ void

This method returns an undefined value.

Allows reacting to a request via the Sense’s own mechanism

Parameters:

  • request (Hash, Waylon::Message)
  • reaction (String)


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

def self.react(request, reaction)
  message = message_from_request(request)
  message.react(reaction)
end

.reply(request, text) ⇒ void

This method returns an undefined value.

Reply to a Message in a Channel with some text

Parameters:

  • request (Hash, Waylon::Message)
  • text (String)

    Reply contents



67
68
69
70
# File 'lib/waylon/senses/slack.rb', line 67

def self.reply(request, text)
  message = message_from_request(request)
  message.channel.post(text:)
end

.reply_with_blocks(request, blocks) ⇒ void

This method returns an undefined value.

Reply to a Message in a Channel with some blocks

Parameters:

  • request (Hash, Waylon::Message)
  • blocks (Array)

    Blocks to reply with



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

def self.reply_with_blocks(request, blocks)
  message = message_from_request(request)
  message.channel.post(blocks:)
end

.run(received_web_content) ⇒ void

This method returns an undefined value.

Executed by Resque, this is how this Sense determines what to do with an incoming request

Parameters:

  • received_web_content (Hash)

    The parsed web request content from the Webhook



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/waylon/senses/slack.rb', line 84

def self.run(received_web_content)
  log("Received request of type #{received_web_content["type"]}", :debug)
  message = message_from_request(received_web_content)
  unless message
    log("Unable to handle request")
    return
  end

  if message.author == Waylon::Slack::User.whoami
    log("Ignoring my own message...", :debug)
    return
  end

  log("Responding to message from bot '#{message.author.handle}'") if message.author.bot?

  route = Waylon::SkillRegistry.route(message) || SkillRegistry.instance.default_route(message)
  enqueue(route, received_web_content)
end

.threaded_reply(request, text) ⇒ void

This method returns an undefined value.

Reply to a Message in a Thread with some text

Parameters:

  • request (Hash, Waylon::Message)
  • text (String)

    Reply contents



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

def self.threaded_reply(request, text)
  message = message_from_request(request)
  message.channel.post(text:, thread: message.thread_parent)
end

.user_classClass

Required by the Waylon framework, this provides the Sense’s own User class

Returns:

  • (Class)


120
121
122
# File 'lib/waylon/senses/slack.rb', line 120

def self.user_class
  Waylon::Slack::User
end