Class: Repp::Handler::Slack::SlackMessageHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/repp/handler/slack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, web_client, app) ⇒ SlackMessageHandler

Returns a new instance of SlackMessageHandler.



17
18
19
20
21
# File 'lib/repp/handler/slack.rb', line 17

def initialize(client, web_client, app)
  @client = client
  @web_client = web_client
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



16
17
18
# File 'lib/repp/handler/slack.rb', line 16

def app
  @app
end

#clientObject (readonly)

Returns the value of attribute client.



16
17
18
# File 'lib/repp/handler/slack.rb', line 16

def client
  @client
end

#web_clientObject (readonly)

Returns the value of attribute web_client.



16
17
18
# File 'lib/repp/handler/slack.rb', line 16

def web_client
  @web_client
end

Instance Method Details

#handleObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/repp/handler/slack.rb', line 28

def handle
  client.on :message do |data|
    reply_to = (data.text || "").scan(REPLY_REGEXP).map do |node|
      user = users.find { |u| u.id == node.first }
      user ? user.name : nil
    end

    receive = SlackReceive.new(
      body: data.text,
      channel: data.channel,
      user: data.user,
      type: data.type,
      ts: data.ts,
      reply_to: reply_to.compact
    )

    user = users.find { |u| u.id == data.user } || users(true).find { |u| u.id == data.user }
    receive.bot = (data['subtype'] == 'bot_message' || user.nil? || user['is_bot'])

    res = app.call(receive)
    if res.first
      channel_to_post = res.last && res.last[:channel] || receive.channel
      attachments = res.last && res.last[:attachments]
      web_client.chat_postMessage(text: res.first, channel: channel_to_post, as_user: true, attachments: attachments)
    end
  end

  client.start!
end

#users(refresh = false) ⇒ Object



23
24
25
26
# File 'lib/repp/handler/slack.rb', line 23

def users(refresh = false)
  @users = @web_client.users_list.members if refresh
  @users ||= @web_client.users_list.members
end