Module: SlackMessage

Includes:
SlackClient, SlackSearch
Included in:
EnerbotSlack
Defined in:
lib/modules/slack_message.rb

Overview

Communication and interactions methods with Slack

Instance Method Summary collapse

Methods included from SlackSearch

#conversation_info, #conversation_list, #conversation_members, #conversation_permalink, #conversation_replies, #conversation_type, #get_user_id, #get_user_info, #get_user_list, #search_messages_on, #verify_type

Methods included from SlackClient

#configure_client

Instance Method Details

#add_reaction(icon, channel, thread) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/modules/slack_message.rb', line 79

def add_reaction(icon, channel, thread)
  client = configure_client
  client.reactions_add channel: channel,
                       name: icon,
                       timestamp: thread,
                       icon_url: ENV['SLACK_ICON'],
                       username: ENV['SLACK_NAME'],
                       as_user: false
rescue Slack::Web::Api::Errors::SlackError => e
  print e.message
  false
end

#destination_points(data, ts = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/modules/slack_message.rb', line 11

def destination_points(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



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/modules/slack_message.rb', line 43

def send_attachment(attachment, data, ts = nil)
  client = configure_client
  destination_points(data, ts)
  client.chat_postMessage channel: @channel,
                          attachments: attachment,
                          icon_url: ENV['SLACK_ICON'],
                          username: ENV['SLACK_NAME'],
                          thread_ts: @thread,
                          as_user: false
rescue Slack::Web::Api::Errors::SlackError => e
  print e.message
  false
end

#send_command(command, text, data, ts = nil) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/modules/slack_message.rb', line 71

def send_command(command, text, data, ts = nil)
  client = configure_client('web', ENV['SLACK_REAL_TOKEN'])
  destination_points(data, ts)
  client.chat_command channel: @channel,
                      command: command,
                      text: text
end

#send_direct_message(text, channel) ⇒ Object



38
39
40
41
# File 'lib/modules/slack_message.rb', line 38

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

#send_ephemeral(text, user, data, ts = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/modules/slack_message.rb', line 57

def send_ephemeral(text, user, data, ts = nil)
  client = configure_client
  destination_points(data, ts)
  client.chat_postEphemeral channel: @channel,
                            text: text,
                            user: user,
                            icon_url: ENV['SLACK_ICON'],
                            username: ENV['SLACK_NAME'],
                            as_user: false
rescue Slack::Web::Api::Errors::SlackError => e
  print e.message
  false
end

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/modules/slack_message.rb', line 92

def send_file(path, data, ts = nil)
  file = path
  client = configure_client
  destination_points(data, ts)
  client.files_upload channels: @channel,
                      icon_url: ENV['SLACK_ICON'],
                      username: ENV['SLACK_NAME'],
                      thread_ts: @thread,
                      file: Faraday::UploadIO.new(file, 'text'),
                      title: File.basename(file),
                      filename: File.basename(file),
                      as_user: false
rescue Slack::Web::Api::Errors::SlackError => e
  print e.message
  false
end

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



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/modules/slack_message.rb', line 24

def send_message(text, data, ts = nil)
  client = configure_client
  destination_points(data, ts)
  client.chat_postMessage channel: @channel,
                          text: text,
                          icon_url: ENV['SLACK_ICON'],
                          username: ENV['SLACK_NAME'],
                          thread_ts: @thread,
                          as_user: false
rescue Slack::Web::Api::Errors::SlackError => e
  print e.message
  false
end