Skip to content

Instantly share code, notes, and snippets.

@codeout
Last active December 2, 2015 23:25
Show Gist options
  • Save codeout/703de1defc784e0550da to your computer and use it in GitHub Desktop.
Save codeout/703de1defc784e0550da to your computer and use it in GitHub Desktop.
require 'json'
require 'net/http'
require 'uri'
require 'webrick'
class Receiver
def initialize(pattern, slack_endpoint)
@pattern = pattern
@endpoint = slack_endpoint
end
def start
server = WEBrick::HTTPServer.new(:Port => 8080)
server.mount_proc '/' do |req, res|
data = JSON.parse(req.body)
case data['object_kind']
when 'issue', 'merge_request'
if data['object_attributes']['title'] =~ @pattern ||
data['object_attributes']['description'] =~ @pattern
notify
end
when 'note'
if data['object_attributes']['note'] =~ @pattern
notify
end
end
end
trap 'INT' do
server.shutdown
end
server.start
end
private
def notify
uri = URI.parse(@endpoint)
request = Net::HTTP::Post.new(uri.request_uri)
request.body = 'もし再起動するならあれもやりたい'
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.start {|h| h.request(request) }
end
end
Receiver.new(/再起動|reboot/, 'https://codeance.slack.com/services/hooks/slackbot?token=ccvIktQ90VrBP03gzsA6WsJK&channel=%23test').start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment