Class: Redata::Notice

Inherits:
Object
  • Object
show all
Defined in:
lib/redata/notice.rb

Instance Method Summary collapse

Constructor Details

#initializeNotice

Returns a new instance of Notice.



3
4
5
6
7
8
9
10
11
# File 'lib/redata/notice.rb', line 3

def initialize
	Log.error! "ERROR: slack bot token missing" if !RED.slack['token'] || RED.slack['token'].empty?
	@slack = Slack::Client.new :token => RED.slack['token']
	channel_exist = false
	@slack.channels_list['channels'].each do |channel|
		channel_exist = true if channel['name'] == RED.slack['channel']
	end
	Log.error! "ERROR: slack channel #{RED.slack['channel']} not exists" unless channel_exist
end

Instance Method Details

#log(msg, log = nil) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/redata/notice.rb', line 13

def log(msg, log=nil)
	log_content = "```\n#{File.read(log).split("\n").map{|line| line.gsub(/\[0;\d{2};\d{2}m/, '').gsub(/\[0m/, '')}.join("\n")}\n```" if log
	@slack.chat_postMessage({
		:channel => RED.slack['channel'],
		:text => "#{msg}\n#{log_content}",
		:as_user => true
	})
end

#mention(user_name, msg) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/redata/notice.rb', line 22

def mention(user_name, msg)
	@slack.users_list['members'].each do |user|
		if user['name'] == user_name
			@slack.chat_postMessage({
				:channel => RED.slack['channel'],
				:text => "<@#{user['id']}> #{msg}",
				:as_user => true
			})
		end
	end
end