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' | |
ID = "Channel ID" | |
SECRET = "Channel Secret" | |
MID = "MID" | |
TO = "送信先のID" | |
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: "てすとbot", | |
} | |
} | |
p params.to_json | |
begin | |
RestClient.post "https://trialbot-api.line.me/v1/events", params.to_json, headers | |
rescue => e | |
p e.response | |
exit | |
end | |
p "send ok" |
ローカル環境で利用することを前提にしています。
送信先IDはcallbackを設定してなんとか事前に取得しておく必要があります。
これでメッセージは送信できたのですが、せっかくなので役に立つ機能を作りたいものです。
なので電車が遅延していたら通知するbotを作ってみます。
適当な時間に自動実行できるようにさせたら便利な気がします。
電車遅延情報の取得は
鉄道遅延情報のjson(https://rti-giken.jp/fhc/api/train_tetsudo/)
を使わせていただきます。
以下のような感じです。
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) |
遅延を確認したい電車情報の部分は、
鉄道遅延情報のjson(https://rti-giken.jp/fhc/api/train_tetsudo/)
を見ながら適当に変更します。
なんとなく便利そうな気がします。
LINE BOT APIを使ってみた感想としては、まだトライアルで機能が少ないので機能追加を期待したいところです。
個人的には以下のような改善されるとうれしいなぁと思うしだいです。
・グループチャットにbotから送信ができるようになってほしい
・callbackがなくてもフレンドリストなどを取得できるようになってほしい
・IPのホワイトリストは、めんどくさいのでなくしてもらいたいなぁ
0 件のコメント:
コメントを投稿