Last active
April 29, 2016 14:33
-
-
Save vivahiraj/3a82857f26dbeceb066b34bcd7d62780 to your computer and use it in GitHub Desktop.
LINE BOT 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' | |
class LineBot | |
ID = "Channel ID" | |
SECRET = "Channel Secret" | |
MID = "MID" | |
TO = "送信先のID" | |
def self.send(msg) | |
headers = { | |
"Content-Type" => "application/json; charser=UTF-8", | |
"X-Line-ChannelID" => ID, | |
"X-Line-ChannelSecret" => SECRET, | |
"X-Line-Trusted-User-With-ACL" => MID | |
} | |
params = { | |
to: [TO], | |
toChannel: "1383378250", | |
eventType: "138311608800106203", | |
content: { | |
contentType: 1, | |
toType: 1, | |
text: msg, | |
} | |
} | |
begin | |
RestClient.post "https://trialbot-api.line.me/v1/events", params.to_json, headers | |
rescue => e | |
p e.response | |
return | |
end | |
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 | |
ret = [] | |
[ | |
["JR東日本","山手線"], | |
["JR東日本","東海道線"], | |
["JR東日本","中央線快速電"], | |
["東京メトロ","丸ノ内線"], | |
["東京メトロ","東西線"], | |
].each do |a| | |
ret.push(a) if TrainChk.delay?(a[0],a[1]) | |
end | |
msg = "電車遅延は発生していません" | |
unless ret.size == 0 | |
msg = "電車遅延が発生しています。" | |
ret.each do |a| | |
msg = msg + "\n #{a[1]}" | |
end | |
end | |
LineBot.send(msg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment