56 lines
1.7 KiB
Ruby
56 lines
1.7 KiB
Ruby
class QuestBook
|
|
include Cinch::Plugin
|
|
|
|
listen_to :questbook
|
|
|
|
def initialize(m)
|
|
@config = YAML.load_file('config.yml')
|
|
super(m)
|
|
end
|
|
|
|
def listen(m)
|
|
params = FormData.new
|
|
params.append('msg', '')
|
|
params.append('from', "#{Time.now.to_i - 5}00")
|
|
params.append('away', 0)
|
|
req = params.post_request("/forum/chat/message.php")
|
|
http = Net::HTTP.new('quest-book.ru', 443)
|
|
http.use_ssl = true
|
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
req.add_field 'Referer', 'https://quest-book.ru/forum/chat/'
|
|
req.add_field 'X-Requested-With', 'XMLHttpRequest'
|
|
response = http.request(req)
|
|
response = JSON.parse response.body
|
|
begin
|
|
if response['msgs'] then
|
|
for msg in response['msgs'] do
|
|
print = false
|
|
if msg[1] != '' then
|
|
print = true
|
|
end
|
|
color = Digest::MD5.hexdigest(msg[1]).to_i(16) % 14 + 2
|
|
who = "[\x03#{color}#{msg[1]}\x0f]"
|
|
message = msg[2]
|
|
a = Nokogiri::HTML.parse message
|
|
message = a.text
|
|
if message != @last and print then
|
|
Channel('#questbook').send("#{who}: #{message}")
|
|
@last = message
|
|
# discord webhook
|
|
http = Net::HTTP.new('discordapp.com', 443)
|
|
http.use_ssl = true
|
|
params = FormData.new
|
|
params.append('content', message)
|
|
params.append('username', msg[1])
|
|
req = params.post_request("/api/webhooks/#{@config['discord_webhook']}/#{@config['discord_token']}")
|
|
http.request(req)
|
|
end
|
|
end
|
|
end
|
|
rescue NoMethodError
|
|
puts "No method error:\n"
|
|
puts response['msgs'].inspect
|
|
end
|
|
end
|
|
end
|