Created
April 4, 2017 13:14
-
-
Save vivahiraj/e0c4c36d417853c9ca05acb8dc62993c to your computer and use it in GitHub Desktop.
LINE Messaging APIを利用して電車遅延情報を通知する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
require 'rest-client' | |
require 'json' | |
require 'date' | |
require 'holiday_jp' | |
require 'gmail' | |
class MyMail | |
ID = "GMail address" | |
PW = "GMail password" | |
TO = "mail send to address" | |
def self.send(sbj,msg) | |
gmail = Gmail.new(ID, PW) | |
message = | |
gmail.generate_message do | |
to TO | |
subject sbj | |
body msg | |
end | |
gmail.deliver(message) | |
gmail.logout | |
end | |
end | |
class LineBot | |
TOKEN = "Channel Access Token" | |
TO = "送信先のID" | |
def self.send(msg) | |
headers = { | |
"Content-Type" => "application/json; charser=UTF-8", | |
"Authorization" => "Bearer #{TOKEN}", | |
} | |
params = { | |
to: TO, | |
messages: [ | |
{ | |
type: "text", | |
text: msg, | |
} | |
] | |
} | |
RestClient.post "https://api.line.me/v2/bot/message/push", params.to_json, headers | |
end | |
end | |
class TrainChk | |
@@cache = nil | |
def self.delay?(company,line) | |
if @@cache.nil? | |
begin | |
response = RestClient.get "https://rti-giken.jp/fhc/api/train_tetsudo/delay.json" | |
rescue => e | |
p e.response | |
return | |
end | |
json = JSON.parser.new(response.to_str) | |
hash = json.parse() | |
@@cache = hash | |
end | |
hash = @@cache | |
ret = false | |
hash.each do |h| | |
ret = true if h["company"] == company and h["name"] == line | |
end | |
ret | |
end | |
end | |
if HolidayJp.holiday?(Date.today) | |
print "#{Time.now.to_s} holiday!\n" | |
exit | |
end | |
ret = [] | |
[ | |
["JR東日本","山手線"], | |
["JR東日本","東海道線"], | |
["JR東日本","中央線快速電"], | |
["東京メトロ","丸ノ内線"], | |
["東京メトロ","東西線"], | |
].each do |a| | |
ret.push(a) if TrainChk.delay?(a[0],a[1]) | |
end | |
unless ret.size == 0 | |
msg = "電車遅延が発生しています。" | |
ret.each do |a| | |
msg = msg + "\n #{a[1]}" | |
end | |
begin | |
LineBot.send(msg) | |
rescue => e | |
print "#{Time.now.to_s} line bot error raise!\n" | |
MyMail.send "line bot error raise",e.response | |
exit | |
end | |
print "#{Time.now.to_s} delay train!\n" | |
else | |
print "#{Time.now.to_s} no delay train.\n" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment