作ったもの概要
- 次の日の予定のうち、特定ラベルの付いた予定のタイトルのメモをLINE Notifyで通知します
仕組み概要のメモ
TImeTree側
TimeTree の Calendar APIで次の日の予定リストを取得できます。
APIの実行にはPersonal Tokenを用いました。
LINE側
LINE Notifyを使いました。アクセスキーを取得してHTTP(S)リクエストでLINE Notify経由でLINEに通知できます。
トークンの取得にはWebで開発者のポータルにログインする必要があるので注意(LINEの設定でID/Passでのログインを許可するひつようがある)。
コード抜粋(Ruby)
TimeTreeの予定リストの取得。
Planの構造体クラスに次の日の予定(タイトル、概要)を格納して返します。
Plan = Struct.new(:title, :description)
def fetch_plans_from_timetree
uri = URI.parse(TIMETREE_URL)
request = Net::HTTP::Get.new(uri)
request["Accept"] = "application/vnd.timetree.v1+json"
request["Authorization"] = "Bearer #{TIMETREE_KEY}"
req_options = {
use_ssl: uri.scheme == "https",
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
response_data = JSON.parse(response.body)
plans = response_data["data"].map do |data|
next unless data["relationships"]["label"]["data"]["id"]== LABEL_ID
Plan.new(data["attributes"]["title"], data["attributes"]["description"])
end.compact
plans
end
Plan の内容をLine Notifyで通知。
def push_plans_to_line(plans)
plans.each do |plan|
uri = URI.parse(LINE_URL)
request = Net::HTTP::Post.new(uri)
request["Authorization"] = "Bearer #{LINE_KEY}"
request.set_form_data({"message": "\n#{plan.title}\n#{plan.description}"})
req_options = {
use_ssl: uri.scheme == "https",
}
Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
end
end