This repository has been archived by the owner on Mar 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitch-plays.rb
77 lines (63 loc) · 2.07 KB
/
twitch-plays.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require 'rubygems'
require 'bunny'
require 'cinch'
require 'yaml'
class IrcConfig
attr_reader :nick, :login, :password, :host, :port, :channels
def initialize(configuration_map)
@nick = configuration_map['irc_nick']
@login = configuration_map['irc_login']
@password = configuration_map['irc_password']
@host = configuration_map['irc_host']
@port = configuration_map['irc_port']
@channels = configuration_map['channels']
end
end
class RabbitConfig
attr_reader :login, :password, :host, :port, :queue
def initialize(configuration_map)
@login = configuration_map['rabbit_login']
@password = configuration_map['rabbit_password']
@host = configuration_map['rabbit_host']
@port = configuration_map['rabbit_port']
@queue = configuration_map['rabbit_queue']
end
def to_s
return "amqp://#{@login}:#{@password}@#{@host}:#{@port}"
end
end
configuration_map = YAML.load_file('configuration.yml')
$irc_config = IrcConfig.new(configuration_map)
$rabbit_config = RabbitConfig.new(configuration_map)
bot = Cinch::Bot.new do
configure do |c|
c.nick = $irc_config.nick
c.user = $irc_config.login
c.password = $irc_config.password
c.server = $irc_config.host
c.port = $irc_config.port
c.channels = $irc_config.channels
end
helpers do
def post_message(m)
# Start a communication session with RabbitMQ
conn = Bunny.new($rabbit_config.to_s)
conn.start
# open a channel
ch = conn.create_channel
# declare a queue
q = ch.queue($rabbit_config.queue, :durable => true, :auto_delete => false,
:arguments => { "x-message-ttl" => 60000, "x-max-length" => 500 })
command = m.message[1..-1].downcase
nickname = m.user.nick.downcase.capitalize
# publish a message to the default exchange which then gets routed to this queue
q.publish("#{nickname},#{command},#{Time.now}", {:content_type => 'text/plain'})
# close the connection
conn.stop
end
end
on :message, /^!(.+)/ do |m|
post_message(m)
end
end
bot.start