Module: MessageSlack

Includes:
SearchOnSlack
Included in:
EnergonNotify
Defined in:
lib/modules/message_slack.rb

Overview

Module for message interactions on Slack

Instance Method Summary collapse

Methods included from SearchOnSlack

#conversation_info, #conversation_list, #conversation_type, #get_user_id, #get_user_info, #get_user_list, #search_messages_on, #verify_type

Instance Method Details

#add_reaction(icon, channel, thread) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/modules/message_slack.rb', line 74

def add_reaction(icon, channel, thread)
  @web_client.reactions_add channel: channel,
                            name: icon,
                            timestamp: thread,
                            icon_url: @bot_icon,
                            username: @bot_name
rescue Slack::Web::Api::Errors::SlackError => e
  print e.message
  false
end

#attach_format(data, title, color = '#e93d94') ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/modules/message_slack.rb', line 20

def attach_format(data, title, color = '#e93d94')
  text = ''
  data.each { |v| text << "#{v}\n" }
  [{
    "pretext": title,
    "color": color,
    "text": text
  }]
end

#discern_end(data, ts = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/modules/message_slack.rb', line 7

def discern_end(data, ts = nil)
  @thread = if data.respond_to? :thread_ts
              data.ts
            else
              ts unless ts.nil?
            end
  @channel = if data.respond_to? :channel
               data.channel
             else
               data
             end
end

#send_attachment(attachment, data, ts = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/modules/message_slack.rb', line 48

def send_attachment(attachment, data, ts = nil)
  discern_end(data, ts)
  @web_client.chat_postMessage channel: @channel,
                               attachments: attachment,
                               icon_url: @bot_icon,
                               username: @bot_name,
                               thread_ts: @thread,
                               as_user: @bot_user
end

#send_direct_message(text, channel) ⇒ Object



43
44
45
46
# File 'lib/modules/message_slack.rb', line 43

def send_direct_message(text, channel)
  dm = get_user_id(channel)
  dm == false ? false : send_message(text, dm)
end

#send_file(path, data, ts = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/modules/message_slack.rb', line 58

def send_file(path, data, ts = nil)
  file = path
  discern_end(data, ts)
  @web_client.files_upload channels: @channel,
                           icon_url: @bot_icon,
                           username: @bot_name,
                           thread_ts: @thread,
                           as_user: @bot_user,
                           file: Faraday::UploadIO.new(file, 'text'),
                           title: File.basename(file),
                           filename: File.basename(file)
rescue Slack::Web::Api::Errors::SlackError => e
  print e.message
  false
end

#send_message(text, data, ts = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/modules/message_slack.rb', line 30

def send_message(text, data, ts = nil)
  discern_end(data, ts)
  @web_client.chat_postMessage channel: @channel,
                               text: text,
                               icon_url: @bot_icon,
                               username: @bot_name,
                               thread_ts: @thread,
                               as_user: @bot_user
rescue Slack::Web::Api::Errors::SlackError => e
  print e.message
  false
end