Skip to content

Instantly share code, notes, and snippets.

@pooza
Last active May 26, 2023 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pooza/f5ec37af39576447bcdde6fbfb7146ed to your computer and use it in GitHub Desktop.
Save pooza/f5ec37af39576447bcdde6fbfb7146ed to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
dir = File.expand_path('..', __dir__)
$LOAD_PATH.unshift(File.join(dir, 'app/lib'))
ENV['BUNDLE_GEMFILE'] = File.join(dir, 'Gemfile')
URL = 'https://timetreeapis.com/calendars/__CALENDAR__/upcoming_events?timezone=Asia/Tokyo&days=7&include=attendees'
TOKEN = '__TOKEN__'
require 'tomato_shrieker'
module TomatoShrieker
today = ARGV.first ? Time.parse(ARGV.first).to_date : Date.today
uri = Ginseng::URI.parse(URL)
http = HTTP.new
entries = http.get(uri, {
headers: {
'Accept' => 'application/vnd.timetree.v1+json',
'Authorization' => "Bearer #{TOKEN}"
},
})['data']
events = Set[]
(0..2).each do |i|
date = today.next_day(i).to_date
events.merge(entries.select do |entry|
date.between?(
Time.parse(entry.dig('attributes', 'start_at')).getlocal.to_date,
Time.parse(entry.dig('attributes', 'end_at')).getlocal.to_date,
)
end)
end
result = events.map do |event|
values = event['attributes'].symbolize_keys
start_at = Time.parse(values[:start_at]).getlocal
end_at = Time.parse(values[:end_at]).getlocal
schedule = start_at.strftime('%Y-%m-%d (%a)') if start_at.to_date == end_at.to_date
schedule ||= '%s〜%s' % [start_at.strftime('%Y-%m-%d (%a)'), end_at.strftime('%Y-%m-%d (%a)')]
[
schedule,
values[:title],
values[:location],
values[:url],
values[:description],
'#予定',
].select(&:present?).join("\n")
end
puts result.join("\n=====\n")
rescue => e
warn e.message
exit 1
end
source:
command:
- bin/schedule.rb
dir: /path/to/tomato-shrieker
schedule:
cron: '2 0 * * *' # 深夜0:02
dest:
account:
bot: true
hooks:
- https://mstdn.example.com/mulukhiya/webhook/__WEBOOK_HASH__
tags:
- 予定
@pooza
Copy link
Author

pooza commented May 25, 2023

TimeTreeのスケジュールをリマインドするボット

動作前提

設置手順

schedule.rb

  • tomato-shriekerのbin/ディレクトリに置く。
  • 複数置く時は、各々を適当にリネーム。
  • 定数URLは、TimeTreeのカレンダーURLを指定。
    • 画面上でカレンダーをひとつだけ表示する様操作すれば、この形式のURLが得られるはず。
  • 定数TOKENは「動作前提」時点で取得した、TimeTreeのパーソナルアクセストークン。

schedule.yaml

  • tomato-shriekerのconfig/sources/ディレクトリに置く。
  • 複数置く時は、schedule.rbの時に行ったリネームを踏襲。
    • ファイル名から拡張子を除いた文字列が、そのソースのIDとなる。
  • 設定内容はいい塩梅に編集。
    • 特にschedule.rbの設置先やファイル名に注意。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment