Module: SlackQuestionAbilities

Included in:
SkillActions
Defined in:
src/abilities/slack_question_abilities.rb

Defined Under Namespace

Classes: Option

Instance Method Summary collapse

Instance Method Details

#ask_bool(question, vars: nil) ⇒ Boolean Also known as: ask_for_bool

Ask a question and return a boolean depending on the answer

Positive answers such as yes, ok, :+1:, etc. give true. Negative answers such as no, not, :-1:, etc. give false.

Examples:

do_this = ask_bool 'Should I perform this action?'
perform_action if do_this

Parameters:

  • question (String)

    The question to ask.

  • vars (Array<{#to_s}>, nil)

    Vars to interpolate into the question.

Returns:

  • (Boolean)

    The response.



65
66
67
68
69
70
71
72
73
74
75
76
# File 'src/abilities/slack_question_abilities.rb', line 65

def ask_bool(question, vars: nil)
  say question, vars: vars
  entities = analyze_message(await_next_response.message.text)

  if strings(:wit_entities, :yes).include? entities.single.bool_response
    true
  elsif strings(:wit_entities, :no).include? entities.single.bool_response
    false
  else
    ask_bool any_string(:ask_for_bool, :i_dont_understand)
  end
end

#ask_options(text, options, numbered: true) ⇒ Object Also known as: ask_for_options, ask_for_option, ask_option

Ask for a choice between several options

The options can be either numbered or not. Numbered options are presented as a list and the user can choose by replying with a number. Unnumbered options are presented as plain text and the user is required to reply with the option itself.

Examples:

Numbered options

person = ask_options 'Who should be next?', ['Ivan', 'Georgi']

# [bot] Who should be next?
# [bot] 1) Ivan
# [bot] 2) Georgi
# [user] 2

person #=> Georgi

Unnumbered options

person = ask_options 'Who should be next?', ['Ivan', 'Georgi'], numbered: false

# [bot] Who should be next? Ivan or Georgi?
# [user] Georgi

person #=> Georgi

Parameters:

  • text (String)

    The question to ask.

  • options (Array<String>, Array<SlackQuestionAbilities::Option>)

    The options

  • numbered (Boolean)

    Should the options be numbered?

Returns:



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'src/abilities/slack_question_abilities.rb', line 132

def ask_options(text, options, numbered: true)
  if numbered
    string_options = options.map.with_index { |option, i| "#{i + 1}) #{option}" }

    say "#{text}\n\n#{string_options.join("\n")}\n"
  else
    string_options = options.map(&:to_s)

    say "#{join_with_or(string_options)}?".capitalize
  end

  loop do
    response = await_next_response.message.text.downcase.strip

    if numbered
      response = response.gsub(/^[^0-9]*|[\(\)\.]/, '').to_i

      return options[response - 1].value if response >= 1 && response <= options.size

      number_options = (1..options.size).to_a.map { |i| "\"#{i}\"" }
      say "Не те разбирам, моля те отговори с #{join_with_or number_options}"
    else
      response = response.gsub(/\!\.\?/, '')
      selected_option = options.find { |option| option.match? response }

      return selected_option.value if selected_option

      response_options = options.map { |option| "\"#{option}\"" }
      say "Не те разбирам, моля те отговори с #{join_with_or response_options}"
    end
  end
end

#ask_string(question, vars: nil) ⇒ String Also known as: ask_for_string

Ask a question and return the answer's text

Examples:

name = ask_string 'What is your name?'
say "Hello, #{name}"

Parameters:

  • question (String)

    The question to ask.

  • vars (Array<{#to_s}>, nil)

    Vars to interpolate into the question.

Returns:

  • (String)

    The response.



46
47
48
49
# File 'src/abilities/slack_question_abilities.rb', line 46

def ask_string(question, vars: nil)
  say question, vars: vars
  await_next_response.message.text
end

#ask_time(question, vars: nil, future: nil, past: nil) ⇒ Time? Also known as: ask_for_time

Ask for datetime input

May trigger multiple questions if the first datetime is ambiguous.

Parameters:

  • question (String)

    The question to ask.

  • vars (Array<{#to_s}>, nil)

    Vars to interpolate into the question.

  • future (Boolean, nil)

    Should only future dates be allowed?

  • past (Boolean, nil)

    Should only past dates be allowed?

Returns:

  • (Time, nil)

    The time that the user responded with.



178
179
180
181
182
# File 'src/abilities/slack_question_abilities.rb', line 178

def ask_time(question, vars: nil, future: nil, past: nil)
  say question, vars: vars

  await_next_response.extracted_time(future: future, past: past)
end

#confirm(question, vars: nil) ⇒ void

This method returns an undefined value.

Ask for confirmation of an action

If a negative confirmation is given, reacts with :+1: on the answer and stops the current interaction directly. This means that the result does not need to be checked.

Examples:

name = ask_string 'What should I call you?'

confirm "I will call you #{name}, correct?"

say "Hello, #{name}"

Parameters:

  • question (String)

    The question to ask.

  • vars (Array<{#to_s}>, nil)

    Vars to interpolate into the question.



96
97
98
99
100
101
# File 'src/abilities/slack_question_abilities.rb', line 96

def confirm(question, vars: nil)
  unless ask_bool(question, vars: vars)
    react_with '+1'
    raise Interaction::StopInteraction
  end
end