8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【TimeTree】特定ラベルの予定をLINEに通知するメモ

Last updated at Posted at 2021-08-09

作ったもの概要

  • 次の日の予定のうち、特定ラベルの付いた予定のタイトルのメモをLINE Notifyで通知します

IMG_1390.PNG

仕組み概要のメモ

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
8
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?