https://kingyo-bachi.blogspot.jp/2017/04/line-bot-apiline-messaging-api.html
以前
LINE BOT APIを利用して電車遅延情報を送信する
というのを書いたのですが、ちょっと改造しました。
以下をできるようにしようと思います。
・祝日は通知しなくてよくしたい
・localで実行しているがIPアドレスが変わる可能性があるので送信できなかったときはメール通知をしたい
祝日に関してはholiday_jpというgemを使って判別することにしました。
https://github.com/komagata/holiday_jp
メール送信はGmailを利用しようと思います。以下が参考になりました。
http://qiita.com/nownabe/items/3a348c86b3c0a2c87ab0
http://grottad.com/blog/228
で、改良版の電車遅延情報を送信するものは以下のとおりです。
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 | |
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, | |
} | |
} | |
RestClient.post "https://trialbot-api.line.me/v1/events", 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 |
これを以下のような感じでcron設定して月曜から金曜までの出勤前の6:50に動作するようにしました。
50 6 * * 1,2,3,4,5 /usr/bin/ruby /home/hogehoge/ruby/line_bot/train_chk2.rb >> /home/hogehoge/ruby/line_bot/train_chk.log
0 件のコメント:
コメントを投稿