Module: SlackMessageAbilities

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

Instance Method Summary collapse

Instance Method Details

#react_with(reaction) ⇒ void

This method returns an undefined value.

React to the last user message in the interaction

The same as Message#react_with applied on Context#last_message.

Parameters:

  • reaction (String)

    The name of the reaction to react with.



72
73
74
75
76
# File 'src/abilities/slack_message_abilities.rb', line 72

def react_with(reaction)
  raise 'Cannot react before a message is received' unless context.last_message

  context.last_message.react_with reaction
end

#say(text, vars: nil, channel: nil, thread: nil) ⇒ Message Also known as: reply, reply_with

Send a Slack message

In a non-direct message channel sends the message in a thread on the last received user message for the interaction.

In a direct message or if channel is changed by SkillActions#in_channel, sends the message directly.

Examples:

Given the following config/strings.yml

hi:
  - hey
  - hello

greeting:
  morning:
    - Good morning, %s!
    - Morning, %s!
say 'hi' # Sends the literal string "hi"
say :hi # Sends either "hey" or "hello" (chosen at random)
say :'greeting.morning', vars: ['Nobody'] # Sends either "Good morning, Nobody!" or "Morning, Nobody!"

Parameters:

  • text (String, Symbol)

    The message to send. Can be either a string, or a symbol. If a string, it will be sent as-is (or interpolated with vars if it was provided). Can also be a symbol, in which case it will use the symbol as a key in the config/strings.yml file.

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

    Variables to interpolate into the message. Uses String#% to do so.

  • channel (String)

    The ID or name of the channel to send the message to. Prefer using SkillActions#in_channel as opposed to this parameter. Defaults to the channel ID of Context#channel.

  • thread (String)

    The ID of the thread to send this message to. On non-DM channels, defaults to the thread ID of Context#last_message.

Returns:

  • (Message)

    The message that was sent.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'src/abilities/slack_message_abilities.rb', line 38

def say(text, vars: nil, channel: nil, thread: nil)
  channel, thread = response_defaults channel, thread

  text = any_string text.to_s.split('.') if text.is_a? Symbol
  text = text % vars if vars

  response = @app.client.web_client.chat_postMessage(
    text: text,
    channel: channel,
    thread_ts: thread,
    as_user: true
  )
  logger.info "Sent message '#{text}' to '#{channel}'#{thread && ":#{thread}"}"

  Message.new(
    @app,
    response.message.type,
    response.channel,
    response.message.thread_ts,
    response.message.user || response.message.bot_id,
    response.message.text,
    response.ts
  )
end