Skip to content

Instantly share code, notes, and snippets.

@FromAtom
Created September 21, 2018 04:10
Show Gist options
  • Save FromAtom/af3236cd777bd3275b88a2c85099c65e to your computer and use it in GitHub Desktop.
Save FromAtom/af3236cd777bd3275b88a2c85099c65e to your computer and use it in GitHub Desktop.
require 'net/http'
require 'uri'
require 'json'
require 'date'
SLACK_OAUTH_ACCESS_TOKEN = ENV['SLACK_OAUTH_ACCESS_TOKEN']
def getRokuyo
today = Date.today
endpoint = 'https://dateinfoapi.appspot.com/v1?date=' + today.to_s
uri = URI.parse(endpoint)
json = Net::HTTP.get(uri)
result = JSON.parse(json)
return result['rokuyo']
end
def getRokuyoDescription(rokuyo)
case rokuyo
when '先勝' then
return '急ぐことは吉。午前は吉、午後は凶。'
when '友引' then
return '祝い事は良いが葬式などの凶事を忌む。朝夕は吉、正午は凶。'
when '先負' then
return '何事も控えめに平静を保つ日。午前は凶、午後は吉。'
when '仏滅' then
return '万事凶。葬式や法事は構わない。'
when '大安' then
return '万事大吉。特に婚礼に良い。'
when '赤口' then
return '凶日。特に祝事は大凶。火の元、刃物に要注意。正午は吉、朝夕は凶。'
else
return '不明'
end
end
def getRokuyoEmoji(rokuyo)
case rokuyo
when '先勝' then
return ':senshou:'
when '友引' then
return ':tomobiki:'
when '先負' then
return ':senpu:'
when '仏滅' then
return ':butsumetsu:'
when '大安' then
return ':taian:'
when '赤口' then
return ':shakkou:'
else
return ':kyomu:'
end
end
def postToSlackAPI(description, emoji)
if SLACK_OAUTH_ACCESS_TOKEN.nil?
return "[ERROR] ENV['SLACK_OAUTH_ACCESS_TOKEN'] not found."
end
endpoint = 'https://slack.com/api/users.profile.set'.freeze
uri = URI.parse(endpoint)
req = Net::HTTP::Post.new(uri.request_uri)
req['Content-Type'] = 'application/json; charset=utf-8'
req['Authorization'] = 'Bearer ' + SLACK_OAUTH_ACCESS_TOKEN
payload = {
profile: {
status_text: description,
status_emoji: emoji
}
}.to_json
req.body = payload
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
return https.request(req)
end
rokuyo = getRokuyo
description = getRokuyoDescription(rokuyo)
emoji = getRokuyoEmoji(rokuyo)
response = postToSlackAPI(description, emoji)
puts response.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment