Skip to content

Instantly share code, notes, and snippets.

@serinuntius
Created November 28, 2017 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serinuntius/aa49c4e478cb255c389087bebc151384 to your computer and use it in GitHub Desktop.
Save serinuntius/aa49c4e478cb255c389087bebc151384 to your computer and use it in GitHub Desktop.
おい岡田Botのバッチ(定期実行して、通知する部分)
require 'json'
require 'net/http'
require 'uri'
require 'redis'
def send_notification(text)
request_url = ENV['INCOMING_URL']
uri = URI.parse(request_url)
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true # HTTPSでよろしく
req = Net::HTTP::Post.new(uri.request_uri)
req['Content-Type'] = 'application/json' # httpリクエストヘッダの追加
payload = {
channel: ENV['TARGET_CHANNEL'],
username: ENV['BOT_NAME'],
icon_emoji: ENV['BOT_EMOJI'],
text: text
}.to_json
req.body = payload
res = https.request(req)
puts "res_code: #{res.code}"
if res.code != 200
raise 'Slack API Error'
end
end
uri = URI.parse(ENV['REDISTOGO_URL'])
redis = Redis.new(host: uri.host, port: uri.port, password: uri.password)
texts = []
keys = redis.keys('*')
keys.each do |key|
value = JSON.parse(redis.get(key))
sabun_sec = Time.now - Time.at(value['timestamp'])
if sabun_sec / 60 > ENV['DURATION'].to_i # 〇〇分経っても返信がなかったら
texts << <<-"EOS"
<@U1ZSV3QSU> #{value['user_name']}さんへの返信が#{(sabun_sec/60).to_i}分経ってるのにまだです。
<##{value['channel_id']}|#{value['channel_name']}> に入って返信しましょう。
EOS
end
end
send_notification(texts.join("\n\n"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment