Class: SkillActions
- Inherits:
-
Object
- Object
- SkillActions
- Includes:
- GoogleCalendarAbilities, MqttAbilities, SlackApiAbilities, SlackMessageAbilities, SlackQuestionAbilities, SlackRubyBot::Loggable, TimerAbilities
- Defined in:
- src/core/skill_actions.rb
Class Method Summary collapse
-
.listen_for(pattern, priority: 20) {|event| ... } ⇒ void
Start listening for text in any channel the bot is invited to.
-
.on_intent(intent, priority: 10) {|event| ... } ⇒ void
Register a handler for a specific Wit.ai intent.
-
.on_mqtt_message(topic, priority: 10) {|event| ... } ⇒ void
Register a handler to be called when an MQTT message is received on a specific topic.
-
.on_reaction(reactions, priority: 10, added: true, removed: false) {|event| ... } ⇒ void
Register a handler for a reaction on any message in a channel the bot is a member of.
-
.on_start { ... } ⇒ void
Provide a block to be called when the skill is started.
-
.on_stop { ... } ⇒ void
Provide a block to be called when the skill is stopped.
-
.strings(*keys) ⇒ String, ...
Get a string from the strings.yml file.
- .teach(&block) ⇒ Object (also: learn)
Instance Method Summary collapse
- #analyze_message(text) ⇒ Object
- #any_string(*keys) ⇒ Object
- #await_next_response ⇒ Object
- #context ⇒ Object
- #humanize(stuff, *args) ⇒ Object
-
#in_channel(channel) { ... } ⇒ Object
Switch the current context to a different Slack channel.
-
#initial_channel ⇒ Object
TODO: Make this an actual channel, not the id.
-
#initial_thread ⇒ Object
TODO: Make this an actual thread, not the id.
- #interaction_id ⇒ Object
- #storage(&block) ⇒ Object
- #strings(*keys) ⇒ Object
Methods included from TimerAbilities
Methods included from SlackQuestionAbilities
#ask_bool, #ask_options, #ask_string, #ask_time, #confirm
Methods included from SlackMessageAbilities
Methods included from SlackApiAbilities
Methods included from MqttAbilities
Methods included from GoogleCalendarAbilities
Class Method Details
.listen_for(pattern, priority: 20) {|event| ... } ⇒ void
This method returns an undefined value.
Start listening for text in any channel the bot is invited to
Only one listener (across listen_for and on_intent) is called for a given message. The one that is chosen depends on their relative priorities.
83 84 85 |
# File 'src/core/skill_actions.rb', line 83 def listen_for(pattern, priority: 20, &block) skill.(pattern, priority, &block) end |
.on_intent(intent, priority: 10) {|event| ... } ⇒ void
This method returns an undefined value.
Register a handler for a specific Wit.ai intent
See teach for how to configure intents.
Only one listener (across listen_for and on_intent) is called for a given message. The one that is chosen depends on their relative priorities.
100 101 102 |
# File 'src/core/skill_actions.rb', line 100 def on_intent(intent, priority: 10, &block) skill.on_intent(intent, priority, &block) end |
.on_mqtt_message(topic, priority: 10) {|event| ... } ⇒ void
This method returns an undefined value.
Register a handler to be called when an MQTT message is received on a specific topic
135 136 137 138 139 140 141 142 143 |
# File 'src/core/skill_actions.rb', line 135 def (topic, priority: 10, &block) unless skill.app.mqtt logger.error "Cannot subscribe to MQTT topic #{topic}, no MQTT connection is configured" return end skill.app.mqtt.subscribe(topic) skill.(topic, priority, &block) end |
.on_reaction(reactions, priority: 10, added: true, removed: false) {|event| ... } ⇒ void
This method returns an undefined value.
Register a handler for a reaction on any message in a channel the bot is a member of
118 119 120 |
# File 'src/core/skill_actions.rb', line 118 def on_reaction(reactions, priority: 10, added: true, removed: false, &block) skill.on_reaction(reactions, priority, added: added, removed: removed, &block) end |
.on_start { ... } ⇒ void
This method returns an undefined value.
Provide a block to be called when the skill is started
The block is called as soon as Zayo starts and is connected to Slack. This makes it safe to directly send messages from this hook.
Note that the interaction here is started with empty context. This means that to use methods such as SlackMessageAbilities#say, you need to first provide a channel with #in_channel.
A skill may have only one start handler at a given time.
46 47 48 |
# File 'src/core/skill_actions.rb', line 46 def on_start(&block) skill.on_start(&block) end |
.on_stop { ... } ⇒ void
Not yet implemented
This method returns an undefined value.
Provide a block to be called when the skill is stopped
A skill may have only one stop handler at a given time.
58 59 60 |
# File 'src/core/skill_actions.rb', line 58 def on_stop(&block) skill.on_stop(&block) end |
.strings(*keys) ⇒ String, ...
Get a string from the strings.yml file
161 162 163 |
# File 'src/core/skill_actions.rb', line 161 def strings(*keys) skill.app.strings.dig(*keys.map(&:to_s)) end |
.teach(&block) ⇒ Object Also known as: learn
Document this
166 167 168 169 170 |
# File 'src/core/skill_actions.rb', line 166 def teach(&block) samples = WitUpdateService::Samples.new.define(&block) skill.wit_samples = samples end |
Instance Method Details
#analyze_message(text) ⇒ Object
242 243 244 |
# File 'src/core/skill_actions.rb', line 242 def (text) @app.request_wit_data(text) end |
#any_string(*keys) ⇒ Object
250 251 252 253 254 255 256 257 258 |
# File 'src/core/skill_actions.rb', line 250 def any_string(*keys) responses = strings(*keys) if responses.is_a? Array responses.sample else responses end end |
#await_next_response ⇒ Object
267 268 269 270 271 |
# File 'src/core/skill_actions.rb', line 267 def await_next_response channel, thread = response_defaults @interaction.await_next_response(channel, thread) end |
#context ⇒ Object
182 183 184 |
# File 'src/core/skill_actions.rb', line 182 def context @interaction.context end |
#humanize(stuff, *args) ⇒ Object
260 261 262 263 264 265 |
# File 'src/core/skill_actions.rb', line 260 def humanize(stuff, *args) case stuff when Date, Time, DateTime then HumanTime.new(stuff).humanize(*args) else stuff end end |
#in_channel(channel) { ... } ⇒ Object
Switch the current context to a different Slack channel
Inside the block, commands such as SlackMessageAbilities#say, SlackMessageAbilities#react_with and SlackQuestionAbilities#ask_string will send messages on this channel.
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'src/core/skill_actions.rb', line 222 def in_channel(channel) last_context = @interaction.context new_context = last_context.clone new_context.channel = Channel.new(@app, find_channel_id(channel)) new_context.thread = nil new_context. = nil new_context. = nil @interaction.context = new_context yield ensure @interaction.context = last_context end |
#initial_channel ⇒ Object
TODO: Make this an actual channel, not the id
187 188 189 |
# File 'src/core/skill_actions.rb', line 187 def initial_channel context.channel.id end |
#initial_thread ⇒ Object
TODO: Make this an actual thread, not the id
192 193 194 |
# File 'src/core/skill_actions.rb', line 192 def initial_thread context.thread ? context.thread.id : nil end |
#interaction_id ⇒ Object
196 197 198 |
# File 'src/core/skill_actions.rb', line 196 def interaction_id @interaction.id end |
#storage(&block) ⇒ Object
238 239 240 |
# File 'src/core/skill_actions.rb', line 238 def storage(&block) @app.storage.for_skill(@skill, &block) end |
#strings(*keys) ⇒ Object
246 247 248 |
# File 'src/core/skill_actions.rb', line 246 def strings(*keys) @app.strings.dig(*keys.flatten(1).map(&:to_s)) end |