-
Notifications
You must be signed in to change notification settings - Fork 2
/
slack_relay.rb
53 lines (44 loc) · 1.54 KB
/
slack_relay.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
require 'em-irc'
require 'logger'
REQUIRED_VARS=[
'SLACK_TOKEN',
'SLACK_ACCOUNT',
'SLACK_CHANNEL',
'IRC_SERVER',
'IRC_PORT',
'IRC_ACCOUNT',
'IRC_CHANNEL'
]
REQUIRED_VARS.each do |v|
raise "You MUST export this environment variable: #{v}" unless ENV[v]
end
class SlackRelay
# Tiphoeus here?
def self.push(source, target, message)
message=" #{message}" if message.start_with?(':')
`curl -X POST --data-urlencode 'payload={"channel": "##{ENV['SLACK_CHANNEL']}", "username": "[IRC] #{source}", "text": "#{message}", "icon_emoji": ":ghost:"}' https://#{ENV['SLACK_ACCOUNT']}.slack.com/services/hooks/incoming-webhook?token=#{ENV['SLACK_TOKEN']} -s`
end
def self.client
client = EventMachine::IRC::Client.new do
host ENV['IRC_SERVER']
port ENV['IRC_PORT']
on(:connect) do
nick(ENV['IRC_ACCOUNT'])
end
on(:nick) do
join("##{ENV['IRC_CHANNEL']}")
end
on(:join) do |channel| # called after joining a channel
message(channel, " :ok_hand: Your IRC bridge is up and :running: on ##{ENV['IRC_CHANNEL']}...")
message(channel, "don't be shy, send a Pull Request to http://github.com/lucasmartins/slack-irc with your fix or feature.")
end
on(:message) do |source, target, message| # called when being messaged
SlackRelay.push(source, target, message)
end
# callback for all messages sent from IRC server
on(:parsed) do |hash|
puts "#{hash[:prefix]} #{hash[:command]} #{hash[:params].join(' ')}"
end
end
end
end