Module: Ruboty::Adapters::SlackRTM::ReactionAddedSlackRTM

Included in:
Ruboty::Adapters::SlackRTM
Defined in:
lib/ruboty/slack_reaction_added/extension/adapter.rb

Instance Method Summary collapse

Instance Method Details

#channel_info(channel_id) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ruboty/slack_reaction_added/extension/adapter.rb', line 62

def channel_info(channel_id)
  @channel_info_caches[channel_id] ||= begin
    resp = case channel_id
      when /^C/, /^D/, /^G/
        client.conversations_info(channel: channel_id)
      else
        {}
      end

    resp['channel']
  end
end

#on_message_from_reaction_added(data) ⇒ Object



18
19
20
21
22
23
24
25
26
27
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
57
58
59
60
# File 'lib/ruboty/slack_reaction_added/extension/adapter.rb', line 18

def on_message_from_reaction_added(data)
  user = (data['user']) || {}
  reaction_added_user = (data['reaction_added']['user']) || {}

  channel = channel_info(data['channel'])

  if (data['subtype'] == 'bot_message' || user['is_bot']) && ENV['SLACK_IGNORE_BOT_MESSAGE'] == '1'
    return
  end

  if channel
    return if channel['name'] == (ENV['SLACK_GENERAL_NAME'] || 'general') && ENV['SLACK_IGNORE_GENERAL'] == '1'

    channel_to = expose_channel_name? ? "##{channel['name']}" : channel['id']
  else # direct message
    channel_to = data['channel']
  end

  message_info = {
    from: data['channel'],
    from_name: user['name'],
    to: channel_to,
    channel: channel,
    user: user,
    ts: data['ts'],
    thread_ts: data['thread_ts'],
    time: Time.at(data['ts'].to_f),
    reaction: data['reaction_added']['reaction'],
    reaction_by: reaction_added_user['name']
  }


  text, mention_to = extract_mention(data['text'])
  robot.receive(message_info.merge(body: text, mention_to: mention_to))

  (data['attachments'] || []).each do |attachment|
    body, body_mention_to = extract_mention(attachment['fallback'] || "#{attachment['text']} #{attachment['pretext']}".strip)

    unless body.empty?
      robot.receive(message_info.merge(body: body, mention_to: body_mention_to))
    end
  end
end

#on_reaction_added(data) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/ruboty/slack_reaction_added/extension/adapter.rb', line 6

def on_reaction_added(data)
  # Ruboty.logger.info data
  channel = data['item']['channel']
  histories = @client.conversations_history(channel: channel, latest: data['item']['ts'], inclusive: true, limit: 1)
  message = histories['messages'][0]
  message['channel'] = channel
  message['reaction_added'] = data
  # Ruboty.logger.info message
  # on_message(message)
  on_message_from_reaction_added(message)
end


75
76
77
78
# File 'lib/ruboty/slack_reaction_added/extension/adapter.rb', line 75

def permalink(channel_id, ts)
  permalink = @client.chat_getPermalink(channel: channel_id, message_ts: ts)
  permalink['permalink']
end