コマンド実行完了時にSlack通知を飛ばす

ディスクコピーやhomebrewのアップデートなど、時間が少しかかる処理が終わった際にSlackに通知を飛ばしたくなった。
macの通知センターに通知を飛ばすのだと意外と気づかなかったりする。

slacknoti.rb

require 'net/http'
require 'uri'
require 'json'

class SlackClient
	def postMessage(uri, text)
		uri = URI.parse("#{uri}")
		https = Net::HTTP.new(uri.host, uri.port)

		https.use_ssl = true
		req = Net::HTTP::Post.new(uri.request_uri)
		req["Content-Type"] = "application/json"
		payload = {
			"text" => "#{text}" 
		}.to_json
		req.body = payload
		res = https.request(req)
	end
end

#send slack notification
slack_url = "slack_api_url"
text = "command executed."
sc = SlackClient.new
sc.postMessage(slack_url, text)

bash_profile

function snoti(){
    ruby 'scrypt_path/slacknoti.rb'
}

終わったらsource ~/.bash_profileを忘れずに。
これでsnotiでslackに通知が飛ぶようになりました。