Skip to content

Instantly share code, notes, and snippets.

@akkunchoi
Created March 28, 2016 09:53
Show Gist options
  • Save akkunchoi/b1168b92ca9f91f73961 to your computer and use it in GitHub Desktop.
Save akkunchoi/b1168b92ca9f91f73961 to your computer and use it in GitHub Desktop.
特定のネットワークに入ったらSlackに投稿
# thanks to
# http://tech.actindi.net/3658802473
# http://yachibit.hateblo.jp/entry/2014/04/15/230301
require 'open-uri'
def hello
require 'date'
office = ['WIFI SSID']
path = '/Users/xxxxx/Dropbox/wifi-slack/last'
return if File.exists?(path) && Date.parse(File.read(path)) == Date.today
x = `networksetup -getairportnetwork en0`.match(/Current Wi-Fi Network: (.+)/)
return unless x
ssid = x[1]
if office.include?(ssid)
params = {
token: 'YOUR SLACK API TOKEN',
channel: 'YOUR CANNEL',
text: 'TEXT',
as_user: 'true',
}.map {|k, v| "#{k}=#{v}" }.join('&')
require 'uri'
url = "https://slack.com/api/chat.postMessage?#{URI.encode(params)}"
res = open(url)
code, message = res.status
if code == '200'
File.write(path, Date.today.to_s)
else
p 'error'
end
end
end
hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment