Class: Waylon::Skill

Inherits:
Object
  • Object
show all
Includes:
BaseComponent
Defined in:
lib/waylon/skill.rb

Overview

Skills are what Waylon can do based on inputs from Senses

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseComponent

included

Constructor Details

#initialize(sense, route, request, meta) ⇒ Skill

Returns a new instance of Skill.

Parameters:

  • sense (Class, String)

    Class (or Class name) of the source Sense

  • request (String, Integer)

    Reference to the request from the Sense provider (usually ID or message itself)

  • meta (Hash)

    Optional meta data that can be passed along from a Sense for use in Skills



52
53
54
55
56
57
58
# File 'lib/waylon/skill.rb', line 52

def initialize(sense, route, request, meta)
  @sense   = sense.is_a?(Class) ? sense : Module.const_get(sense)
  @route   = SkillRegistry.find_by_name(route)
  @request = request
  @tokens  = @route.tokens(message.body) || []
  @meta    = meta
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



8
9
10
# File 'lib/waylon/skill.rb', line 8

def request
  @request
end

#routeObject (readonly)

Returns the value of attribute route.



8
9
10
# File 'lib/waylon/skill.rb', line 8

def route
  @route
end

#senseObject (readonly)

Returns the value of attribute sense.



8
9
10
# File 'lib/waylon/skill.rb', line 8

def sense
  @sense
end

#tokensObject (readonly)

Returns the value of attribute tokens.



8
9
10
# File 'lib/waylon/skill.rb', line 8

def tokens
  @tokens
end

Class Method Details

.config_namespaceString

Config namespace for config keys

Returns:

  • (String)

    The namespace for config keys



12
13
14
# File 'lib/waylon/skill.rb', line 12

def self.config_namespace
  "skills.#{component_namespace}"
end

.perform(details) ⇒ Object

Resque uses this to execute the Skill. Just defers to the ‘action` subclass method

Parameters:

  • details (Hash)

    Input details about the message for running a Skill action



18
19
20
21
# File 'lib/waylon/skill.rb', line 18

def self.perform(details)
  skill = new(details["sense"], details["route"], details["request"], details["meta"])
  skill.send(skill.route.action.to_sym)
end

.queueSymbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Redis/Resque queue name

Returns:

  • (Symbol)


26
27
28
# File 'lib/waylon/skill.rb', line 26

def self.queue
  :skills
end

.route(condition, action, allowed_groups: :everyone, help: nil, name: nil, mention_only: true) ⇒ Object

Adds skills to the SkillRegistry

Parameters:

  • condition (Condition, Regexp)

    The condition that determines if this route applies

  • action (Symbol, String)

    The method on the Skill subclass to call

  • allowed_groups (Symbol, Array<Symbol>) (defaults to: :everyone)

    The group or list of groups allowed to use this

  • help (String, Hash) (defaults to: nil)

    A description of how to use the skill



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/waylon/skill.rb', line 35

def self.route(condition, action, allowed_groups: :everyone, help: nil, name: nil, mention_only: true)
  name ||= "#{to_s.split("::").last.downcase}##{action}"
  real_cond = case condition
              when Condition
                condition
              when Regexp
                Conditions::Regex.new(condition, action, allowed_groups, help, mention_only)
              else
                log("Unknown condition for route for #{name}##{action}", :warn)
                nil
              end
  SkillRegistry.instance.register(name, self, real_cond) if real_cond
end

Instance Method Details

#acknowledgementString

Provides a random entry from a list of canned acknowledgements

Returns:

  • (String)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/waylon/skill.rb', line 62

def acknowledgement
  responses = [
    "You got it!",
    "As you wish.",
    "Certainly!",
    "Sure thing!",
    "Absolutely!",
    "No problem.",
    "Consider it done.",
    "Of course!",
    "I'd be delighted!",
    "Right away!",
    "Gladly!",
    "All right.",
    "I'm all over it.",
    "I'm on it!",
    "Will do!"
  ]
  responses.sample
end

#codify(text) ⇒ String

Provides a simple way to convert some text to a code block in a way the Sense understands

Parameters:

  • text (String)

    The string to codify

Returns:

  • (String)

    Properly wrapped content



86
87
88
# File 'lib/waylon/skill.rb', line 86

def codify(text)
  sense.codify(text)
end

#detailsHash

The details used to call this Skill

Returns:

  • (Hash)


92
93
94
95
96
97
98
99
100
# File 'lib/waylon/skill.rb', line 92

def details
  {
    sense: @sense,
    message: @request,
    tokens: @tokens,
    meta: @meta,
    route: route.name
  }
end

#mention(user) ⇒ String

Defers to the Sense to determine how to mention a user

Parameters:

Returns:

  • (String)


105
106
107
# File 'lib/waylon/skill.rb', line 105

def mention(user)
  sense.mention(user)
end

#messageWaylon::Message

Provides a wrapped message for responding to the received Sense

Returns:



111
112
113
# File 'lib/waylon/skill.rb', line 111

def message
  sense.message_from_request(request)
end

#named_tokensObject



115
116
117
# File 'lib/waylon/skill.rb', line 115

def named_tokens
  @named_tokens ||= @route.named_tokens(message.body) || {}
end

#react(type) ⇒ Boolean, String

Defers to the Sense to react to a message

Parameters:

  • type (String)

    The type of reaction to send

Returns:

  • (Boolean, String)


122
123
124
125
126
# File 'lib/waylon/skill.rb', line 122

def react(type)
  return false unless sense.supports?(:reactions)

  sense.react(request, type)
end

#reply(text, private: false) ⇒ Object

Defers to the Sense to determine how to reply to a message

Parameters:

  • text (String)

    The reply text



130
131
132
133
134
135
136
137
# File 'lib/waylon/skill.rb', line 130

def reply(text, private: false)
  if private && sense.supports?(:private_messages)
    sense.private_reply(request, text)
  else
    log("Unable to send private message for Sense #{sense.name}, replying instead", :debug) if private
    sense.reply(request, text)
  end
end

#reply_with_blocks(blocks, private: false) ⇒ Object

Defers to the Sense to determine how to reply to a message with rich content

Parameters:

  • blocks (String)

    The reply blocks



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/waylon/skill.rb', line 141

def reply_with_blocks(blocks, private: false)
  unless sense.supports?(:blocks)
    log("Unable to use blocks with Sense #{sense.name}")
    return false
  end
  if private
    sense.private_reply_with_blocks(request, blocks)
  else
    log("Unable to send private message for Sense #{sense.name}, replying instead", :debug) if private
    sense.reply_with_blocks(request, blocks)
  end
end

#threaded_reply(text) ⇒ Object

Defers to the Sense to do threaded replies if it can, otherwise it falls back to normal replies

Parameters:

  • text (String)

    The reply text



156
157
158
159
160
161
162
163
# File 'lib/waylon/skill.rb', line 156

def threaded_reply(text)
  if sense.supports?(:threads)
    sense.threaded_reply(request, text)
  else
    log("Unable to reply in theads for Sense #{sense.name}, replying instead", :debug)
    sense.reply(request, text)
  end
end