-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifybot.rb
executable file
·74 lines (65 loc) · 2.07 KB
/
notifybot.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
#!/usr/bin/ruby
# Author: Stuart Auchterlonie 2017-2020
# License: GPL
require 'cinch'
require 'rack'
class WebHandler
def initialize(bot)
@bot = bot
end
def call(env)
req = Rack::Request.new(env)
m_msg = "Service:"
m_msg << " " << Cinch::Formatting.format(:yellow, req.params['monitorFriendlyName'])
if (req.params['alertType'] == 1) # Down
m_msg << " is " << Cinch::Formatting.format(:red, req.params['alertTypeFriendlyName'])
elsif (req.params['alertType'] == 2) # Up
m_msg << " is " << Cinch::Formatting.format(:green, req.params['alertTypeFriendlyName'])
else
m_msg << " is " << Cinch::Formatting.format(:orange, req.params['alertTypeFriendlyName'])
end
m_msg << " - " << Cinch::Formatting.format(:yellow, req.params['alertDetails'])
@bot.info Cinch::Formatting.unformat(m_msg)
@bot.handlers.dispatch(:dispatch_message, nil, m_msg)
res = Rack::Response.new
res.status = 202
res.write "Accepted"
res.finish
end
end
class DispatchMessagePlugin
include Cinch::Plugin
listen_to :dispatch_message
def listen(m, msg)
Channel("#mythtv-dev").send "#{msg}"
end
end
class PrivMsgLogger
include Cinch::Plugin
listen_to :private, method: :on_privmsg
def on_privmsg(m)
info Cinch::Formatting.unformat(m.params[1])
end
end
class NotifyBot
def initialize
@bot = Cinch::Bot.new do
configure do |c|
c.nick = ENV['IRC_NICK'].nil? ? "DevMythLogBot" : ENV['IRC_NICK']
c.realname = ENV['IRC_REALNAME'].nil? ? "DevMythLogBot" : ENV['IRC_REALNAME']
c.password = ENV['IRC_PASSWORD']
c.server = "irc.freenode.org"
c.channels = ENV['IRC_CHANNELS'].nil? ? ["#mythtv-dev"] : ENV['IRC_CHANNELS'].split(/[^#[:word:]-]+/)
c.verbose = true
c.plugins.plugins = [PrivMsgLogger, DispatchMessagePlugin]
end
end
@bot.loggers.level = :info
@wh = WebHandler.new(@bot)
Thread.new { Rack::Handler::default.run(@wh, :Port => 9080) }
@bot.start
end
def call(env)
@wh.call(env)
end
end