Class: Waylon::Senses::Slack
- Inherits:
-
Waylon::Sense
- Object
- Waylon::Sense
- Waylon::Senses::Slack
- Defined in:
- lib/waylon/senses/slack.rb
Overview
The Waylon Sense for interacting with Slack via the Events API
Class Method Summary collapse
-
.client ⇒ Slack::Web::Client
Provides easy access to the Slack Web Client for interacting with the Slack API.
-
.mention(user) ⇒ String
“At-mention” for Slack.
-
.message_class ⇒ Class
Required by the Waylon framework, this provides the Sense’s own Message class.
-
.message_from_request(request) ⇒ Waylon::Message
Takes an incoming request from a webhook and converts it to a usable Waylon Message.
-
.private_reply(request, text) ⇒ void
Provides a simple means to privately reply to the author of a Message.
-
.private_reply_with_blocks(request, blocks) ⇒ void
Provides a simple means to privately reply to the author of a Message using Blocks.
-
.react(request, reaction) ⇒ void
Allows reacting to a request via the Sense’s own mechanism.
-
.reply(request, text) ⇒ void
Reply to a Message in a Channel with some text.
-
.reply_with_blocks(request, blocks) ⇒ void
Reply to a Message in a Channel with some blocks.
-
.run(received_web_content) ⇒ void
Executed by Resque, this is how this Sense determines what to do with an incoming request.
-
.threaded_reply(request, text) ⇒ void
Reply to a Message in a Thread with some text.
-
.user_class ⇒ Class
Required by the Waylon framework, this provides the Sense’s own User class.
Class Method Details
.client ⇒ Slack::Web::Client
Provides easy access to the Slack Web Client for interacting with the Slack API
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.
32 33 34 |
# File 'lib/waylon/senses/slack.rb', line 32 def self.mention(user) "<@#{user.handle}>" end |
.message_class ⇒ Class
Required by the Waylon framework, this provides the Sense’s own Message class
114 115 116 |
# File 'lib/waylon/senses/slack.rb', line 114 def self. 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
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/waylon/senses/slack.rb', line 18 def self.(request) return request if request.is_a?() if request["type"] == "event_callback" && %w[app_mention message].include?(request.dig("event", "type")) # These are typical chat messages .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
40 41 42 43 |
# File 'lib/waylon/senses/slack.rb', line 40 def self.private_reply(request, text) = (request) ..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
49 50 51 52 |
# File 'lib/waylon/senses/slack.rb', line 49 def self.private_reply_with_blocks(request, blocks) = (request) ..dm(blocks:) end |
.react(request, reaction) ⇒ void
This method returns an undefined value.
Allows reacting to a request via the Sense’s own mechanism
58 59 60 61 |
# File 'lib/waylon/senses/slack.rb', line 58 def self.react(request, reaction) = (request) .react(reaction) end |
.reply(request, text) ⇒ void
This method returns an undefined value.
Reply to a Message in a Channel with some text
67 68 69 70 |
# File 'lib/waylon/senses/slack.rb', line 67 def self.reply(request, text) = (request) .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
76 77 78 79 |
# File 'lib/waylon/senses/slack.rb', line 76 def self.reply_with_blocks(request, blocks) = (request) .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
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) = (received_web_content) unless log("Unable to handle request") return end if . == Waylon::Slack::User.whoami log("Ignoring my own message...", :debug) return end log("Responding to message from bot '#{..handle}'") if ..bot? route = Waylon::SkillRegistry.route() || SkillRegistry.instance.default_route() 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
107 108 109 110 |
# File 'lib/waylon/senses/slack.rb', line 107 def self.threaded_reply(request, text) = (request) .channel.post(text:, thread: .thread_parent) end |